Perl: DateTime で日付時間を確認

DateTime-1.07
http://search.cpan.org/~drolsky/DateTime-1.07/

 

導入

$ cpanm install DateTime

 

確認

$ perldoc DateTime

 

今の日付を取得

Perl: date.pl

#!/usr/bin/perl
use DateTime;

my $dt = DateTime->now( time_zone => 'Asia/Tokyo' );
print $dt;
$ ./date.pl
2014-02-24T08:39:10

 

その他 DateTime 操作

#!/usr/bin/perl
use DateTime;
use Data::Dumper;

#######################################
print "■現在時刻\n";
my $dt = DateTime->now( time_zone => 'Asia/Tokyo' );

print $dt."\n";

print $dt->ymd."\n";
print $dt->ymd('/')."\n";

print $dt->hms(':')."\n";

# エポック秒(UNIX時刻からの経過時間)
print $dt->epoch."\n";

#######################################
print "■日付の差分を表示\n";

my $dt1 = DateTime->new(
    year => 2014,
    month => 4,
    day => 29,
);
my $dt2 = DateTime->new(
    year => 2012,
    month => 4,
    day => 12,
);
my $elapsed = $dt1-$dt2;
print Dumper $elapsed;
print "\n";

#######################################
print "■一週間後の日付を取得\n";

my $elapse = DateTime::Duration->new(days => 7);
my $dt3 = $dt + $elapse;
print $dt3->ymd('/')."\n";
■現在時刻
2014-04-29T11:57:28
2014-04-29
2014/04/29
11:57:28
1398740248
■日付の差分を表示
$VAR1 = bless( {
                 'nanoseconds' => 0,
                 'minutes' => 0,
                 'seconds' => 0,
                 'months' => 24,
                 'days' => 17,
                 'end_of_month' => 'wrap'
               }, 'DateTime::Duration' );

■一週間後の日付を取得
2014/05/06