Skip to content
This repository has been archived by the owner on Dec 11, 2017. It is now read-only.

Commit

Permalink
added to_datetime method to Mango::BSON::Time
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 22, 2014
1 parent f3c13c0 commit dc92baf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

1.06 2014-08-21
1.06 2014-08-22
- Added to_datetime method to Mango::BSON::Time.

1.05 2014-08-10
- Added read_preference attribute to Mango::Cursor.
Expand Down
2 changes: 1 addition & 1 deletion Makefile.PL
Expand Up @@ -26,6 +26,6 @@ WriteMakefile(
},
no_index => {directory => ['t']}
},
PREREQ_PM => {Mojolicious => '5.0'},
PREREQ_PM => {Mojolicious => '5.32'},
test => {TESTS => 't/*.t t/*/*.t'}
);
3 changes: 3 additions & 0 deletions lib/Mango/BSON.pm
Expand Up @@ -509,6 +509,9 @@ Pre-encoded BSON document.
Create new BSON element of the UTC datetime type with L<Mango::BSON::Time>,
defaults to milliseconds since the UNIX epoch.
# "2013-02-11T23:48:56Z"
bson_time(1360626536748)->to_datetime;
=head2 bson_true
my $true = bson_true;
Expand Down
9 changes: 9 additions & 0 deletions lib/Mango/BSON/Time.pm
Expand Up @@ -2,12 +2,15 @@ package Mango::BSON::Time;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Mojo::Date;
use Time::HiRes 'time';

sub new { shift->SUPER::new(time => shift // int(time * 1000)) }

sub TO_JSON { shift->to_string }

sub to_datetime { Mojo::Date->new->epoch(shift->to_epoch)->to_datetime }

sub to_epoch { shift->to_string / 1000 }

sub to_string { shift->{time} }
Expand Down Expand Up @@ -50,6 +53,12 @@ Construct a new L<Mango::BSON::Time> object.
Alias for L</"to_string">.
=head2 to_datetime
my $str = $time->to_datetime;
Convert time to L<RFC 3339|http://tools.ietf.org/html/rfc3339> date and time.
=head2 to_epoch
my $epoch = $time->to_epoch;
Expand Down
2 changes: 2 additions & 0 deletions t/bson.t
Expand Up @@ -55,6 +55,8 @@ is length int bson_time->to_epoch, length time, 'right length';
is substr(bson_time->to_epoch, 0, 5), substr(time, 0, 5), 'same start';
is bson_time(1360626536748), 1360626536748, 'right epoch milliseconds';
is bson_time(1360626536748)->to_epoch, 1360626536.748, 'right epoch seconds';
is bson_time(1360626536748)->to_datetime, '2013-02-11T23:48:56Z',
'right format';

# Empty document
my $bson = bson_encode {};
Expand Down

0 comments on commit dc92baf

Please sign in to comment.