让我们从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