Linux系统入门学习:Perl中本地时间和UNIX时间戳间相互转换
问题: 在Perl语言中,我需要转换易读的日期和时间到对应的UNIX时间戳,反之亦然。你可以给我一些将日期及时间转换到UNIX时间戳的Perl代码例子吗?或者相反,转换UNIX时间戳到可读的日期和时间。
当你的Perl脚本需要解决时间信息,这里有两种方法来表示和处理日期和时间。一种方法是易读的时间表示(例,"Sat Mar 14 10:14:05 EDT 2015"),另外一种是使用UNIX时间戳(也叫“新纪元时间”),这是从1970年1月1日到今所经过的时间秒数。每一种方法都有它自己的优劣势,取决于你的需要,也许也就需要转换一种格式到另一种。
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
<span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Parse</span><span class="pun">;</span>
<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>
<span class="com"># 1426342445 will be stored in $unix_time</span>
<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 模块支持多种语言(英语,法语,德语和意大利语)和时区。例如:
<span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Parse</span><span class="pun">;</span>
<span class="kwd">use</span><span class="typ">Date</span><span class="pun">::</span><span class="typ">Language</span><span class="pun">;</span>
<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>
<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构造任何你需要的可读格式。这里有一个代码片段:
<span class="com"># $sec, $min, $hour: 秒,分,时</span>
<span class="com"># $mday: 月中的某天 (0-31)</span>
<span class="com"># $mon: 月份,范围 0 (一月) 至 11 (十二月)</span>
<span class="com"># $year: 年份,与1900年的差值(2015年为2015-1900=115)</span>
<span class="com"># $wday: 星期,范围 0 (星期天) 至 6 (星期六)</span>
<span class="com"># $yday: 年中的某天,范围 0 至 364 (或 365 闰年)</span>
<span class="com"># $isdst: 是否是夏令时</span>
<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>
<span class="com"># necessary conversion of $mon and $year</span>
<span class="pln">$mon </span><span class="pun">+=</span><span class="lit">1</span><span class="pun">;</span>
<span class="pln">$year </span><span class="pun">+=</span><span class="lit">1900</span><span class="pun">;</span>
<span class="kwd">print</span><span class="str">"Current time: $year-$mon-$mday $hour:$min:$sec\n"</span><span class="pun">;</span>
相关推荐
犇骉 2011-01-24
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16