Latest web development tutorials

Perl Date Time

This section, we introduce the Perl language for the date and time of processing.

Perl is a function of the processing time are the following:

  • 1, time () function: Returns from January 1, 1970 the cumulative number of seconds
  • 2, localtime () function: Get the local time zone
  • 3, gmtime () function: Get GMT

Current time and date

Let's looklocaltime () function, which returns the current date and time at no parameters.

The following nine symbols representing different date and time parameters:

sec,     # 秒, 0 到 61
min,     # 分钟, 0 到 59
hour,    # 小时, 0 到 24
mday,    # 天, 1 到 31
mon,     # 月, 0 到 11
year,    # 年,从 1900 开始
wday,    # 星期几,0-6,0表示周日
yday,    # 一年中的第几天,0-364,365
isdst    # 如果夏令时有效,则为真

Examples of presentation as follows:

#!/usr/bin/perl
 
@months = qw( 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 );
@days = qw(星期天 星期一 星期二 星期三 星期四 星期五 星期六);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "$mday $months[$mon] $days[$wday]\n";

Examples of the implementation of the above output is:

12 六月 星期天

If the direct call to localtime (), which returns the current system time zone settings, the examples are as follows:

#!/usr/bin/perl
 
$datestring = localtime();
print "时间日期为:$datestring\n";

Examples of the implementation of the above output is:

时间日期为:Sun Jun 12 11:27:31 2016

Greenwich Mean Time (GMT)

Functiongmtime () and localtime () is similar, but it returns the standard Greenwich Mean Time.

#!/usr/bin/perl

$local_datestring = localtime();
print "本地时间日期为:$local_datestring\n";

$gmt_datestring = gmtime();
print "GMT 时间日期为:$gmt_datestring\n";

Examples of the implementation of the above output is:

本地时间日期为:Sun Jun 12 11:32:14 2016
GMT 时间日期为:Sun Jun 12 03:32:14 2016

We can see from the example, China and the GMT time difference of 8 hours.


Format date and time

We can use localtime () 9 time function element to the output format of the time required to develop. Formatted output using the printf () function:

#!/usr/bin/perl
 
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();

printf("格式化时间:HH:MM:SS\n");
printf("%02d:%02d:%02d", $hour, $min, $sec);

Examples of the implementation of the above output is:

格式化时间:HH:MM:SS
11:35:23

New Era Time (Epoch Time)

We can use the time () function to obtain a new era of time, the function returns from January 1, 1970 Cumulative number of seconds. Examples are as follows:

#!/usr/bin/perl
 
$epoc = time();

print "从1970年1月1日起累计的秒数为:$epoc\n";

Examples of the implementation of the above output is:

从1970年1月1日起累计的秒数为:1465702883

We can output a time format we want:

#!/usr/bin/perl

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "当期时间和日期:";
printf("%d-%d-%d %d:%d:%d",$year+1990,$mon+1,$mday,$hour,$min,$sec);

print "\n";

$epoc = time();
$epoc = $epoc - 24 * 60 * 60;   # 一天前的时间秒数
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoc);
print "昨天时间和日期:";
printf("%d-%d-%d %d:%d:%d",$year+1990,$mon+1,$mday,$hour,$min,$sec);

print "\n";

Examples of the implementation of the above output is:

当期时间和日期:2106-6-12 11:49:28
昨天时间和日期:2106-6-11 11:49:28

POSIX function strftime ()

Function strftime () can be formatted as time format we want.

The following table lists some of the formatting symbols, asterisk indicates you want to rely on local time:

symbol description Examples
%a Week abbreviation (Sun..Sat) * Thu
%A Week full name (Sunday..Saturday) * Thursday
%b Month abbreviation (Jan..Dec) * Aug
%B The full name of the month (January..December) * August
%c Date and Time * Thu Aug 23 14:55:02 2001
%C In addition to the year 100, and rounding ( 00-99 ) 20
%d Day of the month ( 01-31 ) 23
%D Date, MM/DD/YY equivalent to %m/%d/%y 08/23/01
%e The first few days of the month, padded with spaces digits ( 1-31 ) 23
%F YYYY-MM-DD shorthand similar to %Y-%m-%d 2001-08-23
%g The last two digits of the year ( 00-99 ) 01
%g year 2001
%h * Short months (and %b option) Aug
%H 24-hour clock ( 00-23 ) 14
%I 12-hour clock ( 01-12 ) 02
%j The first few days of the year ( 001-366 ) 235
%m Month ( 01-12 ) 08
%M Min ( 00-59 ) 55
%n Newline ( '\n' )
%p Show AM or PM PM
%r Time (hh: mm: ss AM or PM), 12 hours * 02:55:02 pm
%R 24 hours HH:MM time format, equal to %H:%M 14:55
%S Seconds ( 00-61 ) 02
%t Horizontal tab ( '\t' )
%T Time (24-hour clock) (hh: mm: ss) , equal to %H:%M:%S 14:55
%u ISO 8601 format of the week, Monday is 1 ( 1-7 ) 4
%U The first few weeks of the year, Sunday is the first day ( 00-53 ) 33
%V ISO 8601 week number ( 00-53 ) 34
%w The first few days (0 for Sunday) a week ( 0-6 ) 4
%W The first few weeks of the year, Monday is the first day ( 00-53 ) 34
%x Format to display the date (mm / dd / yy) * 08/23/01
%X Display Time format * 14:55:02
%y , The two-digit ( 00-99 ) 01
%Y year 2001
%z ISO 8601 time zone offset from UTC (1 minute = 1, 1 hour = 100)

+100
%Z Name of the current time zone, such as "China Standard Time" *

CDT
%% % Symbol %

Examples are as follows:

#!/usr/bin/perl
use POSIX qw(strftime);

$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
printf("时间日期 - $datestring\n");

#  GMT 格式化时间日期
$datestring = strftime "%Y-%m-%d %H:%M:%S", gmtime;
printf("时间日期 - $datestring\n");

Examples of the implementation of the above output is:

时间日期 - 2016-06-12 12:15:13
时间日期 - 2016-06-12 04:15:13