简介
use Mojo::Date;
# Parse
my $date = Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT');
say $date->epoch;
# Build
my $date = Mojo::Date->new(time + 60);
say "$date";
Mojo :: Date实现基于RFC 7230,RFC 7231和RFC 3339的 HTTP日期和时间函数。
属性
Mojo::Date 仅实现了一个属性epoch。
my $epoch = $date->epoch;
$date = $date->epoch(784111777);
方法
Mojo :: Date继承Mojo :: Base中的所有方法,并实现以下方法。
new
$date = Mojo::Date->new;
my $date = Mojo::Date->new('Sun Nov 6 08:49:37 1994');
构建一个新的Mojo::Date对象,如果传了参数,则把参数当成时间进行解析。
parse
$date = $date->parse('Sun Nov 6 08:49:37 1994');
解析日期。
# Epoch
say Mojo::Date->new('784111777')->epoch;
say Mojo::Date->new('784111777.21')->epoch;
# RFC 822/1123
say Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT')->epoch;
# RFC 850/1036
say Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch;
# Ansi C asctime()
say Mojo::Date->new('Sun Nov 6 08:49:37 1994')->epoch;
# RFC 3339
say Mojo::Date->new('1994-11-06T08:49:37Z')->epoch;
say Mojo::Date->new('1994-11-06T08:49:37')->epoch;
say Mojo::Date->new('1994-11-06T08:49:37.21Z')->epoch;
say Mojo::Date->new('1994-11-06T08:49:37+01:00')->epoch;
say Mojo::Date->new('1994-11-06T08:49:37-01:00')->epoch;
to_datetime
my $str = $date->to_datetime;
根据当前对象的值,返回一个RFC 3339格式的日期时间。
# "1994-11-06T08:49:37Z"
Mojo::Date->new(784111777)->to_datetime;
# "1994-11-06T08:49:37.21Z"
Mojo::Date->new(784111777.21)->to_datetime;
to_string
my $str = $date->to_string;
返回一个用于HTTP消息的时间。
# "Sun, 06 Nov 1994 08:49:37 GMT"
Mojo::Date->new(784111777)->to_string;
重载的运算符
bool
my $bool = !!$date;
永为true。
stringify
my $str = "$date";
to_string方法的别名。