1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

perl で時刻計算 - Time::Piece編 - 指摘されたのを修正したバージョン

Last updated at Posted at 2014-04-15

前回記事に指摘があったので、朝一のスタバで自分なりにやってみました。

love_and_piece.pl
#!/usr/bin/env perl

use strict;
use warnings;

use Time::Piece;
use Time::Seconds;

# Time::Pieceオブジェクトの取得
print "Time::Piece ",$Time::Piece::VERSION,"\n";
my $t;
# $t = "2014-01-29"; # 入力パラメータとかで渡ってきたとする。
if ($t) {
    $t = localtime( Time::Piece->strptime($t, '%Y-%m-%d') );
}
else {
    $t = localtime; # 渡ってこなければ現在時刻
}
# 日付や時刻の情報の表示
print $t->ymd,"\n";
# $t += ONE_MONTH; # 一ヶ月後
$t = $t->add_months(1);
print $t->ymd,"\n";

結果

Time::Piece 1.20_01
2014-04-15
2014-05-15

入力パラメータのコメントアウトを外した場合

Time::Piece 1.20_01
2014-01-29
2014-03-01

修正点:
strptime の箇所をlocaltime で囲んだ。
ONE_MONTHで計算すると正確に一カ月にならないので、add_monthsメソッドを使用した。

参考:
Time::Piece とタイムゾーンの甘い罠
Time::Seconds::ONE_MONTH

やべー現場に遅れそうだ。。。大手町のスタバありがとう(笑)

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?