Perl localtime 函数

描述

This function converts the time specified by EXPR in a list context, returning a nine-element array with the time analyzed for the current local time zone. The elements of the array are −

 # 0  1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

If EXPR is omitted, uses the value returned by time.

$mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December.

$year is the number of years since 1900, not just the last two digits of the year. That is, $year is 123 in year 2023. The proper way to get a complete 4-digit year is simply: $year += 1900;


语法

以下是此函数的简单语法 −

localtime EXPR

返回值

This function returns a string of the form: Thu Sep 21 14:52:52 2000 in scalar context and the individual time component values (seconds, minutes, hours, day of month, month, year, day of week, day of year, daylight savings time) in list context.


示例

以下是显示其基本用法的示例代码 −

#!/usr/bin/perl -w
use POSIX;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                          localtime(time);
$year += 1900;
print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n";
$now_string = localtime; 
print "$now_string\n";

$now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$now_string\n";

执行上述代码时,会产生以下结果 −

19, 58, 14, 1, 8, 2013, 0, 243, 0
Sun Sep  1 14:58:19 2013
Sun Sep  1 14:58:19 2013

❮ Perl 函数参考