在 Perl 中获取 UTC 偏移量的最佳方法是什么? [英] What's the best way to get the UTC offset in Perl?

查看:18
本文介绍了在 Perl 中获取 UTC 偏移量的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以跨平台(Windows 和各种风格的 Unix)的方式在 Perl 中获取当前时区的 UTC 偏移量.它应该符合这种格式:

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,表示相对于 UTC 的 ±hh:mm

zzzzzz, which represents ±hh:mm in relation to UTC

看起来我应该可以通过 strftime() 获得它,但它似乎不一致.

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

窗户:

Input: perl -MPOSIX -e "print strftime("%z", localtime());"
Output: Mountain Standard Time

虽然 Unix 似乎给了我我想要的东西(或者至少是接近的东西),但 Windows 却没有.我很确定我可以使用 Date::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 应该可以解决问题

use Time::Local;
@t = localtime(time);
$gmt_offset_in_seconds = timegm(@t) - timelocal(@t);

这篇关于在 Perl 中获取 UTC 偏移量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆