Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Date and Time

Perl Date and Time

In this chapter, we introduce how the Perl language handles dates and times.

The functions for handling time in Perl are as follows:


Current time and date

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

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 set time zone. Example below:

Example

#!/usr/bin/perl

$datestring

=

localtime

(

)

;

print

"

The date and time are:

$datestring

\n

"

;

The output of the above example is:



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


Greenwich Mean Time (GMT)

function gmtime() Similar to localtime(), but it returns standard Greenwich Mean Time.

Example

#!/usr/bin/perl

$local_datestring

=

localtime

(

)

;

print

"

The local date and time are:

$local_datestring

\n

"

;

$gmt_datestring

=

gmtime

(

)

;

print

"

The GMT date and time are:

$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 China's time differs from Greenwich Mean Time by 8 hours.


Formatting dates and times

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

Example

#!/usr/bin/perl

(

$sec

,

$min

,

$hour

,

$mday

,

$mon

,

$year

,

$wday

,

$yday

,

$isdst

)

=

localtime

(

)

;

printf

(

"

Format 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 number of seconds accumulated since January 1, 1970. Example below:

Example

#!/usr/bin/perl

$epoc

=

time

(

)

;

print

"

The number of seconds accumulated since January 1, 1970 is:

$epoc

\n

"

;

The output of the above example is:



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

We can output a time format we want:

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

;

The number of seconds in a day earlier

(

$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 * symbol indicates dependence on local time:

sign before positive numbers Description Example
%a Abbreviated day of the week (Sun..Sat) * Thu
%A Full day of the week (Sunday..Saturday) * Thursday
%b Abbreviated month (Jan..Dec) * Aug
%B Full month (January..December) * August
%c Date and time * Thu Aug 23 14:55:02 2001
%C Year divided by 100, rounded down (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 Day of the month, with single digits padded by spaces (1-31) 23
%F YYYY-MM-DD Abbreviation of , similar to %Y-%m-%d 2001-08-23
%g The last two digits of the year (00-99) 01
%g Year 2001
%h Abbreviated month * (same as%boption) Aug
%H 24-hour clock (00-23) 14
%I 12-hour clock (01-12) 02
%j Day of the year (001-366) 235
%m Month (01-12) 08
%M Minute (00-59) 55
%n New line ('\n') ``
%p Display AM or PM PM
%r Time (hh:mm:ss AM or PM), 12-hour * 02:55:02 pm
%R 24 hours HH:MM Time format, equivalent to %H:%M 14:55
%S Seconds (00-61) 02
%t Horizontal tab ('\t') ``
%T Time (24-hour clock) (hh:mm:ss), equivalent to%H:%M:%S 14:55
%u ISO 8601 day-of-week format, with Monday as1 (1-7) 4
%U Week number of the year, with 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, with Monday as the first day (00-53) 34
%x Display date format (mm/dd/yy) * 08/23/01
%X Display 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 The name of the current time zone, e.g. "China Standard Time" *

CDT
%% % sign before positive numbers %

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