如何从 localtime() 中提取时区?

发布于 2024-12-21 00:30:51 字数 597 浏览 6 评论 0原文

我正在使用以下代码来打印当前时间。

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 技术交流群。

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

发布评论

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

评论(4

左岸枫 2024-12-28 00:30:51

您可以使用 strftime()

use POSIX;
$tz = strftime("%Z", localtime());

或者,计算 localtime()gmtime() 之间的差异。

You could use strftime():

use POSIX;
$tz = strftime("%Z", localtime());

Or, calculate the difference between localtime() and gmtime().

走过海棠暮 2024-12-28 00:30:51

您可以获取时区以及与 UTC 的偏移量:

perl -MPOSIX -e 'print strftime "%Z (%z)\n",localtime'

You can have the timezone as well as the offset from UTC:

perl -MPOSIX -e 'print strftime "%Z (%z)\n",localtime'
骄兵必败 2024-12-28 00:30:51

这是计算当前时区的纯 Perl 方法,无需使用外部模块:

sub get_timezone {

  # Get the current local time
  my @localtime = localtime();

  # Get the current GMT time
  my @gmtime = gmtime();

  # Calculate the time difference in hours
  my $timezone = ($localtime[2] - $gmtime[2]);

  # If the day is different, adjust the timezone
  if ($localtime[3] != $gmtime[3]) {
    if ($localtime[3] < $gmtime[3]) {
        $timezone += 24;
    } else {
        $timezone -= 24;
    }
  }

  return $timezone; # e.g. -3

} # /get_timezone

print "Timezone: GMT " . &get_timezone() . "\n";

Here is pure Perl approach to calculate current timezone without using external modules:

sub get_timezone {

  # Get the current local time
  my @localtime = localtime();

  # Get the current GMT time
  my @gmtime = gmtime();

  # Calculate the time difference in hours
  my $timezone = ($localtime[2] - $gmtime[2]);

  # If the day is different, adjust the timezone
  if ($localtime[3] != $gmtime[3]) {
    if ($localtime[3] < $gmtime[3]) {
        $timezone += 24;
    } else {
        $timezone -= 24;
    }
  }

  return $timezone; # e.g. -3

} # /get_timezone

print "Timezone: GMT " . &get_timezone() . "\n";
我家小可爱 2024-12-28 00:30:51
$ perl -MPOSIX -le 'tzset; print for tzname'
CST
CDT

POSIX 模块中的 tzset() 和 tzname() 函数是一个容易记住的答案。

$ perl -MPOSIX -le 'tzset; print for tzname'
CST
CDT

tzset() and tzname() functions from POSIX module can be a easily remembered answer.

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