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

[经验分享] PLEAC-Perl 教程 - Date and Time (Perl进阶者极力推荐)

[复制链接]

尚未签到

发表于 2018-8-31 13:10:25 | 显示全部楼层 |阅读模式
  [root@localhost perl]# perl t.pl
  $VAR1 = '2011093011:33:05';
  $VAR1 = '5';
  $VAR2 = '09';
  $VAR3 = '30';
  $VAR4 = '11';
  $VAR5 = '33';
  $VAR6 = '05';
  $VAR7 = '2011';
  [root@localhost perl]# cat t.pl
  #!/usr/bin/perl
  use strict;
  use Data::Dumper;
  use Date::Manip qw(ParseDate UnixDate);
  my $string = "Fri Sep 30 11:33:05 CST 2011";
  my $date = ParseDate($string);
  print Dumper $date;
  my ($w,$mon,$day);
  if (!$date) {
  } else {
  my   @values = UnixDate($date,"%w","%m","%d","%H","%M","%S","%Y");
  print Dumper @values;
  }
  http://blog.csdn.net/bsdplus/article/details/2327111
  3. Dates and Times
  Introduction
  #-----------------------------
  $sec
  #-----------------------------
  $min
  #-----------------------------
  $hours
  #-----------------------------
  $mday
  #-----------------------------
  $month
  #-----------------------------
  $year
  #-----------------------------
  $wday
  #-----------------------------
  $yday
  #-----------------------------
  $isdst
  #-----------------------------
  #Fri Apr 11 09:27:08 1997
  #-----------------------------
  # using arrays
  print "Today is day ", (localtime)[7], " of the current year./n";
  # Today is day 117 of the current year.
  # using Time::tm objects
  use Time::localtime;
  $tm = localtime;
  print "Today is day ", $tm->yday, " of the current year./n";
  # Today is day 117 of the current year.
  #-----------------------------
  Finding Today's Date
  #-----------------------------
  ($DAY, $MONTH, $YEAR) = (localtime)[3,4,5];
  #-----------------------------
  use Time::localtime;
  $tm = localtime;
  ($DAY, $MONTH, $YEAR) = ($tm->mday, $tm->mon, $tm->year);
  #-----------------------------
  ($day, $month, $year) = (localtime)[3,4,5];
  printf("The current date is %04d %02d %02d/n", $year+1900, $month+1, $day);
  # The current date is 1998 04 28
  #-----------------------------
  ($day, $month, $year) = (localtime)[3..5];
  #-----------------------------
  use Time::localtime;
  $tm = localtime;
  printf("The current date is %04d-%02d-%02d/n", $tm->year+1900,
  ($tm->mon)+1, $tm->mday);
  # The current date is 1998-04-28
  #-----------------------------
  printf("The current date is %04d-%02d-%02d/n",
  sub {($_[5]+1900, $_[4]+1, $_[3])}->(localtime));
  #-----------------------------
  use POSIX qw(strftime);
  print strftime "%Y-%m-%d/n", localtime;
  #-----------------------------
  Converting DMYHMS to Epoch Seconds
  #-----------------------------
  use Time::Local;
  $TIME = timelocal($sec, $min, $hours, $mday, $mon, $year);
  $TIME = timegm($sec, $min, $hours, $mday, $mon, $year);
  #-----------------------------
  # $hours, $minutes, and $seconds represent a time today,
  # in the current time zone
  use Time::Local;
  $time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);
  #-----------------------------
  # $day is day in month (1-31)
  # $month is month in year (1-12)
  # $year is four-digit year e.g., 1967
  # $hours, $minutes and $seconds represent UTC time
  use Time::Local;
  $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900);
  #-----------------------------
  Converting Epoch Seconds to DMYHMS
  #-----------------------------
  ($seconds, $minutes, $hours, $day_of_month, $month, $year,
  $wday, $yday, $isdst) = localtime($time);
  #-----------------------------
  use Time::localtime;        # or Time::gmtime
  $tm = localtime($TIME);     # or gmtime($TIME)
  $seconds = $tm->sec;
  # ...
  #-----------------------------
  ($seconds, $minutes, $hours, $day_of_month, $month, $year,
  $wday, $yday, $isdst) = localtime($time);
  printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d/n",
  $hours, $minutes, $seconds, $year+1900, $month+1,
  $day_of_month);
  #-----------------------------
  use Time::localtime;
  $tm = localtime($time);
  printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d/n",
  $tm->hour, $tm->min, $tm->sec, $tm->year+1900,
  $tm->mon+1, $tm->mday);
  #-----------------------------
  Adding to or Subtracting from a Date
  #-----------------------------
  $when = $now + $difference;
  $then = $now - $difference;
  #-----------------------------
  use Date::Calc qw(Add_Delta_Days);
  ($y2, $m2, $d2) = Add_Delta_Days($y, $m, $d, $offset);
  #-----------------------------
  use Date::Calc qw(Add_Delta_DHMS);
  ($year2, $month2, $day2, $h2, $m2, $s2) =
  Add_Delta_DHMS( $year, $month, $day, $hour, $minute, $second,
  $days_offset, $hour_offset, $minute_offset, $second_offset );
  #-----------------------------
  $birthtime = 96176750;                  # 18/Jan/1973, 3:45:50 am
  $interval = 5 +                         # 5 seconds
  17 * 60 +                   # 17 minutes
  2  * 60 * 60 +              # 2 hours
  55 * 60 * 60 * 24;          # and 55 days
  $then = $birthtime + $interval;
  print "Then is ", scalar(localtime($then)), "/n";
  # Then is Wed Mar 14 06:02:55 1973
  #-----------------------------
  use Date::Calc qw(Add_Delta_DHMS);
  ($year, $month, $day, $hh, $mm, $ss) = Add_Delta_DHMS(
  1973, 1, 18, 3, 45, 50, # 18/Jan/1973, 3:45:50 am
  55, 2, 17, 5); # 55 days, 2 hrs, 17 min, 5 sec
  print "To be precise: $hh:$mm:$ss, $month/$day/$year/n";
  # To be precise: 6:2:55, 3/14/1973
  #-----------------------------
  use Date::Calc qw(Add_Delta_Days);
  ($year, $month, $day) = Add_Delta_Days(1973, 1, 18, 55);
  print "Nat was 55 days old on: $month/$day/$year/n";
  # Nat was 55 days old on: 3/14/1973
  #-----------------------------
  Difference of Two Dates
  #-----------------------------
  $seconds = $recent - $earlier;
  #-----------------------------
  use Date::Calc qw(Delta_Days);
  $days = Delta_Days( $year1, $month1, $day1, $year2, $month2, $day2);
  #-----------------------------
  use Date::Calc qw(Delta_DHMS);
  ($days, $hours, $minutes, $seconds) =
  Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, $seconds1,  # earlier
  $year2, $month2, $day2, $hour2, $minute2, $seconds2); # later
  #-----------------------------
  $bree = 361535725;          # 16 Jun 1981, 4:35:25
  $nat  =  96201950;          # 18 Jan 1973, 3:45:50
  $difference = $bree - $nat;
  print "There were $difference seconds between Nat and Bree/n";
  # There were 265333775 seconds between Nat and Bree
  $seconds    =  $difference % 60;
  $difference = ($difference - $seconds) / 60;
  $minutes    =  $difference % 60;
  $difference = ($difference - $minutes) / 60;
  $hours      =  $difference % 24;
  $difference = ($difference - $hours)   / 24;
  $days       =  $difference % 7;
  $weeks      = ($difference - $days)    /  7;
  print "($weeks weeks, $days days, $hours:$minutes:$seconds)/n";
  # (438 weeks, 4 days, 23:49:35)
  #-----------------------------
  use Date::Calc qw(Delta_Days);
  @bree = (1981, 6, 16);      # 16 Jun 1981
  @nat  = (1973, 1, 18);      # 18 Jan 1973
  $difference = Delta_Days(@nat, @bree);
  print "There were $difference days between Nat and Bree/n";
  # There were 3071 days between Nat and Bree
  #-----------------------------
  use Date::Calc qw(Delta_DHMS);
  @bree = (1981, 6, 16, 4, 35, 25);   # 16 Jun 1981, 4:35:25
  @nat  = (1973, 1, 18, 3, 45, 50);   # 18 Jan 1973, 3:45:50
  @diff = Delta_DHMS(@nat, @bree);
  print "Bree came $diff[0] days, $diff[1]:$diff[2]:$diff[3] after Nat/n";
  # Bree came 3071 days, 0:49:35 after Nat
  #-----------------------------
  Day in a Week/Month/Year or Week Number
  #-----------------------------
  ($MONTHDAY, $WEEKDAY, $YEARDAY) = (localtime $DATE)[3,6,7];
  $WEEKNUM = int($YEARDAY / 7) + 1;
  #-----------------------------
  use Date::Calc qw(Day_of_Week Week_Number Day_of_Year);
  # you have $year, $month, and $day
  # $day is day of month, by definition.
  $wday = Day_of_Week($year, $month, $day);
  $wnum = Week_Number($year, $month, $day);
  $dnum = Day_of_Year($year, $month, $day);
  #-----------------------------
  use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text)
  $year  = 1981;
  $month = 6;         # (June)
  $day   = 16;
  $wday = Day_of_Week($year, $month, $day);
  print "$month/$day/$year was a ", Day_of_Week_to_Text($wday), "/n";
  ## see comment above
  $wnum = Week_Number($year, $month, $day);
  print "in the $wnum week./n";
  # 6/16/1981 was a Tuesday
  #
  # in week number 25.
  #-----------------------------
  Parsing Dates and Times from Strings
  #-----------------------------
  use Time::Local;
  # $date is "1998-06-03" (YYYY-MM-DD form).
  ($yyyy, $mm, $dd) = $date =~ /(/d+)-(/d+)-(/d+)/;
  # calculate epoch seconds at midnight on that day in this timezone
  $epoch_seconds = timelocal(0, 0, 0, $dd, $mm, $yyyy);
  #-----------------------------
  use Date::Manip qw(ParseDate UnixDate);
  $date = ParseDate($string);
  if (!$date) {
  # bad date
  } else {
  @values = UnixDate($date, @formats);
  }
  #-----------------------------
  use Date::Manip qw(ParseDate UnixDate);
  while () {
  $date = ParseDate($_);
  if (!$date) {
  warn "Bad date string: $_/n";
  next;
  } else {
  ($year, $month, $day) = UnixDate($date, "%Y", "%m", "%d");
  print "Date was $month/$day/$year/n";
  }
  }
  #-----------------------------
  Printing a Date
  #-----------------------------
  $STRING = localtime($EPOCH_SECONDS);
  #-----------------------------
  use POSIX qw(strftime);
  $STRING = strftime($FORMAT, $SECONDS, $MINUTES, $HOUR,
  $DAY_OF_MONTH, $MONTH, $YEAR, $WEEKDAY,
  $YEARDAY, $DST);
  #-----------------------------
  use Date::Manip qw(UnixDate);
  $STRING = UnixDate($DATE, $FORMAT);
  #-----------------------------
  # Sun Sep 21 15:33:36 1997
  #-----------------------------
  use Time::Local;
  $time = timelocal(50, 45, 3, 18, 0, 73);
  print "Scalar localtime gives: ", scalar(localtime($time)), "/n";
  # Scalar localtime gives: Thu Jan 18 03:45:50 1973
  #-----------------------------
  use POSIX qw(strftime);
  use Time::Local;
  $time = timelocal(50, 45, 3, 18, 0, 73);
  print "strftime gives: ", strftime("%A %D", localtime($time)), "/n";
  # strftime gives: Thursday 01/18/73
  #-----------------------------
  use Date::Manip qw(ParseDate UnixDate);
  $date = ParseDate("18 Jan 1973, 3:45:50");
  $datestr = UnixDate($date, "%a %b %e %H:%M:%S %z %Y");    # as scalar
  print "Date::Manip gives: $datestr/n";
  # Date::Manip gives: Thu Jan 18 03:45:50 GMT 1973
  #-----------------------------
  High-Resolution Timers
  #-----------------------------
  use Time::HiRes qw(gettimeofday);
  $t0 = gettimeofday;
  ## do your operation here
  $t1 = gettimeofday;
  $elapsed = $t1 - $t0;
  # $elapsed is a floating point value, representing number
  # of seconds between $t0 and $t1
  #-----------------------------
  use Time::HiRes qw(gettimeofday);
  print "Press return when ready: ";
  $before = gettimeofday;
  $line = ;
  $elapsed = gettimeofday-$before;
  print "You took $elapsed seconds./n";
  # Press return when ready:
  #
  # You took 0.228149 seconds.
  #-----------------------------
  require 'sys/syscall.ph';
  # initialize the structures returned by gettimeofday
  $TIMEVAL_T = "LL";
  $done = $start = pack($TIMEVAL_T, ());
  # prompt
  print "Press return when ready: ";
  # read the time into $start
  syscall(&SYS_gettimeofday, $start, 0) != -1
  || die "gettimeofday: $!";
  # read a line
  $line = ;
  # read the time into $done
  syscall(&SYS_gettimeofday, $done, 0) != -1
  || die "gettimeofday: $!";
  # expand the structure
  @start = unpack($TIMEVAL_T, $start);
  @done  = unpack($TIMEVAL_T, $done);
  # fix microseconds
  for ($done[1], $start[1]) { $_ /= 1_000_000 }
  # calculate time difference
  $delta_time = sprintf "%.4f", ($done[0]  + $done[1]  )
  -
  ($start[0] + $start[1] );
  print "That took $delta_time seconds/n";
  # Press return when ready:
  #
  # That took 0.3037 seconds
  #-----------------------------
  use Time::HiRes qw(gettimeofday);
  # take mean sorting time
  $size = 500;
  $number_of_times = 100;
  $total_time = 0;
  for ($i = 0; $i < $number_of_times; $i++) {
  my (@array, $j, $begin, $time);
  # populate array
  @array = ();
  for ($j=0; $j]*)/m;
  my($start_date) = /^Date:/s+(.*)/m;
  my $then = getdate($start_date);
  printf &quot;%-20.20s %-20.20s %s/n&quot;, 'Start', $start_from, fmtdate($then);
  my $prevfrom = $start_from;
  # now process the headers lines from the bottom up
  for (reverse split(//n/)) {
  my ($delta, $now, $from, $by, $when);
  next unless /^Received:/;
  s//bon (.*?) (id.*)/; $1/s;         # qmail header, I think
  unless (($when) = /;/s+(.*)$/) {    # where the date falls
  warn &quot;bad received line: $_&quot;;
  next;
  }
  ($from) = /from/s+(/S+)/;
  ($from) = //((.*?)/)/ unless $from; # some put it here
  $from =~ s//)$//;                   # someone was too greedy
  ($by)   = /by/s+(/S+/./S+)/;        # who sent it on this hop
  # now random mungings to get their string parsable
  for ($when) {
  s/ (for|via) .*$//;
  s/([+-]/d/d/d/d) /(/S+/)/$1/;
  s/id /S+;/s*//;
  }
  next unless $now = getdate($when);          # convert to Epoch
  $delta = $now - $then;
  printf &quot;%-20.20s %-20.20s %s  &quot;, $from, $by, fmtdate($now);
  $prevfrom = $by;
  puttime($delta);
  $then = $now;
  }
  exit;
  # convert random date strings into Epoch seconds
  sub getdate {
  my $string     =  shift;
  $string        =~ s//s+/(.*/)/s*$//;        # remove nonstd tz
  my $date       =  ParseDate($string);
  my $epoch_secs =  UnixDate($date,&quot;%s&quot;);
  return $epoch_secs;
  }
  # convert Epoch seconds into a particular date string
  sub fmtdate {
  my $epoch = shift;
  my($sec,$min,$hour,$mday,$mon,$year) = localtime($epoch);
  return sprintf &quot;%02d:%02d:%02d %04d/%02d/%02d&quot;,
  $hour, $min, $sec,
  $year + 1900, $mon + 1, $mday,
  }
  # take seconds and print in pleasant-to-read format
  sub puttime {
  my($seconds) = shift;
  my($days, $hours, $minutes);
  $days    = pull_count($seconds, 24 * 60 * 60);
  $hours   = pull_count($seconds, 60 * 60);
  $minutes = pull_count($seconds, 60);
  put_field('s', $seconds);
  put_field('m', $minutes);
  put_field('h', $hours);
  put_field('d', $days);
  print &quot;/n&quot;;
  }
  # usage: $count = pull_count(seconds, amount)

  # remove from seconds the amount quantity,>  # return the integral number of those amounts so removed.
  sub pull_count {
  my($answer) = int($_[0] / $_[1]);
  $_[0] -= $answer * $_[1];
  return $answer;
  }
  # usage: put_field(char, number)
  # output number field in 3-place decimal format, with trailing char
  # suppress output unless char is 's' for seconds
  sub put_field {
  my ($char, $number) = @_;
  printf &quot; %3d%s&quot;, $number, $char if $number || $char eq 's';
  }
  #-----------------------------
  # Sender               Recipient            Time                   Delta
  #
  # Start                wall.org             09:17:12 1998/05/23
  #
  # wall.org             mail.brainstorm.net  09:20:56 1998/05/23    44s   3m
  #
  # mail.brainstorm.net  jhereg.perl.com      09:20:58 1998/05/23     2s
  #
  #-----------------------------


运维网声明 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-559405-1-1.html 上篇帖子: perl模块 Getopt::Std模块的使用 下篇帖子: perl学习总结之引用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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