http://qs1969.pair.com?node_id=408168

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 (Patriarch) 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.