设为首页 收藏本站
查看: 1420|回复: 0

[经验分享] perl 时间相关

[复制链接]

尚未签到

发表于 2015-12-25 15:22:59 | 显示全部楼层 |阅读模式
  
1.获取系统时间
  孔乙己说:回字有四样写法,你知道么?
  我说: 我不知道,我只知道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中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
  %a 星期几的简写
  %A 星期几的全称
  %b 月分的简写
  %B 月份的全称
  %c 标准的日期的时间串
  %C 年份的后两位数字
  %d 十进制表示的每月的第几天
  %D 月/天/年
  %e 在两字符域中,十进制表示的每月的第几天
  %F 年-月-日
  %g 年份的后两位数字,使用基于周的年
  %G 年分,使用基于周的年
  %h 简写的月份名
  %H 24小时制的小时
  %I 12小时制的小时
  %j 十进制表示的每年的第几天
  %m 十进制表示的月份
  %M 十时制表示的分钟数
  %n 新行符
  %p 本地的AM或PM的等价显示
  %r 12小时的时间
  %R 显示小时和分钟:hh:mm
  %S 十进制的秒数
  %t 水平制表符
  %T 显示时分秒:hh:mm:ss
  %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
  %U 第年的第几周,把星期日做为第一天(值从0到53)
  %V 每年的第几周,使用基于周的年
  %w 十进制表示的星期几(值从0到6,星期天为0)
  %W 每年的第几周,把星期一做为第一天(值从0到53)
  %x 标准的日期串
  %X 标准的时间串
  %y 不带世纪的十进制年份(值从0到99)
  %Y 带世纪部分的十制年份
  %z,%Z 时区名称,如果不能得到时区名称则返回空字符。
  %% 百分号
  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";
  运行结果:
DSC0000.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156288-1-1.html 上篇帖子: perl简明教程 下篇帖子: perl中的map和grep
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表