Perl localtime 函式



描述

此函式在列表上下文中轉換由 EXPR 指定的時間,返回一個九元素陣列,其中包含針對當前本地時區分析的時間。陣列的元素如下:

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

如果省略 EXPR,則使用 `time` 返回的值。

$mday 是月份中的日期,$mon 是月份本身,範圍為 0..11,其中 0 表示 1 月,11 表示 12 月。

$year 是自 1900 年以來的年數,而不僅僅是年份的後兩位數字。也就是說,在 2023 年,$year 為 123。獲取完整 4 位年份的正確方法是:$year += 1900;

語法

以下是此函式的簡單語法:

localtime EXPR

返回值

此函式在標量上下文中返回以下形式的字串:Thu Sep 21 14:52:52 2000,在列表上下文中返回各個時間成分值(秒、分、時、月中的日期、月、年、星期幾、一年中的日期、夏令時)。

示例

以下示例程式碼展示了其基本用法:

#!/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_function_references.htm
廣告

© . All rights reserved.