Skip to content

Instantly share code, notes, and snippets.

/precision.diff Secret

Created August 23, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2ac7896cd82c10dd9d87 to your computer and use it in GitHub Desktop.
Save anonymous/2ac7896cd82c10dd9d87 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Date.pm b/lib/Mojo/Date.pm
index 015d83c..e0af0b0 100644
--- a/lib/Mojo/Date.pm
+++ b/lib/Mojo/Date.pm
@@ -7,7 +7,7 @@ use Time::Local 'timegm';
has 'epoch';
my $RFC3339_RE = qr/
- ^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.\d+)? # Date and time
+ ^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+(?:\.\d+)?) # Date and time
(?:Z|([+-])(\d+):(\d+))?$ # Offset
/xi;
@@ -22,7 +22,7 @@ sub parse {
my ($self, $date) = @_;
# epoch (784111777)
- return $self->epoch($date) if $date =~ /^\d+$/;
+ return $self->epoch($date) if $date =~ /^\d+$|^\d+\.\d+$/;
# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
my $offset = 0;
@@ -59,9 +59,10 @@ sub parse {
sub to_datetime {
# RFC 3339 (1994-11-06T08:49:37Z)
- my ($s, $m, $h, $day, $month, $year) = gmtime(shift->epoch // time);
- return sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ', $year + 1900, $month + 1,
+ my ($s, $m, $h, $day, $month, $year) = gmtime(my $e = shift->epoch // time);
+ my $str = sprintf '%04d-%02d-%02dT%02d:%02d:%02d', $year + 1900, $month + 1,
$day, $h, $m, $s;
+ return $e =~ /^\d+\.(\d+)$/ ? "$str.$1Z" : "${str}Z";
}
sub to_string {
@@ -130,6 +131,7 @@ Parse date.
# 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;
@@ -154,7 +156,10 @@ Parse date.
Render L<RFC 3339|http://tools.ietf.org/html/rfc3339> date and time.
# "1994-11-06T08:49:37Z"
- Mojo::Date->new(784111777)->to_datetime;
+ Mojo::Date->new('784111777')->to_datetime;
+
+ # "1994-11-06T08:49:37.21Z"
+ Mojo::Date->new('784111777.21')->to_datetime;
=head2 to_string
@@ -163,7 +168,7 @@ Render L<RFC 3339|http://tools.ietf.org/html/rfc3339> date a
Render date suitable for HTTP messages.
# "Sun, 06 Nov 1994 08:49:37 GMT"
- Mojo::Date->new(784111777)->to_string;
+ Mojo::Date->new('784111777')->to_string;
=head1 OPERATORS
diff --git a/t/mojo/date.t b/t/mojo/date.t
index 2991bd6..d749a20 100644
--- a/t/mojo/date.t
+++ b/t/mojo/date.t
@@ -15,7 +15,7 @@ is(Mojo::Date->new('2014-08-20T20:45:00')->epoch,
is(Mojo::Date->new(1408567500)->to_datetime,
'2014-08-20T20:45:00Z', 'right format');
is(Mojo::Date->new('2014-08-20T20:45:00.01')->epoch,
- 1408567500, 'right epoch value');
+ 1408567500.01, 'right epoch value');
is(Mojo::Date->new('2014-08-20T20:45:00-00:46')->epoch,
1408570260, 'right epoch value');
is(Mojo::Date->new(1408570260)->to_datetime,
@@ -29,9 +29,9 @@ is(Mojo::Date->new(1408561140)->to_datetime,
is(Mojo::Date->new('1994-11-06T08:49:37Z')->epoch,
784111777, 'right epoch value');
is(Mojo::Date->new('1994-11-06t08:49:37.33z')->epoch,
- 784111777, 'right epoch value');
-is(Mojo::Date->new(784111777)->to_datetime,
- '1994-11-06T08:49:37Z', 'right format');
+ 784111777.33, 'right epoch value');
+is(Mojo::Date->new(784111777.33)->to_datetime,
+ '1994-11-06T08:49:37.33Z', 'right format');
# RFC 850/1036
is(Mojo::Date->new('Sunday, 06-Nov-94 08:49:37 GMT')->epoch,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment