| 1 | package Mojo::Date; | ||||||||||||||||||||||||||||
| 2 | 29 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use Time::Local qw(timegm); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 168 | ▶ | has epoch => sub {time}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 29 | my $RFC3339_RE = qr/ | |||||||||||||||||||||||||||
| 10 | ^(\d+)-(\d+)-(\d+)\D+(\d+):(\d+):(\d+(?:\.\d+)?) # Date and time | ||||||||||||||||||||||||||||
| 11 | (?:Z|([+-])(\d+):(\d+))?$ # Offset | ||||||||||||||||||||||||||||
| 12 | /xi; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 29 | my @DAYS = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||||||||||||||||||||
| 15 | 29 | my @MONTHS = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||||||||||
| 16 | 29 | my %MONTHS; | |||||||||||||||||||||||||||
| 17 | 29 | @MONTHS{@MONTHS} = (0 .. 11); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 267 | ▶ | sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 99 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 99 | my ($self, $date) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # epoch (784111777) | ||||||||||||||||||||||||||||
| 25 | 99 | ▶ | return $self->epoch($date) if $date =~ /^\d+$|^\d+\.\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT) | ||||||||||||||||||||||||||||
| 28 | # RFC 850/1036 (Sunday, 06-Nov-94 08:49:37 GMT) | ||||||||||||||||||||||||||||
| 29 | 59 | my $offset = 0; | |||||||||||||||||||||||||||
| 30 | 59 | my ($day, $month, $year, $h, $m, $s); | |||||||||||||||||||||||||||
| 31 | 59 | ▶ | if ($date =~ /^\w+\W+(\d+)\W+(\w+)\W+(\d+)\W+(\d+):(\d+):(\d+)\W*\w+$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 32 | 38 | ($day, $month, $year, $h, $m, $s) = ($1, $MONTHS{$2}, $3, $4, $5, $6); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # RFC 3339 (1994-11-06T08:49:37Z) | ||||||||||||||||||||||||||||
| 36 | elsif ($date =~ $RFC3339_RE) { | ||||||||||||||||||||||||||||
| 37 | 10 | ($year, $month, $day, $h, $m, $s) = ($1, $2 - 1, $3, $4, $5, $6); | |||||||||||||||||||||||||||
| 38 | 10 | ▶ | $offset = (($8 * 3600) + ($9 * 60)) * ($7 eq '+' ? -1 : 1) if $7; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # ANSI C asctime() (Sun Nov 6 08:49:37 1994) | ||||||||||||||||||||||||||||
| 42 | elsif ($date =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)$/) { | ||||||||||||||||||||||||||||
| 43 | 2 | ($month, $day, $h, $m, $s, $year) = ($MONTHS{$1}, $2, $3, $4, $5, $6); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Invalid | ||||||||||||||||||||||||||||
| 47 | 9 | else { return $self->epoch(undef) } | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 50 | my $epoch = eval { timegm $s, $m, $h, $day, $month, $year }; | |||||||||||||||||||||||||||
| 50 | 50 | ▶ | return $self->epoch((defined $epoch && ($epoch += $offset) >= 0) ? $epoch : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $epoch and ($epoch += $offset) >= 0
MC/DC100%2/2
defined $epoch and ($epoch += $offset) >= 0
defined $epoch
($epoch += $offset) >= 0
Truth table100%3/3
A and B
A= defined $epochB= ($epoch += $offset) >= 0
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 5 | ▶ | sub to_datetime { | ||||||||||||||||||||||||||
|
Subroutine
to_datetime: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # RFC 3339 (1994-11-06T08:49:37Z) | ||||||||||||||||||||||||||||
| 56 | 5 | my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift->epoch); | |||||||||||||||||||||||||||
| 57 | 5 | my $str = sprintf '%04d-%02d-%02dT%02d:%02d:%02d', $year + 1900, $month + 1, $day, $h, $m, $s; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | return $str . ($epoch =~ /(\.\d+)$/ ? $1 : '') . 'Z'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 203 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # RFC 7231 (Sun, 06 Nov 1994 08:49:37 GMT) | ||||||||||||||||||||||||||||
| 64 | 203 | my ($s, $m, $h, $mday, $month, $year, $wday) = gmtime shift->epoch; | |||||||||||||||||||||||||||
| 65 | 203 | return sprintf '%s, %02d %s %04d %02d:%02d:%02d GMT', $DAYS[$wday], $mday, $MONTHS[$month], $year + 1900, $h, $m, $s; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 29 | 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =encoding utf8 | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 NAME | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Mojo::Date - HTTP date | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | use Mojo::Date; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Parse | ||||||||||||||||||||||||||||
| 81 | my $date = Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT'); | ||||||||||||||||||||||||||||
| 82 | say $date->epoch; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Build | ||||||||||||||||||||||||||||
| 85 | my $date = Mojo::Date->new(time + 60); | ||||||||||||||||||||||||||||
| 86 | say "$date"; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | L<Mojo::Date> implements HTTP date and time functions, based on L<RFC 7230|https://tools.ietf.org/html/rfc7230>, L<RFC | ||||||||||||||||||||||||||||
| 91 | 7231|https://tools.ietf.org/html/rfc7231> and L<RFC 3339|https://tools.ietf.org/html/rfc3339>. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | L<Mojo::Date> implements the following attributes. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 epoch | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | my $epoch = $date->epoch; | ||||||||||||||||||||||||||||
| 100 | $date = $date->epoch(784111777); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Epoch seconds, defaults to the current time. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 METHODS | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | L<Mojo::Date> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 new | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $date = Mojo::Date->new; | ||||||||||||||||||||||||||||
| 111 | my $date = Mojo::Date->new('Sun Nov 6 08:49:37 1994'); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Construct a new L<Mojo::Date> object and L</"parse"> date if necessary. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 parse | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | $date = $date->parse('Sun Nov 6 08:49:37 1994'); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Parse date. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Epoch | ||||||||||||||||||||||||||||
| 122 | say Mojo::Date->new('784111777')->epoch; | ||||||||||||||||||||||||||||
| 123 | say Mojo::Date->new('784111777.21')->epoch; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # RFC 822/1123 | ||||||||||||||||||||||||||||
| 126 | say Mojo::Date->new('Sun, 06 Nov 1994 08:49:37 GMT')->epoch; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # RFC 850/1036 | ||||||||||||||||||||||||||||
| 129 | say Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Ansi C asctime() | ||||||||||||||||||||||||||||
| 132 | say Mojo::Date->new('Sun Nov 6 08:49:37 1994')->epoch; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # RFC 3339 | ||||||||||||||||||||||||||||
| 135 | say Mojo::Date->new('1994-11-06T08:49:37Z')->epoch; | ||||||||||||||||||||||||||||
| 136 | say Mojo::Date->new('1994-11-06T08:49:37')->epoch; | ||||||||||||||||||||||||||||
| 137 | say Mojo::Date->new('1994-11-06T08:49:37.21Z')->epoch; | ||||||||||||||||||||||||||||
| 138 | say Mojo::Date->new('1994-11-06T08:49:37+01:00')->epoch; | ||||||||||||||||||||||||||||
| 139 | say Mojo::Date->new('1994-11-06T08:49:37-01:00')->epoch; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 to_datetime | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | my $str = $date->to_datetime; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Render L<RFC 3339|https://tools.ietf.org/html/rfc3339> date and time. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # "1994-11-06T08:49:37Z" | ||||||||||||||||||||||||||||
| 148 | Mojo::Date->new(784111777)->to_datetime; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # "1994-11-06T08:49:37.21Z" | ||||||||||||||||||||||||||||
| 151 | Mojo::Date->new(784111777.21)->to_datetime; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 to_string | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | my $str = $date->to_string; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Render date suitable for HTTP messages. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # "Sun, 06 Nov 1994 08:49:37 GMT" | ||||||||||||||||||||||||||||
| 160 | Mojo::Date->new(784111777)->to_string; | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 OPERATORS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | L<Mojo::Date> overloads the following operators. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 bool | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | my $bool = !!$date; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Always true. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 stringify | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $str = "$date"; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Alias for L</"to_string">. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||