Perl time date

In this section, we'll introduce you to the Perl language's handling of time dates.

There are several functions in Perl that handle time:

  • 1, time() function: Returns the number of seconds accumulated since January 1, 1970
  • 2, localtime() function: Get local time zone time
  • 3, gmtime() function: Get GREENWICH

The current time and date

Let's look at the localtime() function, which returns the current time and date without arguments.

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 examples are 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 output of the above examples is:

12 六月 星期天

If you call localtime() directly, it returns the time the system set the time zone for the current period, as follows:

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

The output of the above examples is:

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

GREENWICH (GMT)

The function gmtime() is similar to localtime(), but it returns standard GMT.

#!/usr/bin/perl

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

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

The output of the above examples is:

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

As can be seen from the example, the time in China is eight hours apart from GMT.


Format the date and time

We can use the 9 time elements of the localtime() function to output the formatting time that needs to be developed. Formatting the 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 output of the above examples is:

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

Epoch Time

We can use the time() function to get the time of the new era, which returns the number of seconds accumulated since January 1, 1970. Here's an example:

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

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

The output of the above examples 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 output of the above examples is:

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

POSIX function strftime()

The function strftime() formats the time to the format we want.

The following table lists some formatted symbols, and the number indicates that you want to rely on local time:

Symbol Describe Instance
%a Short for the day of the week (Sun. Sat) * Thu
%A The full name of the day of the week (Sunday: S aturday) * Thursday
%b Short for the month (Jan. Dec) * Aug
%B The full name of the moon (January. December) * August
%c Date and time Thu Aug 23 14:55:02 2001
%C The year is divided by 100 and rounded 00-99 ) 20
%d Day of the month 01-31 ) 23
%D Date, MM/DD/YY equal %m/%d/%y 08/23/01
%e On the first day of the month, fill the single digits 1-31 ) 23
%F YYYY-MM-DD is %Y-%m-%d 2001-08-23
%g The last two digits of 00-99 ) 01
%g Years 2001
%h Short for the month (same %b option) Aug
%H 24-hour system 00-23 ) 14
%I 12-hour system 01-12 ) 02
%j Day of the year 001-366 ) 235
%m Month 01-12 ) 08
%M minutes ( 00-59 ) 55
%n The new line '\n' . )
%p Displays AM or PM PM
%r Time (hh:mm:ss AM or PM), 12 hours 02:55:02 pm
%R 24-hour HH:MM time format equal to %H:%M 14:55
%S Seconds ( 00-61 ) 02
%t Horizontal tabs '\t' . )
%T Time (24-hour) (hh:mm:ss), %H:%M:%S 14:55
%u IsO 8601's day of the week format, Monday 1 1-7 ) 4
%U Sunday is the first day of the year 00-53 ) 33
%V WEEKS 8601 00-53 ) 34
%w Day of the week (0 for Sunday) 0-6 ) 4
%W Monday is the first day 00-53 ) 34
%x Format for display date (mm/dd/yy) 08/23/01
%X Show time format 14:55:02
%y double digits 00-99 ) 01
%Y Years 2001
%z Time zone offset between ISO 8601 and UTC (1 minute, 1 hour, 100)

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

CDT
%% % Symbol %

Here's an example:

#!/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 output of the above examples is:

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