Linux系统入门学习:Perl中本地时间和UNIX时间戳间相互转换

问题: 在Perl语言中,我需要转换易读的日期和时间到对应的UNIX时间戳,反之亦然。你可以给我一些将日期及时间转换到UNIX时间戳的Perl代码例子吗?或者相反,转换UNIX时间戳到可读的日期和时间。

当你的Perl脚本需要解决时间信息,这里有两种方法来表示和处理日期和时间。一种方法是易读的时间表示(例,"Sat Mar 14 10:14:05 EDT 2015"),另外一种是使用UNIX时间戳(也叫“新纪元时间”),这是从1970年1月1日到今所经过的时间秒数。每一种方法都有它自己的优劣势,取决于你的需要,也许也就需要转换一种格式到另一种。

Linux系统入门学习:Perl中本地时间和UNIX时间戳间相互转换

Perl中转换本地时间到UNIX时间戳

为了从日期字符串中获得UNIX时间,可以使用Date::Parse模块中str2time()函数。此函数可以处理多种格式,例如:

  • Sat Mar 14 10:14:05 EDT 2015
  • 3/14/2015 10:14:05 -0400
  • 14/Mar/15 10:14:05
  • 14 Mar 15 10:14:05
  1. <span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Parse</span><span class="pun">;</span>
  2. <span class="kwd">my</span><span class="pln"> $local_time </span><span class="pun">=</span><span class="str">"Sat Mar 14 10:14:05 EDT 2015"</span><span class="pun">;</span>
  3. <span class="com"># 1426342445 will be stored in $unix_time</span>
  4. <span class="kwd">my</span><span class="pln"> $unix_time </span><span class="pun">=</span><span class="pln"> str2time</span><span class="pun">(</span><span class="pln">$local_time</span><span class="pun">);</span>

Date:Parse 模块支持多种语言(英语,法语,德语和意大利语)和时区。例如:

  1. <span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Parse</span><span class="pun">;</span>
  2. <span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Language</span><span class="pun">;</span>
  3. <span class="kwd">my</span><span class="pln"> $lang </span><span class="pun">=</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Language</span><span class="pun">-></span><span class="kwd">new</span><span class="pun">(</span><span class="str">'French'</span><span class="pun">);</span>
  4. <span class="kwd">my</span><span class="pln"> $unix_time </span><span class="pun">=</span><span class="pln"> $lang</span><span class="pun">-></span><span class="pln">str2time</span><span class="pun">(</span><span class="str">"12:14:05, Ago 16, 2014 (CEST)"</span><span class="pun">);</span>

Perl中UNIX时间戳到易读的日期和时间

如果你想要转换UNIX时间戳到易读的格式,可以使用localtime()函数,此函数可以转换UNIX时间戳为一个9元素列表。然后你可以使用返回的list构造任何你需要的可读格式。这里有一个代码片段:

  1. <span class="com"># $sec, $min, $hour: 秒,分,时</span>
  2. <span class="com"># $mday: 月中的某天 (0-31)</span>
  3. <span class="com"># $mon: 月份,范围 0 (一月) 至 11 (十二月)</span>
  4. <span class="com"># $year: 年份,与1900年的差值(2015年为2015-1900=115)</span>
  5. <span class="com"># $wday: 星期,范围 0 (星期天) 至 6 (星期六)</span>
  6. <span class="com"># $yday: 年中的某天,范围 0 至 364 (或 365 闰年)</span>
  7. <span class="com"># $isdst: 是否是夏令时</span>
  8. <span class="kwd">my</span><span class="pun">(</span><span class="pln">$sec</span><span class="pun">,</span><span class="pln"> $min</span><span class="pun">,</span><span class="pln"> $hour</span><span class="pun">,</span><span class="pln"> $mday</span><span class="pun">,</span><span class="pln"> $mon</span><span class="pun">,</span><span class="pln"> $year</span><span class="pun">,</span><span class="pln"> $wday</span><span class="pun">,</span><span class="pln"> $yday</span><span class="pun">,</span><span class="pln"> $isdst</span><span class="pun">)</span><span class="pun">=</span><span class="pln"> localtime</span><span class="pun">(</span><span class="pln">$unix_timestamp</span><span class="pun">);</span>
  9. <span class="com"># necessary conversion of $mon and $year</span>
  10. <span class="pln">$mon </span><span class="pun">+=</span><span class="lit">1</span><span class="pun">;</span>
  11. <span class="pln">$year </span><span class="pun">+=</span><span class="lit">1900</span><span class="pun">;</span>
  12. <span class="kwd">print</span><span class="str">"Current time: $year-$mon-$mday $hour:$min:$sec\n"</span><span class="pun">;</span>

相关推荐