1

DateTimeモジュールを使用しています。しかし、それは間違った時間を提供しています。以下のコードを検討してください。

#!/usr/bin/perl -w
use strict;

use Time::localtime;
my $now = ctime();
print $now."\n";

print "------------------------------\n";

use DateTime;
my $dt = DateTime->now;
print $dt."\n";

そして、その出力は次のとおりです。

Wed Dec 26 22:11:52 2012
------------------------------
2012-12-27T06:11:52

ご覧のとおり、DateTime出力は8時間進んでいますが、これは間違っています。Linuxdateコマンドの出力は次のとおりです。

# date
Wed Dec 26 22:13:17 PST 2012

したがって、dateコマンドの出力は出力の出力と一致しtime::localtimeます。

DateTimeモジュールの使用でどこが間違っているのかを理解するのを手伝ってくれませんか。

-ありがとう。

アップデート:

hte CPANドキュメントから:

DateTime->now( ... )

This class method is equivalent to calling from_epoch() with the value returned from Perl's time() function. Just as with the new() method, it accepts "time_zone" and "locale" parameters.

By default, the returned object will be in the UTC time zone.

したがって、返される時刻はUTCであるようです。ただし、私がPSTにいるタイムゾーン。おそらくそれが私が別の時間を見る理由です。

4

2 に答える 2

7

ゾーン情報を渡しましたが、完全に機能するようになりました。

#!/usr/bin/perl -w
use strict;

use Time::localtime;
my $now = ctime();
print $now."\n";

print "------------------------------\n";

use DateTime;
my $dt = DateTime->now ( time_zone => 'America/Los_Angeles' );
print $dt."\n";

出力:

Wed Dec 26 22:28:44 2012
------------------------------
2012-12-26T22:28:44

東海岸の場合

my $dateF = DateTime->now( time_zone => 'America/New_York' )->ymd;
于 2012-12-27T06:29:00.983 に答える
1

ユーザーがマシンのタイムゾーンを知っている必要がある理由はありません。たとえ知っていたとしても、それをハードコーディングすることはお勧めできません。答えは

my $dt = DateTime->now(time_zone => 'local') # current time, local timezone
my $dt = DateTime->from_epoch(epoch=>time(), time_zone=>'local') # from timestamp

マシンのタイムゾーンに設定されます。

于 2021-05-09T10:55:04.797 に答える