Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

SOLVED: DateTime question

by antigua (Initiate)
on Oct 05, 2009 at 19:01 UTC ( #799319=perlquestion: print w/ replies, xml ) Need Help??
antigua has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Having some trouble with Perl's DateTime package -- essentially trying to periodically query a MySQL db for the nearest time stamp and compare it with the current time, to see how much time there is before the next "event". The correct record is chosen via the SQL query, so no worries there. Here's the date comparison code:

my $now = DateTime->now(time_zone => 'America/New_York'); my $then = DateTime::Format::MySQL->parse_datetime($last); print "NOW: "; print $now; print "\n"; print "THEN: "; print $then; print "\n"; my $diff = $then->delta_ms($now); print "\n"; print $diff->in_units('minutes'); print "\n";

...where the $last var corresponds to the MySQL record's datetime format. The conversion to Perl's DateTime object seems to go ok, as can be seen by this output..

[root@localhost directory]# ./ SQL: 2009-10-06 18:00:00 NOW: 2009-10-05T14:48:09 THEN: 2009-10-06T18:00:00 191

But the problem is that I want to compare the diff in minutes, and as can be seen above, the day is ignored in the comparison. yes, there are more than just minutes in the diff, but between the delta_ms and in_minutes calls the larger time blocks should be converted to minutes. Any idea what's going on here, I'm banging my head against the keyboard... ...thanks.

Comment on SOLVED: DateTime question
Select or Download Code
Re: DateTime question
by ikegami (Pope) on Oct 05, 2009 at 19:16 UTC

    It's related to MySQL's timestamp's time zone being unknown ('floating').

    use strict; use warnings; use DateTime qw( ); use DateTime::Format::MySQL qw( ); my $now = DateTime->new( year => 2009, month => 10, day => 5, hour => 14, minute => 48, second => 9, time_zone => 'America/New_York', ); my $then = DateTime::Format::MySQL->parse_datetime( '2009-10-06 18:00:00', ); for (1..2) { my $diff = $then->delta_ms($now); print( $diff->in_units('minutes'), "\n" ); $then->set_time_zone('America/New_York'); }
    191 1631

    I don't know why it doesn't give an error.

      Thanks, thanks. My headache went away a little bit. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://799319]
Approved by AnomalousMonk
Front-paged by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-12-27 05:29 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (176 votes), past polls