mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-10 22:21:11 +08:00
Translated
This commit is contained in:
parent
62438ef56f
commit
620535311f
@ -1,14 +1,12 @@
|
||||
Vic020
|
||||
|
||||
Linux FAQs with Answers--How to convert between local time and UNIX timestamp in Perl
|
||||
Linux有问必答:Perl中本地时间和UNIX时间戳间相互转换
|
||||
================================================================================
|
||||
> **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?
|
||||
> **问题**: 在Perl语言中,我需要转换易读的日期和时间到对应的UNIX时间戳,反之亦然。你可以给我一些Perl代码例子吗?关于日期及时间转换到UNIX时间戳,或者相反,转换UNIX时间戳到可读的日期和时间。
|
||||
|
||||
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.
|
||||
当你的Perl脚本需要解决时间信息,这里有两种方法来表示和处理日期和时间。一种方法是易读的时间表示(例,"Sat Mar 14 10:14:05 EDT 2015"),另外一种是使用UNIX时间戳(也叫“新纪元时间”),这是从1970年1月1日到今所消耗的时间秒数。每一种方法都有它自己的优劣势,取决于你的需要,也许也就需要转换一种格式到另一种。
|
||||
|
||||
### Convert Local Time to UNIX Timestamp in Perl ###
|
||||
### Perl中转换本地时间到UNIX时间戳 ###
|
||||
|
||||
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:
|
||||
为了从日期字符串中获得UNIX时间,可以使用Date::Parse模块中str2time()函数。此函数可以处理多种格式,例如:
|
||||
|
||||
- Sat Mar 14 10:14:05 EDT 2015
|
||||
- 3/14/2015 10:14:05 -0400
|
||||
@ -22,7 +20,7 @@ To obtain UNIX time from a date string, you can use str2time() in Date::Parse mo
|
||||
# 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:
|
||||
Date:Parse 模块支持多种语言(英语,法语,德语和意大利语)和时区。例如:
|
||||
|
||||
use Date::Parse;
|
||||
use Date::Language;
|
||||
@ -30,17 +28,18 @@ Date:Parse module supports multiple languages (English, French, German and Itali
|
||||
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 ###
|
||||
### Perl中UNIX时间戳到可读的日期和时间 ###
|
||||
|
||||
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
|
||||
如果你想要转换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);
|
||||
|
||||
@ -55,7 +54,7 @@ If you want to show a UNIX timestamp in a human-readable format, you can use loc
|
||||
via: http://ask.xmodulo.com/convert-local-time-unix-timestamp-perl.html
|
||||
|
||||
作者:[Dan Nanni][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[VicYu/Vic020](http://vicyu.net)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
Loading…
Reference in New Issue
Block a user