确定另一个时区的本地时间

发布于 2024-10-11 11:34:33 字数 601 浏览 8 评论 0原文

如何使用在美国服务器上执行的 PERL 脚本确定各个国家/地区的当前日期和时间?例如,getDTnow() 应确定服务器上的当前日期和时间,并使用它返回各个国家/地区的日期和时间。

PS:如果仅使用内置函数即可完成此操作,而不需要任何外部模块,那就太好了。


结论:日期数学[在这里使用脏话]复杂且容易出错。 IRC、群组和网络其他部分上的其他 Perl 专家证实了 Ether 一直建议我的内容 - 使用 日期时间DVK 的解决方案对于那些不介意弄乱 perl 环境的人来说也非常简洁。 (注意:尽管在 Windows 上,Time::Piece 的警告部分文档说,“在 Win32 上的线程中设置 $ENV{TZ} 时应该小心”)。

How can I determine the current date and time of various countries using a PERL script that executes on a server in the US? For example, getDTnow() should determine the current date and time on the server and use that to return the date and time of various countries.

P.S: It would be great if this can be done using only the built-in functions, without any external modules.


Conclusion: Date maths is [use swear word here] complicated and easy to get wrong. Other perl gurus on IRC, groups and other parts of the net confirmed what Ether had been advicing me - use DateTime. DVK's solution is also pretty neat for those of you who don't mind messing with the perl environment. (Note: Though on windows, the caveats section of the Time::Piece docs says one should be careful while 'Setting $ENV{TZ} in Threads on Win32').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪梦之恋 2024-10-18 11:34:33

DateTime 是一个很棒的库,它可以使用标准时区来完成您想要的一切以及更多:

use DateTime;

# returns local time in Italy
my $dt = DateTime->now(time_zone => 'Europe/Rome');

# prints time in desired format
print "The current date and time in Italy is: ", $dt->strftime('%Y-%m-%d %T');

DateTime is a wonderful library that can use standard timezones to do everything you desire and more:

use DateTime;

# returns local time in Italy
my $dt = DateTime->now(time_zone => 'Europe/Rome');

# prints time in desired format
print "The current date and time in Italy is: ", $dt->strftime('%Y-%m-%d %T');

您可以通过 TZ 环境变量控制本地时间返回哪个时区:

local $ENV{TZ} = ":/usr/share/lib/zoneinfo/Asia/Tokyo";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); 
print "$sec,$min,$hour,$mday,$mon,$year,$wday,$yday\n"'
# Prints 40,58,4,12,0,111,3,11

local $ENV{TZ} = ":/usr/share/lib/zoneinfo/Europe/London";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); 
print "$sec,$min,$hour,$mday,$mon,$year,$wday,$yday\n"'
# Prints 41,58,19,11,0,111,2,10

不幸的是,上面的路径在不同的 Unix 上有所不同(Solaris 上的 /usr/share/lib/zoneinfo,< Linux 上的代码>/usr/share/zoneinfo)。由于似乎没有其他变体,一个稍微可移植的版本将检查这两个目录中的哪一个存在并使用它 - 但这显然只适用于 Solaris 和 Linux,并且可能适用于其他 UNIX。不知道 Windows/MacOS/什么的。

TZ 的有效位置可以在这里找到:http://www.timezoneconverter.com/cgi- bin/tzref.tzc (但并非所有这些都一定在您的系统上可用 - 检查上面的目录)。

有关 TZ 数据库的更多信息,请参阅 http://en.wikipedia.org/wiki/Tz_database

You can control which timezone localtime returns in via TZ environmental variable:

local $ENV{TZ} = ":/usr/share/lib/zoneinfo/Asia/Tokyo";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); 
print "$sec,$min,$hour,$mday,$mon,$year,$wday,$yday\n"'
# Prints 40,58,4,12,0,111,3,11

local $ENV{TZ} = ":/usr/share/lib/zoneinfo/Europe/London";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); 
print "$sec,$min,$hour,$mday,$mon,$year,$wday,$yday\n"'
# Prints 41,58,19,11,0,111,2,10

Unfortunately, the path above is different on different Unixes (/usr/share/lib/zoneinfo on Solaris, /usr/share/zoneinfo on Linux). Since there appear to be no other variations, a slightly portable version would check which of the 2 directories exists and use that - but this obviously only works on Solaris and Linux and may be other unixes. No idea about Windows/MacOS/whatnot.

Valid locations for TZ can be found here: http://www.timezoneconverter.com/cgi-bin/tzref.tzc (but not all of them would necessarily be available on your system - check the above directory).

Please see http://en.wikipedia.org/wiki/Tz_database for more info on TZ database.

孤单情人 2024-10-18 11:34:33

您始终可以将时区的变化存储在哈希中,其中键是时区,值是对当前时间的调整。那么当您传递当前时间时,它应该返回该区域的当地时间。

You could always store the variation from your timezone in a hash where the key is the timezone and the value is the adjustment from the current time. then when you pass the current time it should return the local time for that zone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文