in reply to Baffled by DateTime

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.

Replies are listed 'Best First'.
Re^2: Baffled by DateTime
by BernieC (Pilgrim) on Mar 31, 2023 at 13:33 UTC
    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?
        The docs say: "Returns the day of the week as a number, from 1..7, with 1 being Monday and 7 being Sunday."

        That's what the docs for ->day_of_week say. The docs for ->local_day_of_week say "The day corresponding to 1 will vary based on the locale." I suspect you want to call ->day_of_week instead, which will return 5 for the current date as expected.

        The docs say: "Returns the day of the week as a number, from 1..7, with 1 being Monday and 7 being Sunday."

        Actually, they don't. They say:

        Returns the day of the week as a number, from 1..7. The day corresponding to 1 will vary based on the locale. See the "Locales" section for more details.

        Unless you are using an old version, in which case surely you would have mentioned this. ;-)


        🦛