Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

SOLVED: DateTime question

by antigua (Novice)
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.

Replies are listed 'Best First'.
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-09-22 11:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (261 votes). Check out past polls.