[英]Perl one liner command to convert EST time to GMT time
I am trying to build a one line command in Perl to convert an EST datetime to GMT time and print the result. 我试图在Perl中构建一个单行命令,以将EST日期时间转换为GMT时间并打印结果。 I'm using the module "DateTime" to accomplish this.
我正在使用模块“ DateTime”来完成此任务。
The command I tried is: 我尝试的命令是:
perl -MDateTime=set_Time_Zone -e "DateTime->new(year=>2019,month=>5,day=>10,hour=>18,minute=>40,time_zone=>EST)" -e "$dt->set_time_zone(GMT)"
The error it's throwing is 它抛出的错误是
Can't call method "set_time_zone" on an undefined value at -e line 2.
无法在-e第2行的未定义值上调用方法“ set_time_zone”。
How to call a method and print the hour in GMT? 如何在GMT中调用方法并打印小时?
Four problems: 四个问题:
set_Time_Zone
. set_Time_Zone
。 $dt
without ever giving it a value. $dt
不会给出任何值。 (This is what produces the error message.) -0500
instead. -0500
。 If you really did mean Eastern Standard Time (despite it not being used on May 10th in places that observe DST), use the following: 如果您确实的意思是东部标准时间(尽管5月10日在遵守DST的地方未使用该时间),请使用以下方法:
perl -MDateTime -le'print DateTime->new(...,time_zone=>"-0500")->set_time_zone("UTC")'
It's far more likely that you meant Eastern Daylight Time. 您更可能是指东部夏令时间。 If so, use the following:
如果是这样,请使用以下命令:
perl -MDateTime -le'print DateTime->new(...,time_zone=>"-0400")->set_time_zone("UTC")'
Instead of using fixed offsets, you can specify the geographically-based names from the tz database. 您可以从tz数据库中指定基于地理位置的名称,而不是使用固定偏移量。 When using these, DateTime factors in whether DST is used on the date-time in question and uses the appropriate offset.
使用这些时,DateTime会影响是否在相关日期时间使用DST并使用适当的偏移量。 This is done as follows:
这样做如下:
perl -MDateTime -le'print DateTime->new(...,time_zone=>"America/New_York")->set_time_zone("UTC")'
Finally, the following is how you'd tell DateTime to use the local time zone: 最后,以下是您告诉DateTime使用本地时区的方法:
perl -MDateTime -le'print DateTime->new(...,time_zone=>"local")->set_time_zone("UTC")'
Notes: 笔记:
->strftime(...)
. ->strftime(...)
。 '
and "
. '
和"
。 Where do you think $dt
is set? 您认为
$dt
设置在哪里? And how is it gonna print without a print()
? 不用
print()
怎么print()
? Try: 尝试:
perl -MDateTime=set_Time_Zone -e "print(DateTime->new(year=>2019,month=>5,day=>10,hour=>18,minute=>40,time_zone=>EST)->set_time_zone(GMT));"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.