最近需要将字符串转换成时间,找了下资料,实战如下,发现时timelocal费了些时间
strftime也可在 c / c++ / awk / php 中使用,用法基本一致。
这个也不错
$time = strftime("%Y-%m-%d %H:%M:%S", localtime($time));
print "$time
";
print str2time($time);
use POSIX qw(strftime); use Time::Local; use HTTP::Date; my $time = "{"date":"2013-06-24","time":"01:53:40"}"; print "$time "; print str2time($time); formattime($time); sub formattime{ my ($srctime) = @_; my ($year,$mon,$day,$hour,$min,$sec) = $srctime=~ /.+?(d+)-(d+)-(d+).+?(d+):(d+):(d+)/ ; print " $year,$mon,$day,$hour,$min,$sec "; $time = str2time("$year-$mon-$day $hour:$min:$sec"); #print "$time "; #print strftime("%Y%m%d%H%M%S",localtime($time)); return $time; }
use strict; use POSIX; use Time::Local; my $time = time(); print "$time "; my @time = localtime($time); print "@time "; $time = strftime("%Y-%m-%d %H:%M:%S", localtime($time)); print "$time "; my ($year,$mon,$day,$hour,$min,$sec) = $time=~/(d+)-(d+)-(d+) (d+):(d+):(d+)/ ; print "$year,$mon,$day,$hour,$min,$sec "; $time = timelocal($sec,$min,$hour,$day,$mon,$year); print "$time ";