Perl Date and Time

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

Perl provides the following functions for handling time:

  • 1. The time() function:Returns the cumulative seconds since January 1, 1970
  • 2. The localtime() function:Gets the local time zone time
  • 3. The gmtime() function:Gets Greenwich Mean Time

Current Time and Date

Next, let's take a look at thelocaltime()function, which returns the current time and date when called 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:

Example

#!/usr/bin/perl @months = qw(January February March April May June July August September October November December); @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print "$mday $months[$mon] $days[$wday]\n";

The output of the above example is:

12 六月 星期天

If you call localtime() directly, it returns the time in the system's currently configured time zone, as shown in the example below:

Example

#!/usr/bin/perl $datestring = localtime(); print "The date and time is:$datestring\n";

The output of the above example is:

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

Greenwich Mean Time (GMT)

The functiongmtime()is similar to localtime(), but it returns standard Greenwich Mean Time.

Example

#!/usr/bin/perl $local_datestring = localtime(); print "The local date and time is:$local_datestring\n"; $gmt_datestring = gmtime(); print "The GMT date and time is:$gmt_datestring\n";

The 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 Chinese time is 8 hours ahead of Greenwich Mean Time.


Formatting Date and Time

We can use the 9 time elements of the localtime() function to output the time in a desired format. Use the printf() function for formatted output:

Example

#!/usr/bin/perl ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); printf("Formatted time: HH:MM:SS\n"); printf("%02d:%02d:%02d", $hour, $min, $sec);

The output of the above example is:

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

Epoch Time

We can use the time() function to get the epoch time, which returns the cumulative seconds since January 1, 1970. Here is an example:

Example

#!/usr/bin/perl $epoc = time(); print "The cumulative seconds since January 1, 1970 is:$epoc\n";

The output of the above example is:

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

We can output a time format of our choosing:

Example

#!/usr/bin/perl ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print "Current time and date:"; printf("%d-%d-%d %d:%d:%d",$year+1900,$mon+1,$mday,$hour,$min,$sec); print "\n"; $epoc = time(); $epoc = $epoc - 24 * 60 * 60; # Seconds of the time one day ago ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoc); print "Yesterday's time and date:"; printf("%d-%d-%d %d:%d:%d",$year+1900,$mon+1,$mday,$hour,$min,$sec); print "\n";

The output of the above example is:

当期时间和日期:2017-3-15 12:47:54
昨天时间和日期:2017-3-14 12:47:54

POSIX Function strftime()

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

The following table lists some formatting symbols; the * indicates that it depends on local time:

Symbol Description Example
%a Abbreviated weekday name (Sun..Sat) * Thu
%A Full weekday name (Sunday..Saturday) * Thursday
%b Abbreviated month name (Jan..Dec) * Aug
%B Full month name (January..December) * August
%c Date and time * Thu Aug 23 14:55:02 2001
%C Year divided by 100, and rounded down (00-99) 20
%d Day of the month (01-31) 23
%D Date,MM/DD/YYequivalent to%m/%d/%y 08/23/01
%e Day of the month, single digits padded with spaces ( 1-31) 23
%F YYYY-MM-DDThe abbreviation is similar to%Y-%m-%d 2001-08-23
%g Last two digits of the year (00-99) 01
%g year 2001
%h Abbreviated month name * (same as%boption) Aug
%H24-hour format (00-23) 14
%I 12-hour format (01-12) 02
%j Day of the year (001-366) 235
%m Month (01-12) 08
%M Minute (00-59) 55
%n Newline ('\n')
%pDisplays AM or PM PM
%r Time (hh:mm:ss AM or PM), 12-hour * 02:55:02 pm
%R 24-hourHH:MMTime format, equivalent to%H:%M 14:55
%S Seconds (00-61) 02
%t Horizontal tab ('\t')
%T Time (24-hour format) (hh:mm:ss), equivalent to%H:%M:%S 14:55
%u ISO 8601 weekday format, Monday is1 (1-7) 4
%U Week number of the year, Sunday as the first day (00-53) 33
%V ISO 8601 week number (00-53) 34
%w Day of the week (0 represents Sunday) (0-6) 4
%W Week number of the year, Monday as the first day (00-53) 34
%x Displays date format (mm/dd/yy) * 08/23/01
%X Displays time format * 14:55:02
%y Year, two digits (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, e.g., "China Standard Time" *

CDT
%% %Symbol %

Example

#!/usr/bin/perl use POSIX qw(strftime); $datestring = strftime "%Y-%m-%d %H:%M:%S", localtime; printf("Date and time -$datestring\n"); # GMT formatted date and time $datestring = strftime "%Y-%m-%d %H:%M:%S", gmtime; printf("Date and time -$datestring\n");

The output of the above example is:

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