Perl 中的當前日期和時間
我們先從 Perl 中的 localtime() 函式開始,如果沒有給定引數,它將返回當前日期和時間的數值。下面是使用列表上下文中 localtime 函式返回的 9 元素列表 −
sec, # seconds of minutes from 0 to 61 min, # minutes of hour from 0 to 59 hour, # hours of day from 0 to 24 mday, # day of month from 1 to 31 mon, # month of year from 0 to 11 year, # year since 1900 wday, # days since sunday yday, # days since January 1st isdst # hours of daylight savings time
嘗試以下示例列印 localtime() 函式返回的不同元素 −
示例
#!/usr/local/bin/perl @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print "$mday $months[$mon] $days[$wday]\n";
輸出
執行以上程式碼後,它將產生以下結果 −
16 Feb Sat
如果你在標量上下文中使用 localtime() 函式,那麼它將從系統中設定的當前時區返回日期和時間。嘗試以下示例列印完整格式的當前日期和時間 −
示例
#!/usr/local/bin/perl $datestring = localtime(); print "Local date and time $datestring\n";
輸出
執行以上程式碼後,它將產生以下結果 −
Local date and time Sat Feb 16 06:50:45 2013
廣告