Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

time difference calculation

by Anonymous Monk
on Oct 28, 2013 at 12:27 UTC ( [id://1059968]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm trying to calculate the number of seconds between "now" and an ISO8601 date

So far I have the following code,

use Time::Local; $start = '2013-10-28T18:59:52.863Z'; @parts = $start =~ /^(.{4})-(.{2})-(.{2})T(.{2}):(.{2}):(.{2})\.(.{3}) +Z$/; $start_secs = pop(@parts) / 1000; $start_secs += timegm reverse @parts; print "start_secs : $start_secs\n"; $end=timegm(gmtime()); print "end_secs : $end_secs\n"; $diff = $end_secs - $start_secs; print "$diff";
It's not working, so can someone suggest a working/better/more efficient way?

Replies are listed 'Best First'.
Re: time difference calculation
by RMGir (Prior) on Oct 28, 2013 at 12:36 UTC
    You've got a simple typo (which use strict; use warnings; would have pointed out to you...)
    #$end=timegm(gmtime()); #should be $end_secs=timegm(gmtime());

    Mike
      D'oh! Thanks.

      That still doesn't seem correct, as I'm getting a difference of around 750 hours.

        You have a difference of 750 hours. Have you looked at the expected time difference?

        Maybe you want to study again the exact ranges of allowed input parameters for Time::Local. Especially re-read the second paragraph of the description, where the documentation draws particular attention to the allowed values for the month.

        Maybe you want to try the following dates and study the error message you get

        2013-02-31 2013-01-28 2013-01-29 2013-01-30 2013-01-31
Re: time difference calculation
by hdb (Monsignor) on Oct 28, 2013 at 12:51 UTC

    At a first glance, this DateTime::Format::ISO8601 should be helpful. Probably this as well: ISO 8601...

    Update: Here is some sample code

    use strict; use warnings; use DateTime::Format::ISO8601; my $str = '2013-10-28T18:59:52.863Z'; my $dt = DateTime::Format::ISO8601->parse_datetime( $str ); my $now = DateTime->now; print +($dt->epoch() - $now->epoch())/3600, " hours\n";
      that is perfect!
Re: time difference calculation
by poolpi (Hermit) on Oct 28, 2013 at 12:41 UTC

    Hi,
    You might try DateTime module and one of the math method, for example :$dt->delta_ms( $datetime )

    #!/usr/bin/perl use strict; use warnings; use DateTime; my $now = DateTime->now; $now->iso8601; $now->subtract( seconds => 101 )->iso8601;


    Update: piece of code


    hth,
    PooLpi

    The stone that the builder refused, will always be the head cornerstone.
    [ Robert Nesta MARLEY ]
Re: time difference calculation
by marinersk (Priest) on Oct 28, 2013 at 13:37 UTC
    To harp on a very, very, very, very, very, very important point, always

    use strict;

    During development, always

    use warnings;

    Some will argue warningsshould stay in production code; I disagree but this is a local decision anyway.

Re: time difference calculation
by Anonymous Monk on Oct 28, 2013 at 12:56 UTC

    Date::Manip, Date::Calc, a search of http://search.cpan.org for the keyword, "date."

    Get to know CPAN very well. If you actually had to "start from scratch" to do most things, Perl would be nothing-special. Even if you can't easily install a module, you can always look at the source-code to it on the website to see precisely how the trick is done.

Re: time difference calculation
by kcott (Archbishop) on Oct 29, 2013 at 08:09 UTC

    You can do this with the builtin module Time::Piece:

    #!/usr/bin/env perl -l use strict; use warnings; use Time::Piece; my $start = '2013-10-28T18:59:52.863Z'; my ($whole, $ms) = $start =~ /([^.]+)(.*)Z$/; my $t1 = Time::Piece->strptime($whole => '%Y-%m-%dT%H:%M:%S'); my $t2 = gmtime; my $diff = $t2 - $t1; print 'Whole seconds diff: ', $diff; print 'Fractional seconds diff: ', $diff - $ms;

    Output (which is correct for my timezone):

    Whole seconds diff: 46979 Fractional seconds diff: 46978.137

    My UTC date/time:

    $ date -u Tue 29 Oct 2013 08:02:51 UTC

    Confirmation calculation:

    $ perl -E 'say(5*60*60 + 8 + 8*60*60 + 2*60 + 51)' 46979

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1059968]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2024-03-28 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found