Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I find difference between two timestamps?

by my_perl (Initiate)
on Nov 16, 2004 at 17:00 UTC ( #408168=perlquestion: print w/replies, xml ) Need Help??
my_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have 2 timestamps in following format
$start = 2004-11-15T18:59:52.863Z $end = 2004-11-15T19:09:52.972Z
I need to find how many hr, min, sec, millisec is between them
Please help
Thanks

20041116 Edit by ysth: code tags

Replies are listed 'Best First'.
Re: How do I find difference between two timestamps?
by davorg (Chancellor) on Nov 16, 2004 at 17:19 UTC

    Pretty simple with the correct DateTime modules installed.

    #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $fmt = '%Y-%m-%dT%H:%M:%S.%3NZ'; my ($start, $end) = qw(2004-11-15T18:59:52.863Z 2004-11-15T19:09:52.972Z); my $parser = DateTime::Format::Strptime->new(pattern => $fmt); my $dt1 = $parser->parse_datetime($start) or die; my $dt2 = $parser->parse_datetime($end) or die; my $diff = $dt2 - $dt1; print $diff->hours, " hours\n"; print $diff->minutes, " minutes\n"; print $diff->seconds, " seconds\n"; print $diff->nanoseconds / 1e6, " milliseconds\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How do I find difference between two timestamps?
by ikegami (Pope) on Nov 16, 2004 at 17:20 UTC
    use Time::Local; $start = '2004-11-15T18:59:52.863Z'; $end = '2004-11-15T19:09:52.972Z'; @parts = $start =~ /^(.{4})-(.{2})-(.{2})T(.{2}):(.{2}):(.{2})\.(.{3}) +Z$/; $start_secs = pop(@parts) / 1000; $start_secs += timegm reverse @parts; @parts = $end =~ /^(.{4})-(.{2})-(.{2})T(.{2}):(.{2}):(.{2})\.(.{3})Z$ +/; $end_secs = pop(@parts) / 1000; $end_secs += timegm reverse @parts; $diff = $end_secs - $start_secs; printf("%.3f$/", $diff); # 600.109
      Thanks :)
Re: How do I find difference between two timestamps?
by tomhukins (Curate) on Nov 16, 2004 at 17:18 UTC
Re: How do I find difference between two timestamps?
by tall_man (Parson) on Nov 16, 2004 at 17:13 UTC
    The timegm function in Time::Local can convert a Zulu (GMT) time into a timestamp. Since your date formats are nonstandard, it would be simplest to break out the year, month, day, hours, minutes, and seconds with a regular expression or substrings, then do:
    $time = timegm($sec,$min,$hour,$mday,$mon,$year);
    These times can be subtracted to give differences in seconds. You'll have to subtract the milliseconds separately and add that to the difference.
      Since your date formats are nonstandard

      Non-standard how? They conform to ISO 8601, which defines the international standard for expressing dates and times.

        That may be so according to the most recent 8601 standard, but as far as Perl is concerned it is not. For example, Date::Manip claims to be able to handle ISO 8601 date formats, but the following will fail:
        use strict; use Date::Manip; my $string = "2004-11-15T18:59:52.863Z"; my $date = ParseDate($string); print $date,"\n";
Re: How do I find difference between two timestamps?
by castaway (Parson) on Nov 17, 2004 at 08:49 UTC
    You could also skip all this 'figuring out what format that is, and passing it to something', and use a module that parses exactly what you have there, to wit: Time::ParseDate.
    perl -MTime::ParseDate -le'print scalar localtime(parsedate("2004-11-1 +5T18:59:52.863Z"))'
    Gives me:
    Mon Nov 15 18:59:52 2004

    Which looks good to me. So just run parsedate() on both, subtract the results, then all you'll need to do is convert the resulting number of seconds to something you understand ,)

    C.

Re: How do I find difference between two timestamps?
by Diakoneo (Beadle) on Nov 16, 2004 at 18:42 UTC
    I'm new to programming, but can you convert them to epoch time, subtract them, then convert them back? Or is that inefficient?
      I'm not completly sure, but I think the epoch counts seconds, so the OP's timestamps would lose their information on milliseconds during the conversion.
      That's what all the previously mentioned modules do. It's not a question of efficiency, it's a question of necessity.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://408168]
Approved by cLive ;-)
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-12-18 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (497 votes). Check out past polls.

    Notices?