在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
孔乙己说:回字有四样写法,你知道么? 我说: 我不知道,我只知道perl获取系统时间有3种方法(孤陋寡闻) 1. 使用localtime函数 Converts a time as returned by the time function to a 9-element a hash list with the time analyzed for the local time zone. Typically used as follows: # 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); All list elements are numeric and come straight out of the C `struct tm'. $sec, $min, and $hour are the seconds, minutes, and hours of the specified time. $mday is the day of the month and $mon the month in the range 解释 mday是每个月的天(1~28/29/30/31) 0..11, with 0 indicating January and 11 indicating December. mon 是月份 0-11 真实的月份需要$mon+1 This makes it easy to get a month name from a list: my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) print "$abbr[$mon] $mday"; # $mon=9, $mday=18 gives "Oct 18" $year contains the number of years since 1900. To get a 4-digit 2.使用POSIX strftime函数 例如:strftime("%Y-%m-%d %H:%M:%S", localtime); 输出2013-05-28 22:25:12 函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。 3.使用shell 获取 $(date +%Y-%m-%d" "%H:%M:%S ) %Y year %m month %d the day of month %s second since 1970 %H hour %M minute %S second
补充:get microsecond gettimeofday () In array context it returns a 2 element array with the seconds and microseconds since the epoch. In scalar context it returns floating seconds like Time::HiRes::time() (see below). 使用Time::HiRes qw/gettimeofday/ 会返回2个值 ($second,$microsecond)=gettimeofday(); 第一个是 1970到当前时间总秒数;第二个是当今microsecond
1 #!/usr/bin/perl -w 2 use Time::HiRes qw/gettimeofday tv_interval/; 3 use POSIX; 4 sub gettime 5 { 6 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 7 $year+=1900; #$year return since 1900,so current year=1900+$year 8 $mon+=1; #$mon return month between 0 and 11 9 $mon="0$mon" if( $mon<10); #add a 0 if <10,eg:5 ->05 10 $mday="0$mday" if( $mday<10); 11 $hour="0$hour" if( $hour<10); 12 $min="0$min" if( $min<10); 13 $sec="0$sec" if( $sec<10); 14 #print "$year-$mon-$mday $hour:$min:$sec\n"; 15 return my %temp=('second'=>$sec, 16 'minute'=>$min, 17 'hour'=>$hour, 18 'day'=>$mday, 19 'month'=>$mon, 20 'year'=>$year, 21 'date'=>"$year-$mon-$mday", 22 'hms'=>"$hour:$min:$sec"); 23 } 24 25 my %time=&gettime; 26 print "$time{'date'} $time{'hms'}\n"; 27 my $day=strftime("%Y-%m-%d %H:%M:%S", localtime); 28 print "$day\n"; 29 my $day_shell=`echo \$(date +%Y-%m-%d" "%H:%M:%S )`; 30 print $day_shell; 31 sub getmicrosecond 32 { 33 (my $second,my $microsecond)=gettimeofday(); 34 if(length($microsecond)<6) 35 { 36 $microsecond="0" x (6-length($microsecond)).$microsecond; #make sure the length of microsend is 6 37 } 38 return "$second$microsecond"; 39 40 } 41 my $microsec1=&getmicrosecond; 42 print "The first time get microsecond :$microsec1\n"; 43 print "wait for two seconds\n"; 44 sleep 2; 45 my $microsec2=&getmicrosecond; 46 print "The second time get microsecond :$microsec2\n"; 47 print "get two seconds microsends"; 48 print $microsec2-$microsec1."\n"; 运行结果: |
请发表评论