Perl 中的紀元時間


您可以在 Perl 中使用 time() 函式獲取紀元時間,即從一個特定日期開始經過的秒數,在 Unix 中該日期為 1970 年 1 月 1 日。

示例

 線上演示

#!/usr/local/bin/perl
$epoc = time();
print "Number of seconds since Jan 1, 1970 - $epoc\n";

輸出

執行上述程式碼時,將產生以下結果 -

Number of seconds since Jan 1, 1970 - 1361022130

您可以按照以下方法將給定的秒數轉換為日期和時間字串 -

示例

 線上演示

#!/usr/local/bin/perl
$datestring = localtime();
print "Current date and time $datestring\n";
$epoc = time();
$epoc = $epoc - 24 * 60 * 60;    # one day before of current date.
$datestring = localtime($epoc);
print "Yesterday's date and time $datestring\n";

輸出

執行上述程式碼時,將產生以下結果 -

Current date and time Tue Jun 5 05:54:43 2018
Yesterday's date and time Mon Jun 4 05:54:43 2018

更新日期:2019-11-29

7K+ 瀏覽量

開啟你的 職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.