Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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-test.pl 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-08-02 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls