Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

"Should be a simple thing" not going well for me

by TheBigAmbulance (Acolyte)
on Nov 02, 2011 at 20:48 UTC ( [id://935487]=perlquestion: print w/replies, xml ) Need Help??

TheBigAmbulance has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to manipulate a time stamp. The original time is UTC and is stored as 2011-07-24T17:46:33Z I need to convert this from UTC to the Olsen zone "America/North_Dakota/Center". I am using the perl module Date::Manip::Date. I have tried various scripts coming up with this mess
#!/usr/bin/perl -w use strict; use warnings; use Date::Manip::Date; my $date = "2011-07-24T17:46:33Z"; my $localzone = "America/North_Dakota/Center"; my $err = $date->convert([$localzone]); print $err;
and I am not getting anywhere to convert this. The desired output is 2011-07-24 12:46:33 PM CDT AND be daylight savings time sensative. Anyone have mercy on this poor soul?

Replies are listed 'Best First'.
Re: "Should be a simple thing" not going well for me
by BrowserUk (Patriarch) on Nov 02, 2011 at 21:48 UTC

    This is wrong:

    use Date::Manip::Date; my $date = "2011-07-24T17:46:33Z"; ... my $err = $date->convert([$localzone]);

    Before you can call methods, you first need to create an object:

    my $date = new Date::Manip::Date;

    Then you need to set the date & time value into that object using the appropriate method(s).

    Then you need to invoke the convert method passing a timezone in one of the formats documented within the modules POD.

    Try reading the POD.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: "Should be a simple thing" not going well for me
by Plankton (Vicar) on Nov 02, 2011 at 21:12 UTC
Re: "Should be a simple thing" not going well for me (optional)
by tye (Sage) on Nov 02, 2011 at 21:24 UTC

    Drop the square brackets. I believe they were shown just to indicate that passing in a timezone scalar is optional.

    - tye        

Re: "Should be a simple thing" not going well for me
by jandrew (Chaplain) on Nov 03, 2011 at 16:03 UTC

    I prefer DateTime::Format::DateManip over the module you have selected. This provides an object with all the exposed methods allowed for DateTime while still providing some parsing of date strings.

    #! C:/Perl/bin/perl use Modern::Perl; use DateTime::Format::DateManip; my $dt = DateTime::Format::DateManip->parse_datetime( "2011-07-24T17:46:33Z" ); say $dt; # Lock down the parsed time zone $dt->set_time_zone( 'UTC' ); say $dt; # change the time zone $dt->set_time_zone( "America/North_Dakota/Center" ); say $dt;

    Output

    # 2011-07-24T17:46:33 # 2011-07-24T17:46:33 # 2011-07-24T12:46:33
Re: "Should be a simple thing" not going well for me
by sundialsvc4 (Abbot) on Nov 03, 2011 at 14:11 UTC

    Try reading the POD.

    /me nods ...

    Unfortunately for the OP, BrowerUK’s admonition here is right on the mark.   Click on the link in this sentence to look at the search.cpan.org description of Date::Manip::Date, and you will see, at the very beginning of that page in the SYNOPSIS section, that the too-brief example code uses the module, then instantiates a new object.   Unfortunately the synopsis is not complete:   it does not tell you what to do next.

    Objects like these are designed to be instantiated, and then to provide the service of parseing a date string, thus obtaining its value (or throwing an error), and to answer questions about the value, and to print the formatted value.

    In a case like this, I find that sometimes the best source of “but how does it actually work?” information is to look at the test-library (a directory t) within the package subdirectory.   Here you will find code that is designed to work as well as code that is designed to fail in certain ways.   Perl runs all of these tests before installing the module permanently.   They can be an excellent source of information, usually written by the author himself.

    P.S.:   “Simple things” never are.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found