LoginSignup
3
3

More than 5 years have passed since last update.

Perlで時刻計算を行う - Time::Piece 編 -

Last updated at Posted at 2014-04-14
Piece.pl
#!/usr/bin/env perl

use strict;
use warnings;

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

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

結果
2012-01-15
2012-02-14

結果(localtimeが渡ったとき)
2014-04-14
2014-05-15

もっと良い方法があれば教えてください。

以上

3
3
8

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
3
3