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:
Symbol | Description | Instance |
---|---|---|
%a | Abbreviation of day of the week (Sun..Sat) * | Thu |
##%A
| The full name of the day of the week (Sunday..Saturday) *Thursday
| |
%b
| Abbreviation of month (Jan..Dec) *Aug
| |
of month Full name (January..December) * | August |
|
Date and time* | Thu Aug 23 14:55:02 2001 |
|
Year divided by 100 , and rounded to the nearest integer ( 00-99 | ) 20 |
| ##%d
The day of the month (01-31 ) | 23 |
| %D
Date, MM/DD/YY is equal to | %m/%d/%y
08/23/ 01 | ##%e |
1-31) | 23
| %F |
YYYY-MM-DD# The abbreviation of ## is similar to %Y-%m-%d | 2001-08-23
| %g |
)
| 01
| ##%g
| 年
2001
| %h | Abbreviation of month* (same option as |
Aug |
| ## %H 24-hour format ( | 00-23
14 | | %I 12 hour format ( | 01-12
02 | | %j The day of the year ( | 001-366
235 |
| %m 月( | 01-12
08 |
| %M minutes ( | 00-59
55 |
| %n New line( | '\n'
| ##%p | displays AM or PM |
| %r | Time (hh: mm: ss AM or PM), 12 hours
* |
| ##%R24 hours HH:MM time format, equal to | %H:%M|
14:55 |
%S | Seconds (00-61 ) |
| %t | Horizontal 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 ( | 1-7
4 | ||
%U | The number of weeks in the year, Sunday is the first day (00-53 ) | 33 |
##%V
| ISO 8601 Week (00-53) | 34
|
The day of the week (0 represents Sunday) ( | 0-6)
| 4
|
一Which week of the year, Monday is the first day ( 00-53 | ) 34 |
|
Display date format (mm/dd/yy) * | 08/23/01 | |
Display time format* | 14:55:02 |
|
year, two digits ( 00-99 | ) 01 |
|
年 | 2001 |
|
Time zone offset between ISO 8601 and UTC (1 minute=1, 1 hour=100) |
|
| %Z
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"); | The execution output result of the above example is: