Package to calculate time diff

Discussion in 'Perl Misc' started by * Tong *, Sep 21, 2005.

  1. * Tong *

    * Tong * Guest

    Hi,

    I'm wondering which package can do time diff calculation. I.e., something
    that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.

    I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
    of them can. Maybe I'm too blunt, so I'm not going to show my stupid
    trials here.

    Thanks for your help.

    --
    Tong (remove underscore(s) to reply)
    *niX Power Tools Project: http://xpt.sourceforge.net/
    - All free contribution & collection
     
    * Tong *, Sep 21, 2005
    #1
    1. Advertising

  2. * Tong * wrote:
    > Hi,
    >
    > I'm wondering which package can do time diff calculation. I.e., something
    > that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.
    >
    > I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
    > of them can. Maybe I'm too blunt, so I'm not going to show my stupid
    > trials here.
    >
    > Thanks for your help.
    >
    > --
    > Tong (remove underscore(s) to reply)
    > *niX Power Tools Project: http://xpt.sourceforge.net/
    > - All free contribution & collection


    use Date::Manip;

    and here's a link that provides some examples of its use:
    http://www.icewalkers.com/Perl/5.8.0/lib/Date/Manip.html
     
    it_says_BALLS_on_your forehead, Sep 21, 2005
    #2
    1. Advertising

  3. * Tong * wrote:
    >
    > I'm wondering which package can do time diff calculation. I.e., something
    > that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.
    >
    > I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
    > of them can. Maybe I'm too blunt, so I'm not going to show my stupid
    > trials here.



    $ perl -le'
    use Time::Local;
    my $end = q/0:40:33/;
    my $start = q/0:28:50/;

    sub time_diff { join q/:/, ( gmtime timegm( reverse 1, $_[1] =~ /\d+/g ) -
    timegm( reverse 1, $_[0] =~ /\d+/g ) )[2,1,0] }

    print "Start: $start End: $end Diff: ", time_diff $start, $end;
    '
    Start: 0:28:50 End: 0:40:33 Diff: 0:11:43


    Or if you want to use sprintf to get leading zeros:

    sub time_diff { sprintf q/%3$d:%2$02d:%1$02d/, gmtime timegm( reverse 1, $_[1]
    =~ /\d+/g ) - timegm( reverse 1, $_[0] =~ /\d+/g ) }




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 21, 2005
    #3
  4. * Tong * wrote:
    > Hi,
    >
    > I'm wondering which package can do time diff calculation. I.e., something
    > that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.
    >
    > I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
    > of them can. Maybe I'm too blunt, so I'm not going to show my stupid
    > trials here.
    >
    > Thanks for your help.
    >
    > --
    > Tong (remove underscore(s) to reply)
    > *niX Power Tools Project: http://xpt.sourceforge.net/
    > - All free contribution & collection


    Perhaps you would prefer to use Ruby:

    class String
    def to_time
    Time.local(2000,1,1,*self.split(':'))
    end
    end

    def timediff( t1, t2 )
    Time.at(t2.to_time - t1.to_time).gmtime.strftime("%H:%M:%S")
    end

    puts timediff( '0:28:50', '0:40:33' )
     
    William James, Sep 21, 2005
    #4
  5. * Tong *

    l v Guest

    * Tong * wrote:
    > Hi,
    >
    > I'm wondering which package can do time diff calculation. I.e., something
    > that can do time_diff 0:40:33 0:28:50 and gives me 00:11:43.
    >
    > I've tried Date::Simple, Date::Calc, Class::Date and DateTime, but none
    > of them can. Maybe I'm too blunt, so I'm not going to show my stupid
    > trials here.
    >
    > Thanks for your help.
    >


    I don't know how the Ruby solution will help you out, but Date::Calc is
    my preferred *Perl* module. However, when comparing times with
    Date::Calc you need to pass it the date (year, month, day) as well. For
    this example I used Date::Calc's Today function for that. If you
    determine the correct date, then Date::Calc will correctly calculate the
    time diff when spanning different days.

    use Date::Calc qw(Today Delta_YMDHMS);

    my $end = q/0:40:33/;
    my $start = q/0:28:50/;

    my ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds) =
    Delta_YMDHMS(Today(), (split /:/, $start) ,
    Today(), (split /:/, $end) );

    printf "Start: %s End: %s Diff: %02d:%02d:%02d",
    $start, $end, $Dh, $Dm, $Ds;

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    l v, Sep 22, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    528
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    672
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,971
    Steve Fulton
    Aug 31, 2004
  4. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    209
    Austin Ziegler
    Aug 9, 2004
  5. Ruwan Budha
    Replies:
    4
    Views:
    1,193
    Kirk Haines
    Mar 9, 2011
Loading...

Share This Page