Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5361489
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: Oracle

2013-08-11 08:59:29

----------
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
阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~