[Perl]localtimeで日本時間にならない場合の対処

Posted on 28th 5月 2009 in Perl, プログラム

Perlのlocaltime関数を使って、以下のようなコードで日本時間を出力しようとしても、何らかの原因(海外から取り寄せたサーバでタイムゾーンが違うとか)で正しく出力されない場合がある。

▼環境によってはうまく動かないコード

1
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time + 32400);

この場合、gmtime関数を使えば、必ずグリニッジ標準時を返すので以下のコードで環境に依存せずに日本時間が出力可能になる。

▼環境に依存しないコード

1
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time + 32400);

▼動作確認用コード

1
2
3
4
5
6
7
8
9
use strict;
use warnings;
use Perl6::Say;
 
say gmtime time;
say gmtime;
say gmtime time + 32400;
say localtime time;
say localtime;
comments: 0 » tags: , このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>