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

I know this is going to sound stupid, but I can't seem to get DateTime to give me the local time.
my $now = DateTime->now() ; say $now->mdy ; say $now->hour ; ------------------ 03-31-2023 12
That looks like GMT. I've tried "DateTime->now(timezone=>"EST5EDT") and it blows up with errors. I know I can brute force it, but it really feels like I'm missing something obvious in not being able to get it to give me "now" in my time zone.

Replies are listed 'Best First'.
Re: Baffled by DateTime
by haukex (Archbishop) on Mar 31, 2023 at 13:04 UTC
    I've tried "DateTime->now(timezone=>"EST5EDT") and it blows up with errors.

    Probably because the argument is called time_zone, not timezone?

    $ perl -wMstrict -MDateTime -le 'print DateTime->now->strftime("%Y-%m- +%d %H:%M:%S %z")' 2023-03-31 13:08:16 +0000 $ perl -wMstrict -MDateTime -le 'print DateTime->now(time_zone=>"local +")->strftime("%Y-%m-%d %H:%M:%S %z")' 2023-03-31 15:08:23 +0200 $ perl -wMstrict -MDateTime -le 'print DateTime->now(time_zone=>"EST5E +DT")->strftime("%Y-%m-%d %H:%M:%S %z")' 2023-03-31 09:08:29 -0400 $ perl -wMstrict -MDateTime -le 'print DateTime->now(time_zone=>"Ameri +ca/New_York")->strftime("%Y-%m-%d %H:%M:%S %z")' 2023-03-31 09:13:15 -0400 $ perl -wMstrict -MDateTime -le 'print DateTime->now(timezone=>"EST5ED +T")->strftime("%Y-%m-%d %H:%M:%S %z")' Found extra parameters passed to _check_named_from_epoch_params: [time +zone]

    Update: In regards to local, see Determining the Local Time Zone Can Be Slow. Also, I would recommend to stay away from ambiguous time zone names like CST ("Central Standard Time", "China Standard Time", "Cuba Standard Time", ...) - correspondingly, I updated my examples above to use %z instead of %Z. Update 2: Added the America/New_York example as an alternative to EST5EDT, as noted on Wikipedia.

      A big DUH. Thanks for telling me the obvious. it is now perfectly well behaved
      use DateTime; my $dt = DateTime->now(time_zone => "EST5EDT") ; say $dt->hour ; 9
      which is the right time {it's now 9:32}. Thanks!
        A small followon. With:
        perl -E "use DateTime ;my $now = DateTime->now(time_zone => \"EST5EDT\ +") ; my $day = $now->local_day_of_week ; say \"day of the week: $day\ +" ; " --------------------- day of the week: 6
        The docs say: "Returns the day of the week as a number, from 1..7, with 1 being Monday and 7 being Sunday." But today is Friday...looks like it is "with 1 being Sunday" This is not a big deal, but am I missing something?
Re: Baffled by DateTime
by hippo (Archbishop) on Mar 31, 2023 at 13:44 UTC
    I've tried "DateTime->now(timezone=>"EST5EDT") and it blows up with errors.

    As ever, those errors (which you've decided to omit from your post for some unknown reason) hold the clue:

    $ perl -MDateTime -e 'print DateTime->now(timezone=>"EST5EDT")->hour' Found extra parameters passed to _check_named_from_epoch_params: [time +zone] Trace begun at (eval 241) line 155 DateTime::_check_named_from_epoch_params('epoch', 1680269935, 'timezon +e', 'EST5EDT') called at /usr/local/lib64/perl5/DateTime.pm line 493 DateTime::from_epoch('DateTime', 'epoch', 1680269935, 'timezone', 'EST +5EDT') called at /usr/local/lib64/perl5/DateTime.pm line 545 DateTime::now('DateTime', 'timezone', 'EST5EDT') called at -e line 1

    Found extra parameters passed to _check_named_from_epoch_params: [timezone] tells us that it isn't expecting timezone there, so you can go back to the doc and discover that what you actually need is time_zone instead. See the difference:

    $ perl -MDateTime -e 'print DateTime->now(time_zone=>"EST5EDT")->hour' 9

    Always include the full text of the error message in your post. Even if you don't understand it, someone else probably does.


    🦛

Re: Baffled by DateTime
by ikegami (Patriarch) on Mar 31, 2023 at 14:17 UTC

    UTC is indeed the default time zone.

    $ perl -Mv5.14 -e'use DateTime; say DateTime->now->time_zone->name' UTC

    It's also in the documentation.

    For local time, use

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

    All of these could be considered EST5EDT:

    Name (bold) and AliasesLocation observed
    America/New_York
    US/Eastern
    US, most areas of many states
    America/DetroitUS, MI, most areas
    America/Indiana/Indianapolis
    America/Fort_Wayne
    America/Indianapolis
    US, IN, most areas
    America/Indiana/MarengoUS, IN, Crawford
    America/Indiana/PetersburgUS, IN, Pike
    America/Indiana/VevayUS, IN, Switzerland
    America/Indiana/VincennesUS, IN, Da/Du/K/Mn
    America/Indiana/WinamacUS, IN, Pulaski
    America/Kentucky/Louisville
    America/Louisville
    US, KY, Louisville area
    America/Kentucky/MonticelloUS, KY, Wayne
    America/Toronto
    America/Montreal
    America/Thunder_Bay
    America/Nipigon
    America/Nassau
    CA, ON
    CA, QC
    BS
    America/Iqaluit
    America/Pangnirtung
    CA, NU
    America/Grand_TurkTC
    America/Port-au-PrinceHT

    (WTF Indiana? That's not even including the parts of Indiana that couldn't be considered EST5EDT!)

    Assuming you mean the US Eastern Time, use the following instead

    my $now = DateTime->now( time_zone => 'America/New_York' );

    Or if you prefer the alias,

    my $now = DateTime->now( time_zone => 'US/Eastern' );
Re: Baffled by DateTime
by Corion (Patriarch) on Mar 31, 2023 at 13:04 UTC

    What error do you get? Also, are you sure that EST5EDT makes sense as timezone? Shouldn't it be EST and DateTime figures out that it is currently DST there?

      It is accepted (once you fix the parameter name), but that doesn't mean it makes sense. It's not a well defined term. If you're lucky, you want the US time zone and it will be an alias for America/New_York. So why not avoid chance and use America/New_York if that's what you want. Or did you want America/Toronto...

        but that doesn't mean it makes sense

        I am rather glad that I live close to the Greenwich Meridian - I least I (almost always) only have Daylight Saving Time to worry about!