在Perl中获得UTC偏移量的最佳方法是什么? [英] What's the best way to get the UTC offset in Perl?
问题描述
我需要在跨平台(Windows和各种Unix版本)中获取Perl中当前时区的UTC偏移量。它应该符合以下格式:
$ b
zzzzzz,代表相对UTC的±hh:mm
blockquote>
看起来我应该可以通过
strftime()
来得到它,但是看起来并不一致
Unix:
输入:perl -MPOSIX -e打印strftime(\%z\,localtime());
输出:-0700
Windows:
输入:perl -MPOSIX -eprint strftime(\%z\,localtime());
输出:Mountain标准时间
Unix似乎给了我想要的东西或者至少有些东西关闭),Windows不是。我敢肯定,我可以用
Date :: Time
或类似的方式来完成,但我真的不想有任何依赖,我不能保证用户会由于我们广泛的安装基地。
我在这里错过了一些明显的东西?提前致谢。
解决方案
Time :: Local 使用Time :: Local;
@t = localtime(time);
$ gmt_offset_in_seconds = timegm(@t) - timelocal(@t);
I need to get the UTC offset of the current time zone in Perl in a cross platform (Windows and various flavors of Unix) way. It should meet this format:
zzzzzz, which represents ±hh:mm in relation to UTC
It looks like I should be able to get it via
strftime()
, but it doesn't appear to be consistent.Unix:
Input: perl -MPOSIX -e "print strftime(\"%z\", localtime());" Output: -0700
Windows:
Input: perl -MPOSIX -e "print strftime(\"%z\", localtime());" Output: Mountain Standard Time
While it appears that Unix is giving me what I want (or at least something close), Windows is not. I'm pretty sure I can do it with
Date::Time
or similar, but I'd really like to not have any dependencies that I can't guarantee a user will have due to our wide install base.Am I missing something obvious here? Thanks in advance.
解决方案
Time::Local
should do the trickuse Time::Local; @t = localtime(time); $gmt_offset_in_seconds = timegm(@t) - timelocal(@t);
这篇关于在Perl中获得UTC偏移量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文