如何从 localtime() 中提取时区?
我正在使用以下代码来打印当前时间。
use Getopt::Long;
use Time::Local;
sub gettime
{
my $t = time();
my ($sec,$mn,$hr,$mday,$mon,$yr,@left, $dstr);
($sec,$mn,$hr,$mday,$mon,$yr,@left) = localtime($t);
$yr = $yr-100+2000;
$mon += 1;
$dstr = sprintf "%02d:%02d:%02d (%02d-%02d-%04d)", $hr, $mn, $sec, $mon, $mday, $yr;
print $dstr;
}
gettime();
我可以使用以下方法设置时区:
local $ENV{TZ} = ":/usr/share/lib/zoneinfo/America/Los_Angeles";
How can I extract the timezone from localtime()
?
I'm using the following code to print the current time.
use Getopt::Long;
use Time::Local;
sub gettime
{
my $t = time();
my ($sec,$mn,$hr,$mday,$mon,$yr,@left, $dstr);
($sec,$mn,$hr,$mday,$mon,$yr,@left) = localtime($t);
$yr = $yr-100+2000;
$mon += 1;
$dstr = sprintf "%02d:%02d:%02d (%02d-%02d-%04d)", $hr, $mn, $sec, $mon, $mday, $yr;
print $dstr;
}
gettime();
I can set the timezone using:
local $ENV{TZ} = ":/usr/share/lib/zoneinfo/America/Los_Angeles";
How can I extract the timezone from localtime()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
strftime()
:或者,计算
localtime()
和gmtime()
之间的差异。You could use
strftime()
:Or, calculate the difference between
localtime()
andgmtime()
.您可以获取时区以及与 UTC 的偏移量:
You can have the timezone as well as the offset from UTC:
这是计算当前时区的纯 Perl 方法,无需使用外部模块:
Here is pure Perl approach to calculate current timezone without using external modules:
POSIX 模块中的 tzset() 和 tzname() 函数是一个容易记住的答案。
tzset()
andtzname()
functions from POSIX module can be a easily remembered answer.