Perl date and time


In this chapter, we introduce to you how Perl language handles time and date.

The functions that handle time in Perl are as follows:

  • 1. time() function: Returns from January 1, 1970 The accumulated number of seconds since

  • 2. localtime() function: Get the local time zone time

  • 3 , gmtime() function: Get Greenwich Mean Time


Current time and date

Let us look at localtime( ) Function that returns the current time and date without parameters.

The following 9 symbols represent different time and date 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    # 如果夏令时有效,则为真

The example demonstration is 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";

The execution output result of the above example is:

12 六月 星期天

If Directly call localtime(), which returns the time of the system's current time zone setting. The example is as follows:

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

The execution output result of the above example is:

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

Greenwich Mean Time (GMT)

Functiongmtime() Similar to localtime(), but it returns standard Greenwich Time.

#!/usr/bin/perl

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

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

The execution output of the above example is:

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

From the example we can see that the time in China is 8 hours different from Greenwich Mean Time.


Format date and time

We can use the 9 time elements of the localtime() function to output the format time that needs to be formulated. Formatted output uses 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);

The above example execution output result is:

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

New Epoch Time (Epoch Time)

We can use time( ) function to get the epoch time, which returns the number of seconds since January 1, 1970. The example is as follows:

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

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

The execution output result of the above example 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";

The execution output result of the above example is:

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

POSIX function strftime()

The function strftime() can format the time into the format we want.

The following table lists some formatting symbols. The * symbol indicates that you want to rely on local time:

The full name of the day of the week (Sunday..Saturday) * Abbreviation of month (Jan..Dec) *##%BAugust##%c%C)##%d%D%m/%d/%yThe day of the month, use spaces to fill in single digits (The last two digits of the year (00-9901年Abbreviation of month* (same option as %b)## %H00-23)%I01-12)%j001-366)%m01-12)%M00-59)%n'\n')PM02:55:02 pm##%R%H:%M0214:551-7)ISO 8601 Week (##%w0-64##%W)%x%X%y)%Y%z%Z##%%The execution output result of the above example is:
时间日期 - 2016-06-12 12:15:13
时间日期 - 2016-06-12 04:15:13
SymbolDescriptionInstance
%aAbbreviation of day of the week (Sun..Sat) *Thu
##%AThursday
%bAug
of month Full name (January..December) *
Date and time* Thu Aug 23 14:55:02 2001
Year divided by 100 , and rounded to the nearest integer (00-9920
The day of the month (01-31)23
Date, MM/DD/YY is equal to 08/23/ 01##%e
1-31)23%F
YYYY-MM-DD# The abbreviation of ## is similar to %Y-%m-%d2001-08-23%g
)##%g
2001%h
Aug24-hour format (
14 12 hour format (
02 The day of the year (
235月(
08 minutes (
55New line(
##%p displays AM or PM
%rTime (hh: mm: ss AM or PM), 12 hours *
24 hours HH:MM time format, equal to
14:55%SSeconds (00-61)
%tHorizontal tab ('\t')
%T Time (24-hour format) (hh:mm:ss), equal to %H:%M:%S
%u ISO 8601 day of the week format, Monday is 1 (
4
%UThe number of weeks in the year, Sunday is the first day (00-53) 33
##%V00-53)34
The day of the week (0 represents Sunday) ( )
一Which week of the year, Monday is the first day (00-5334
Display date format (mm/dd/yy) *08/23/01
Display time format*14:55:02
year, two digits (00-9901
2001
Time zone offset between ISO 8601 and UTC (1 minute=1, 1 hour=100)


##+100

The name of the current time zone, such as "China Standard Time" *

##CDT

%%
Symbol The example is 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");