zoukankan      html  css  js  c++  java
  • perl 时间相关


    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";

    运行结果:

    转载请注明出处:http://www.cnblogs.com/tobecrazy/

    软件测试交流QQ群:312937087 we are QA!

  • 相关阅读:
    android自定义控件onLayout方法
    android自定义控件onMeasure方法
    activity的四种启动模式详细分析
    android屏幕适配的全攻略2--支持手机各种屏幕密度dpi
    android屏幕适配的全攻略3-动态获取手机屏幕宽高及动态设置控件宽高
    DIV滚动条
    .NET面试题6
    .NET面试题5
    .NET面试题4
    .NET面试题2
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3104919.html
Copyright © 2011-2022 走看看