📅  最后修改于: 2021-01-07 08:41:12             🧑  作者: Mango
在erl中处理日期和时间的最合适方法是使用DateTime模块。
在执行日期和时间的每个操作之前,我们需要通过脚本将DateTime加载到内存中。
use DateTime;
如果不带任何参数使用localtime()函数,则会根据系统返回当前日期和时间。
#!/usr/local/bin/perl
$datetime = localtime();
print "Current date and time according to the system : $datetime\n";
输出:
Current date and time according to the system : Fri Jan 6 11:52:04 2017
显示当前日期和时间的DateTime对象可以通过现在呼吁构造方法创建。
例:
use DateTime;
my $datetime = DateTime->now;
print "$datetime\n";
输出:
2017-01-06T06:29:38
我们还可以通过部分提供所有详细信息(例如日期,分钟,秒等)来创建DateTime对象。perl将假定数据为“ 0”,而不会传递任何详细信息。
例:
use DateTime;
$datetime = DateTime->new(
day => 18,
month => 7,
year => 2003,
hour => 12,
);
print"$datetime\n";
输出:
2003-07-18T12:00:00
在上面的输出中,我们没有在第二和第三部分传递任何细节。因此,Perl假定它为零。
该函数的工作方式与localtime()函数类似,只是gmtime()返回值已针对标准格林威治时区进行本地化。
#!/usr/local/bin/perl
$gmt = gmtime();
print "$gmt\n";
$local = localtime();
print "$local\n";
输出:
Fri Jan 6 08:43:31 2017
Fri Jan 6 14:13:31 2017
纪元时间是指特定日期和时间之后的秒数。此特定日期和时间因不同的操作系统而异。例如,对于Unix,它是1970年1月1日。由于所有操作系统具有不同的纪元时间,因此您不能假定一个系统的纪元时间与另一个系统的纪元时间相同。
#!/usr/local/bin/perl
$epoch = time();
print "$epoch\n";
输出:
1483686914
Perl POSIX strftime()函数用于格式化日期和时间,并在说明符前加(%)号。说明符有两种类型,一种用于本地时间,另一种用于gmt时区。
Specifier | Used For | Example |
---|---|---|
%a | Represents weekday name in short | Sun |
%A | Represents full weekday name | Sunday |
%b | Represents month name in short | Jul |
%B | Represents full month name | July |
%c | Represents date and time | Fri Jan 6 12:34:07 2017 |
%h | Represents month name in short, same as %b | Jul |
%r | 12-hour format clock time | 6:15:30 pm |
%x | Represent date | 12/28/12 |
%X | Represent time | 15:34:06 |
%Z | Represents time zone |
Specifiers | Used For | Example |
---|---|---|
%C | Year divided by 100 and written in integers (00-99) | 34 |
%d | Day of the month, zero padded (01-31) | 33 |
%D | Represents MM/DD/YY | 07/18/17 |
%e | Day of the month, space padded (1-31) | 33 |
%F | Represents YYYY-MM-DD | 2017/07/18 |
%g | Week based year, last two digit (00-99) | 05 |
%g | Week based year | 2015 |
%H | Hour in 24 hours format | 17 |
%I | Hour in 12hours format | 05 |
%J | Day of year (001-366) | 365 |
%m | Month in decimal number (01-12) | 07 |
%M | Minute (00-59) | 35 |
%n | New line character | |
%p | AM or PM | AM |
%R | HH:MM time in 24 hour format | 17:55 |
%S | Secone (00-59) | 45 |
% t | Horizontal tab character | |
%T | ISO 8601 time format (HH:MM:SS) | 21:45 |
%u | ISO 8601 weekday as number starting with Monday (1-7) | |
%U | week number with first Sunday as first day of week one (00-53) | |
%V | ISO 8601week number (00-53) | |
%w | Weekday as decimal number starting with Sunday(0-6) | |
% W | Week number with first Monday as first day of week one (00-53) | 17 |
%y | Last two digits of a year (00-99) | 2017 |
% Y | Full year | |
%z | ISO 8601 offset from UTC in time zone (1min =1, 1 hour = 100) | +100 |
%% | A % sign | % |
有时我们需要以不同的格式显示日期或时间。为此,我们可以如下所示在Perl中更改格式。
例:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use DateTime;
my $datetime = DateTime->new(
day => 18,
month => 7,
year => 2003,
hour => 12,
minute => 00,
second => 00,
);
say $datetime;
say $datetime->ymd;
say $datetime->ymd('_');
say $datetime->hms;
say $datetime->epoch;
say $datetime->year;
say $datetime->month;
say $datetime->day;
say $datetime->strftime( '%Y-%m-%d-%H-%M-%S' );
输出:
2003-07-18T12:00:00
2003-07-18
18-07-2003
12:00:00
1058529600
2003
7
18
2003-07-18-12-00-00