简体   繁体   English

Perl一线命令将EST时间转换为GMT时间

[英]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: 四个问题:

  1. DateTime doesn't export set_Time_Zone . DateTime不导出set_Time_Zone
  2. You use $dt without ever giving it a value. 您在使用$dt不会给出任何值。 (This is what produces the error message.) (这就是产生错误消息的原因。)
  3. "EST" is not a standard time zone name, and should be avoided. “ EST”不是标准的时区名称,应避免使用。 It could refer to Eastern Standard Time (UTC-0500), a time zone that's mostly unused on May 10th. 它可能指的是东部标准时间(UTC-0500),该时区在5月10日大部分未使用 If so, specify -0500 instead. 如果是这样,请改为指定-0500
  4. You don't print anything. 您什么都不打印。

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: 笔记:

  • If you want a different output format, you can use ->strftime(...) . 如果需要其他输出格式,可以使用->strftime(...)
  • If using the Windows command shell, swap ' and " . 如果使用Windows命令外壳程序,则交换'"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM