in reply to Converting dates with Date:Format

For completeness: DateTime:

(Note that your input has no timezone info, so you can't use %Z to print the timezone with accuracy. Also note that you have two spaces in your input, so the solution reflects that.)

use DateTime::Format::Strptime; my $formatter = DateTime::Format::Strptime->new(pattern => "%F %T"); my $dt_obj = $formatter->parse_datetime("2008-11-03 19:03:44"); my $datetime = $dt_obj->strftime("%a, %d %b %Y %T EST");
Or, more succinctly as a one-liner:
perl -ML -E'say DateTime::Format::Strptime->new(pattern => "%F %T")-> parse_datetime("2008-11-03 19:03:44")->strftime("%a, +%d %b %Y %T EST"); '
Output:
Mon, 03 Nov 2008 19:03:44 EST

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Converting dates with Date:Format
by haukex (Archbishop) on Jan 10, 2017 at 14:32 UTC

    Hi 1nickt,

    my $datetime = $dt_obj->strftime("%a, %d %b %Y %T EST");

    I wouldn't hardcode the timezone into the format string like that, since technically the object's time zone is in DateTime's special "floating" zone, and that's what'd be output if you use %Z (which I personally would still do). To assume a time zone for the input data instead use time_zone=>'EST' in the DateTime::Format::Strptime constructor:

    use DateTime::Format::Strptime; my $formatter = DateTime::Format::Strptime->new(pattern => "%F %T",ti +me_zone=>'EST'); my $dt_obj = $formatter->parse_datetime("2008-11-03 19:03:44"); print $dt_obj->strftime("%a, %d %b %Y %T %Z"), "\n"; # to convert to a different zone: $dt_obj->set_time_zone('America/Chicago'); print $dt_obj->strftime("%a, %d %b %Y %T %Z"), "\n"; __END__ Mon, 03 Nov 2008 19:03:44 EST Mon, 03 Nov 2008 18:03:44 CST

    Regards,
    -- Hauke D

      Hi haukex,

      Your suggestion is quite correct and a much more complete treatment of the problem (as is your way!).

      My choice of using the string was intended to show precisely that since the provided input data lacked any TZ (and %Z returns the literal string "floating" in that case), no meaningful TZ info could be provided by the DateTime object, so you'd have to just add it as a string. Of course I didn't explain any of that :(

      If, as is I guess most likely, the OP has datetimes lacking a TZ, and is himself located in Eastern Time Zone, his TZ will shift during the year, so while it may be EST today it could be EDT in six months. I think the best of all in his case may be to add the time_zone param in the constructor as you say, but provide the value "local".

      nick@vagrant$ perl -Mstrict -Mwarnings -ML -E' say DateTime::Format::Strptime->new(pattern => "%F %T", time_zone => +"local")-> parse_datetime("2008-11-03 19:03:44")-> strftime("%a, %d %b %Y %T %Z"); ' Mon, 03 Nov 2008 19:03:44 EST
      (now you know where that vagrant lives!)


      The way forward always starts with a minimal test.

        Hi 1nickt,

        I think the best of all in his case may be to add the time_zone param in the constructor as you say, but provide the value "local".

        That's an excellent point!

        Just to expand on that, it's also possible to say time_zone=>'America/New_York' - see the output of perl -MDateTime::TimeZone -le 'print for DateTime::TimeZone->all_names' .

        Regards,
        -- Hauke D