Perl日期时间处理示例
2013-03-09 15:19:21 阿炯

本站赞助商链接,请多关照。 ------------------------------
Getting current epoch time in Perl-取得当前时戳
Time returns an integer with the current epoch:
time

------------------------------
Converting from epoch to normal date in Perl-将纪元时戳转为日常格式日期时间
Using the internal localtime or gmtime functions, localtime and gmtime return an array:

my $time = time;# or any other epoch timestamp
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my ($sec, $min, $hour, $day,$month,$year) = (localtime($time))[0,1,2,3,4,5];

# You can use 'gmtime' for GMT/UTC dates instead of 'localtime'
print "Unix time ".$time." converts to ".$months[$month]." ".$day.", ".($year+1900);
print " ".$hour.":".$min.":".$sec."\n";

But you can also use the scalar function to display your date with far less code:
print scalar localtime(946684800); returns Sat Jan  1 08:00:00 2000 (in china CST timezone).

Here is a simple version which uses two built-in perl functions:
my $timeLabel = sprintf "%02d:%02d:%02d", (localtime($adjSendTime/1000))[2,1,0];

For anything much more complicated than this, I would likely use an external module, but I just don't see much point in doing so for this particular need.

perl -MDateTime -le 'print DateTime->from_epoch(epoch=>time,time_zone=>"Asia/Shanghai")->hms'

use DateTime;

my $epoch=1362562931.352329;
my $dt = DateTime->from_epoch(epoch=>$epoch,time_zone => "Asia/Shanghai");
print $dt->ymd . " " . $dt->hms . "\n";

The output is:
2013-03-06 17:42:11

This can be achived simply using core tools: POSIX's strftime in combination with localtime.

use POSIX qw( strftime );
say strftime('%H:%M:%S', localtime( $mstime/1000 ));

use DateTime qw( );
my $dt = DateTime->from_epoch(epoch => $mstime/1000, time_zone => 'local');
say $dt->strftime('%H:%M:%S');

localtime array slice problem
perl -e "print @{localtime}[1,3];"
perl -e "sub m {@q=(1,2,3); return \@q;} print @{&m}[0,1];"
    print @{[localtime]}[1,3]; or print ((localtime)[1,3]);

------------------------------
Using the DateTime module:
use DateTime;
$dt = DateTime->new( year => 1974, month => 11, day => 30, hour => 13, minute => 30,second => 0, nanosecond => 500000000, time_zone => 'Asia/Taipei' );
$epoch_time = $dt->epoch;

use DateTime;
$dt = DateTime->from_epoch( epoch => $epoch );
$year = $dt->year;
$month = $dt->month; # 1-12 - you can also use '$dt->mon'
$day = $dt->day; # 1-31 - also 'day_of_month', 'mday'
$dow = $dt->day_of_week; # 1-7 (Monday is 1) - also 'dow', 'wday'
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59 - also 'min'
$second = $dt->second; # 0-61 (leap seconds!) - also 'sec'
$doy = $dt->day_of_year; # 1-366 (leap years) - also 'doy'
$doq = $dt->day_of_quarter; # 1.. - also 'doq'
$qtr = $dt->quarter; # 1-4
$ymd = $dt->ymd; # 1974-11-30
$ymd = $dt->ymd('/'); # 1974/11/30 - also 'date'
$hms = $dt->hms; # 13:30:00
$hms = $dt->hms('|'); # 13|30|00 - also 'time'

注意:DateTime模块功能丰富,但计算效率却不高,当频繁调用时,尤其是通过epoch_time计算日期时间时比内置的localtime要慢很多。

How to get the first and last day of the month with Perl's DateTime?(在Perl 的DateTime模块中取得某月的第一天及最后一天)
First day:
$dt->set_day(1);

Last day:
$dt->set_day(1)->add( months => 1 )->subtract( days => 1 );

------------------------------
Converting from normal date to epoch in Perl-将日期时间转换为时戳

Using the Time::Local module:
use Time::Local;
my $time = timelocal($sec,$min,$hours,$day,$month,$year);
# replace 'timelocal' with 'timegm' if your input date is GMT/UTC

------------------------------
Using the Date::Parse module
use Date::Parse;
print str2time("02/25/2011 11:50 AM");

The Perl Cookbook, Second Edition gives detailed information on manipulating dates and times in chapter 3 (pages 90-110).As an alternative there is the core Perl module Time::Piece.

For the current month and year:
perl -MTime::Piece -wE '$t=localtime;say $t->month_last_day'
31

------------------------------
取得昨/明天的日期
1、localtime(time() - 24*60*60)

2、引入模块
use v5.10;
#以下两模块是5.10版本以上才有的核心模块
use Time::Piece;
use Time::Seconds;

my $yesterday = localtime() - ONE_DAY;
say $yesterday->strftime('%b %d %Y');

[Using Perl]
Get yesterday's date:
$ perl -e '@T=localtime(time-86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],($T[5]+1900)%100)'

To print tomorrow's date:
$ perl -e '@T=localtime(time+86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],($T[5]+1900)%100)'

[GNU date]
The GNU date command has a powerful -d option that the HP-UX date command does not have. You can do things like:
$ date -d yesterday
$ date -d '2 days ago'
$ date -d '1 week ago'
$ date -d tomorrow
$ date -d '2 days'
$ date -d '1 week'

------------------------------
取得某月最后一天的日期及本月所在的unix时间戳区间
use Time::Piece;
use feature qw(:5.10);
use POSIX qw(strftime);
$ENV{TZ}='Asia/Shanghai';
my $mon=shift @ARGV||8;

my $tt=localtime;
my ($cyear,$offset)=($tt->year,$tt->tzoffset);

my $t=Time::Piece->strptime("$cyear-$mon","%Y-%m");
#当月最后一天
my $lastday=$t->month_last_day;

my $st=Time::Piece->strptime("$cyear-$mon-01 00:00:00","%Y-%m-%d %H:%M:%S");
my $et=Time::Piece->strptime("$cyear-$mon-$lastday 23:59:59","%Y-%m-%d %H:%M:%S");

say $st->epoch-$offset,"\t",$et->epoch-$offset;
#取得该月的起始时间戳区间
my ($mstepoch,$metepoch)=($st->epoch-$offset,$et->epoch-$offset);

------------------------------
取得指定月最后一个星期五
Getting last Friday of the month


Using DateTime the code becomes more readable:
use DateTime;
# Computes the last day in the given month which occurs on the given
# day of the week. Returns the day of the month [22, 31].
# 1 <= $month <= 12
# 1 <= $dow <= 7 (1=Monday)
sub last_dow_in_month {
    my ($year, $month, $dow) = @_;
    my $dt = DateTime->last_day_of_month(year => $year, month => $month);
    $dt->subtract(days => ($dt->day_of_week - $dow) % 7);
    return $dt->day_of_month;
}


Using Time::Piece

use v5.10;
use Time::Piece;

sub get_last_dow_in_month {
    my($year, $month, $dow) = @_;
    # Get a Time::Piece object at the last day of the month.
    my $first_of_the_month = Time::Piece->strptime("$year $month", "%Y %m");
    my $last_day = $first_of_the_month->month_last_day;
    my $last_of_the_month  = Time::Piece->strptime("$year $month $last_day", "%Y %m %d");
    # Figure out how many days you need to go back.
    my $days_offset = -(($last_of_the_month->day_of_week + (7 - $dow)) % 7);
    return $last_of_the_month->mday + $days_offset;
}

say get_last_dow_in_month(2017, 3, 5);

------------------------------