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. | [reply] [d/l] [select] |
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!
| [reply] [d/l] |
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? | [reply] [d/l] |
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.
| [reply] [d/l] [select] |
$ 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 Aliases | Location observed
|
|---|
America/New_York US/Eastern | US, most areas of many states
| | America/Detroit | US, MI, most areas
| America/Indiana/Indianapolis America/Fort_Wayne America/Indianapolis | US, IN, most areas
| | America/Indiana/Marengo | US, IN, Crawford
| | America/Indiana/Petersburg | US, IN, Pike
| | America/Indiana/Vevay | US, IN, Switzerland
| | America/Indiana/Vincennes | US, IN, Da/Du/K/Mn
| | America/Indiana/Winamac | US, IN, Pulaski
| America/Kentucky/Louisville America/Louisville | US, KY, Louisville area
| | America/Kentucky/Monticello | US, 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_Turk | TC
| | America/Port-au-Prince | HT
|
(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' );
| [reply] [d/l] [select] |
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?
| [reply] [d/l] [select] |
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...
| [reply] [d/l] [select] |
| [reply] |