DateTime::now() does not appear to be accurate for me. I used to methods to
prove this. The first method is creating a DateTime object using information
provided by localtime(). The second is using the DateTime::now() method. The
localtime method provides what I expected, the DateTime::now() method has an
incorrect hour for me.

 

Run at the same time (roughly within seconds at least), I get this result
when using the localtime approach:

 

my @now = localtime (time);

my $dt_now = DateTime->new ( year => $now[5]+1900, month => $now[4]+1, day
=> $now[3], hour => $now[2], minute => $now[1] );

 

%dt_now = bless( (

                   'local_rd_secs' => 29820,

                   'local_rd_days' => 732819,

                   'rd_nanosecs' => 0,

                   'locale' => bless( {

                                        'default_time_format_length' =>
'medium',

                                        'native_territory' => 'United
States',

                                        'native_language' => 'English',

                                        'native_complete_name' => 'English
United States',

                                        'en_language' => 'English',

                                        'id' => 'en_US',

                                        'default_date_format_length' =>
'medium',

                                        'en_complete_name' => 'English
United States',

                                        'en_territory' => 'United States'

                                      }, 'DateTime::Locale::en_US' ),

                   'local_c' => {

                                  'hour' => 8,

                                  'second' => 0,

                                  'month' => 5,

                                  'quarter' => 2,

                                  'day_of_year' => 143,

                                  'day_of_quarter' => 53,

                                  'minute' => 17,

                                  'day' => 23,

                                  'day_of_week' => 3,

                                  'year' => 2007

                                },

                   'utc_rd_secs' => 29820,

                   'formatter' => undef,

                   'tz' => bless( {

                                    'name' => 'floating',

                                    'offset' => 0

                                  }, 'DateTime::TimeZone::Floating' ),

                   'utc_year' => 2008,

                   'utc_rd_days' => 732819,

                   'offset_modifier' => 0

                 ), 'DateTime' );

 

Here is what I get when I use DateTime::now():

 

%dt_now = bless( (

                   'local_rd_secs' => 44142,

                   'local_rd_days' => 732819,

                   'rd_nanosecs' => 0,

                   'locale' => bless( {

                                        'default_time_format_length' =>
'medium',

                                        'native_territory' => 'United
States',

                                        'native_language' => 'English',

                                        'native_complete_name' => 'English
United States',

                                        'en_language' => 'English',

                                        'id' => 'en_US',

                                        'default_date_format_length' =>
'medium',

                                        'en_complete_name' => 'English
United States',

                                        'en_territory' => 'United States'

                                      }, 'DateTime::Locale::en_US' ),

                   'local_c' => {

                                  'hour' => 12,

                                  'second' => 42,

                                  'month' => 5,

                                  'quarter' => 2,

                                  'day_of_year' => 143,

                                  'day_of_quarter' => 53,

                                  'minute' => 15,

                                  'day' => 23,

                                  'day_of_week' => 3,

                                  'year' => 2007

                                },

                   'utc_rd_secs' => 44142,

                   'formatter' => undef,

                   'tz' => bless( {

                                    'name' => 'UTC'

                                  }, 'DateTime::TimeZone::UTC' ),

                   'utc_year' => 2008,

                   'utc_rd_days' => 732819,

                   'offset_modifier' => 0

                 ), 'DateTime' );

 

Shouldn't the 2 be identical? The localtime method is accurate for me (it is
roughly 8am right now here). The DateTime::now() method has the hour as 12.

 

Some miscellaneous information:

 

1.       Running Perl 5.8.5

2.       DateTime.pm version 0.2901

3.       Centos w/ kernel 2.6.9.

 

Reply via email to