Merge pull request #2538 from Vic020/master

Translated:20150316 Linux FAQs with Answers--How to convert between local time and UNIX timestamp in Perl.md
This commit is contained in:
Xingyu.Wang 2015-03-20 09:33:20 +08:00
commit 030c07d045
2 changed files with 62 additions and 63 deletions

View File

@ -1,63 +0,0 @@
Vic020
Linux FAQs with Answers--How to convert between local time and UNIX timestamp in Perl
================================================================================
> **Question**: I need to convert readable date and time to a corresponding UNIX timestamp (i.e., Epoch time), or vice versa in a Perl program. Can you show me Perl code examples of converting date/time to UNIX timestamp, or conversely, converting UNIX timestamp to human-readable date and time?
When your Perl script needs to deal with time information, there are two different ways to represent and manipulate date and time in the script. One way is a human-readable string representation of time (e.g., "Sat Mar 14 10:14:05 EDT 2015"), and the other is via UNIX timestamp (also known as "Epoch time") which is the number of elapsed seconds since 00:00:00 UTC, 1 Jaunary 1970. Either method has its own pros and cons, and depending on your requirements you may want to convert one format to the other.
### Convert Local Time to UNIX Timestamp in Perl ###
To obtain UNIX time from a date string, you can use str2time() in Date::Parse module. It can handle a variety of date formats, such as:
- 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
use Date::Parse;
my $local_time = "Sat Mar 14 10:14:05 EDT 2015";
# 1426342445 will be stored in $unix_time
my $unix_time = str2time($local_time);
Date:Parse module supports multiple languages (English, French, German and Italian) and timezones. For example:
use Date::Parse;
use Date::Language;
my $lang = Date::Language->new('French');
my $unix_time = $lang->str2time("12:14:05, Ago 16, 2014 (CEST)");
### Convert UNIX Timestmp to Human-Readable Date and Time in Perl ###
If you want to show a UNIX timestamp in a human-readable format, you can use localtime() function which converts a UNIX timestamp into a 9-element list. You can use the returned list to construct any kind of human-readable date/time string as you want. Here is a code snippet.
# $sec, $min, $hour: seconds, minutes and hours
# $mday: the day of the month (0-31)
# $mon: month in the range of 0 (Jaunary) and 11 (December)
# $year: the number of years that have elapsed since 1900
# $wday: the day of the week in the range of 0 (Sunday) and 6 (Saturday)
# $yday: the day of the year in the range of 0 and 364 (or 365 in leap years)
# $isdst: whether or not it's in daylight saving time
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($unix_timestamp);
# necessary conversion of $mon and $year
$mon += 1;
$year += 1900;
print "Current time: $year-$mon-$mday $hour:$min:$sec\n";
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/convert-local-time-unix-timestamp-perl.html
作者:[Dan Nanni][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://ask.xmodulo.com/author/nanni

View File

@ -0,0 +1,62 @@
Linux有问必答Perl中本地时间和UNIX时间戳间相互转换
================================================================================
> **问题**: 在Perl语言中我需要转换易读的日期和时间到对应的UNIX时间戳反之亦然。你可以给我一些Perl代码例子吗关于日期及时间转换到UNIX时间戳或者相反转换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
use Date::Parse;
my $local_time = "Sat Mar 14 10:14:05 EDT 2015";
# 1426342445 will be stored in $unix_time
my $unix_time = str2time($local_time);
Date:Parse 模块支持多种语言(英语,法语,德语和意大利语)和时区。例如:
use Date::Parse;
use Date::Language;
my $lang = Date::Language->new('French');
my $unix_time = $lang->str2time("12:14:05, Ago 16, 2014 (CEST)");
### Perl中UNIX时间戳到可读的日期和时间 ###
如果你想要转换UNIX时间戳到可读的格式可以使用localtime()函数此函数可以转换UNIX时间戳为一个9元素列表。然后你可以使用返回的list构造任何你需要的可读格式。这里有一个代码片段
# $sec, $min, $hour: 秒,分,时
# $mday: 月中的某天 (0-31)
# $mon: 月份,范围 0 (一月) 至 11 (十二月)
# $year: 年份与1900年的差值(2015年为2015-1900=115)
# $wday: 星期,范围 0 (星期天) 至 6 (星期六)
# $yday: 年中的某天,范围 0 至 364 (或 365 闰年)
# $isdst: 是否是夏令时
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($unix_timestamp);
# necessary conversion of $mon and $year
$mon += 1;
$year += 1900;
print "Current time: $year-$mon-$mday $hour:$min:$sec\n";
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/convert-local-time-unix-timestamp-perl.html
作者:[Dan Nanni][a]
译者:[VicYu/Vic020](http://vicyu.net)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://ask.xmodulo.com/author/nanni