Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

DateTime normalize

by gwhite (Friar)
on Aug 25, 2009 at 15:30 UTC ( [id://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/DateTime.pm line 207.''

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

g_White

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:

    DateTime->now();

    and never the way you are trying to use it:

    $dt->now();

    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,

    $result->now()

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

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://791107]
Approved by broomduster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found