Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

DateTime normalize

by gwhite (Friar)
on Aug 25, 2009 at 15:30 UTC ( #791107=perlquestion: print w/replies, xml ) Need Help??
gwhite has asked for the wisdom of the Perl Monks concerning the following question:

This code:
my( $year, $month, $day, $hour, $minute, $seconds ) = (localtime)[5,4, +3,2,1,0]; $year += 1900; $month += 1; my $source = DateTime->new(year => $year, month => $month, day => $day, hour => $hour, minute => $minute, second => $seconds, nanosecond => 1, time_zone => 'America/Chicago'); my $result = $source->clone() ->set_time_zone('Australia/Sydney'); print $result->now();

produces this error: Program fragment delivered error ``Can't locate object method "_normalize_nanoseconds" via package "2009-08-26T00:59:20" at /usr/local/lib/perl5/site_perl/5.8.8/mach/ line 207.''

sub _normalize_nanoseconds is in the file. Any suggestions on what I am doing wrong?


Replies are listed 'Best First'.
Re: DateTime normalize
by toolic (Bishop) on Aug 25, 2009 at 16:08 UTC
    I also get the same error that you get when I run your code.

    I have no experience with DateTime, but looking at its POD, I notice that now() is always accessed as:


    and never the way you are trying to use it:


    Perhaps now() (whatever it does) was never meant to be called that way? There seem to be no bug reports for this.

      Correct. now creates a new object and initialises it to the current time.

      my $dt = DateTime->now(); my $dt = DateTime->now( time_zone => 'local' );

      The OP appears to be trying to format the timestamp into a string. For this, he has a number of options.

      • He could construct the formatted timestamp himself. There are getters to get each individual components, there are getters that return the date in a couple of simple formats, and there's a getter that returns the time.

      • He could use $dt->strftime, which returns the contained date and/or time in just about any format you can imagine.

      • There's also the option of using a (parsing and) formatting helper module. These are typically named DateTime::Format::<format> (e.g. DateTime::Format::Atom).

Re: DateTime normalize
by Herkum (Parson) on Aug 26, 2009 at 03:26 UTC

    now() is a class method not a object method. When you call it with,


    It assumes $result is the package and looks at it as a string. A DateTime object which is used in a string context returns the date in a iso8601 format(by default).

    If you use it as a class method you should not see an error.

    my $result = DateTime->now;
Re: DateTime normalize
by perlkiller (Acolyte) on Aug 26, 2009 at 04:03 UTC

    I suggest that you use POSIX::strftime function which uses less coding.

    my $now = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);
Re: DateTime normalize
by astroboy (Chaplain) on Aug 26, 2009 at 05:00 UTC
    I don't understand why you set $source the way you do. Is this what you want?
    my $source = DateTime->now(time_zone => 'America/Chicago');

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://791107]
Approved by broomduster
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-05-20 14:03 GMT
Find Nodes?
    Voting Booth?