| 1 | 71 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 71 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package BSON::Time; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: BSON type wrapper for date and time | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 71 | our $VERSION = 'v1.12.2'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 71 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 71 | ▶ | use Time::HiRes qw/time/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 71 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 71 | ▶ | use if !$Config{use64bitint}, 'Math::BigInt'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 71 | ▶ | use if !$Config{use64bitint}, 'Math::BigFloat'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 71 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #pod =attr value | ||||||||||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||||||||||
| 23 | #pod A integer representing milliseconds since the Unix epoch. The default | ||||||||||||||||||||||||||||
| 24 | #pod is 0. | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 71 | has 'value' => ( | |||||||||||||||||||||||||||
| 29 | is => 'ro' | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 71 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 17656 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=15 SCAR=37.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 17656 | my $class = shift; | |||||||||||||||||||||||||||
| 36 | 17656 | my $n = scalar(@_); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 17656 | my %args; | |||||||||||||||||||||||||||
| 39 | 17656 | ▶ | if ( $n == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | if ( $Config{use64bitint} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 6 | $args{value} = time() * 1000; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 0 | $args{value} = Math::BigFloat->new(time()); | |||||||||||||||||||||||||||
| 45 | 0 | $args{value}->bmul(1000); | |||||||||||||||||||||||||||
| 46 | 0 | $args{value} = $args{value}->as_number('zero'); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif ( $n == 1 ) { | ||||||||||||||||||||||||||||
| 50 | 8918 | ▶ | croak "argument to BSON::Time::new must be epoch seconds, not '$_[0]'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | unless looks_like_number( $_[0] ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 8917 | ▶ | if ( !$Config{use64bitint} && ref($args{value}) ne 'Math::BigInt' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
MC/DC0%0/2
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
not $BSON::Time::Config{"use64bitint"}
ref $args{"value"} ne "Math::BigInt"
Truth table33%1/3
not(A) and B
A= not $BSON::Time::Config{"use64bitint"}B= ref $args{"value"} ne "Math::BigInt"
| |||||||||||||||||||||||||||||
| 54 | 0 | $args{value} = Math::BigFloat->new(shift); | |||||||||||||||||||||||||||
| 55 | 0 | $args{value}->bmul(1000); | |||||||||||||||||||||||||||
| 56 | 0 | $args{value} = $args{value}->as_number('zero'); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 8917 | $args{value} = 1000 * shift; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | elsif ( $n % 2 == 0 ) { | ||||||||||||||||||||||||||||
| 63 | 8732 | %args = @_; | |||||||||||||||||||||||||||
| 64 | 8732 | ▶ | if ( defined $args{value} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | croak "argument to BSON::Time::new must be epoch seconds, not '$args{value}'" | ||||||||||||||||||||||||||||
| 66 | 8732 | ▶ | unless looks_like_number( $args{value} ) || overload::Overloaded($args{value}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($args{"value"}) or overload::Overloaded($args{"value"})
MC/DC0%0/2
&looks_like_number($args{"value"}) or overload::Overloaded($args{"value"})
&looks_like_number($args{"value"})
overload::Overloaded($args{"value"})
Truth table33%1/3
A or B
A= &looks_like_number($args{"value"})B= overload::Overloaded($args{"value"})
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8732 | ▶ | if ( !$Config{use64bitint} && ref($args{value}) ne 'Math::BigInt' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
MC/DC0%0/2
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
not $BSON::Time::Config{"use64bitint"}
ref $args{"value"} ne "Math::BigInt"
Truth table33%1/3
not(A) and B
A= not $BSON::Time::Config{"use64bitint"}B= ref $args{"value"} ne "Math::BigInt"
| |||||||||||||||||||||||||||||
| 69 | 0 | $args{value} = Math::BigInt->new($args{value}); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( !$Config{use64bitint} && ref($args{value}) ne 'Math::BigInt' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
MC/DC0%0/2
not $BSON::Time::Config{"use64bitint"} and ref $args{"value"} ne "Math::BigInt"
not $BSON::Time::Config{"use64bitint"}
ref $args{"value"} ne "Math::BigInt"
Truth table0%0/3
not(A) and B
A= not $BSON::Time::Config{"use64bitint"}B= ref $args{"value"} ne "Math::BigInt"
| |||||||||||||||||||||||||||||
| 74 | 0 | $args{value} = Math::BigFloat->new(shift); | |||||||||||||||||||||||||||
| 75 | 0 | $args{value}->bmul(1000); | |||||||||||||||||||||||||||
| 76 | 0 | $args{value} = $args{value}->as_number('zero'); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | $args{value} = 1000 * shift; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 0 | croak("Invalid number of arguments ($n) to BSON::Time::new"); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # normalize all to integer ms | ||||||||||||||||||||||||||||
| 88 | 17655 | $args{value} = int( $args{value} ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 17655 | return \%args; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #pod =method epoch | ||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||
| 95 | #pod Returns the number of seconds since the epoch (i.e. a floating-point value). | ||||||||||||||||||||||||||||
| 96 | #pod | ||||||||||||||||||||||||||||
| 97 | #pod =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 17400 | ▶ | sub epoch { | ||||||||||||||||||||||||||
|
Subroutine
epoch: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 17400 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 17400 | ▶ | if ( $Config{use64bitint} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 17400 | return $self->value / 1000; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | 0 | require Math::BigFloat; | |||||||||||||||||||||||||||
| 106 | 0 | my $upgrade = Math::BigFloat->new($self->value->bstr); | |||||||||||||||||||||||||||
| 107 | 0 | return 0 + $upgrade->bdiv(1000)->bstr; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | #pod =method as_iso8601 | ||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||
| 113 | #pod Returns the C<value> as an ISO-8601 formatted string of the form | ||||||||||||||||||||||||||||
| 114 | #pod C<YYYY-MM-DDThh:mm:ss.sssZ>. The fractional seconds will be omitted if | ||||||||||||||||||||||||||||
| 115 | #pod they are zero. | ||||||||||||||||||||||||||||
| 116 | #pod | ||||||||||||||||||||||||||||
| 117 | #pod =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | ▶ | sub as_iso8601 { | ||||||||||||||||||||||||||
|
Subroutine
as_iso8601: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 6 | my ($s, $m, $h, $D, $M, $Y) = gmtime($self->epoch); | |||||||||||||||||||||||||||
| 122 | 6 | $M++; | |||||||||||||||||||||||||||
| 123 | 6 | $Y+=1900; | |||||||||||||||||||||||||||
| 124 | 6 | my $f = $self->{value} % 1000; | |||||||||||||||||||||||||||
| 125 | 6 | ▶ | return $f | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | ? sprintf( "%4d-%02d-%02dT%02d:%02d:%02d.%03dZ", $Y, $M, $D, $h, $m, $s, $f ) | ||||||||||||||||||||||||||||
| 127 | : sprintf( "%4d-%02d-%02dT%02d:%02d:%02dZ", $Y, $M, $D, $h, $m, $s ); | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | #pod =method as_datetime | ||||||||||||||||||||||||||||
| 131 | #pod | ||||||||||||||||||||||||||||
| 132 | #pod Loads L<DateTime> and returns the C<value> as a L<DateTime> object. | ||||||||||||||||||||||||||||
| 133 | #pod | ||||||||||||||||||||||||||||
| 134 | #pod =cut | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub as_datetime { | ||||||||||||||||||||||||||
|
Subroutine
as_datetime: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 0 | require DateTime; | |||||||||||||||||||||||||||
| 138 | 0 | return DateTime->from_epoch( epoch => $_[0]->{value} / 1000 ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | #pod =method as_datetime_tiny | ||||||||||||||||||||||||||||
| 142 | #pod | ||||||||||||||||||||||||||||
| 143 | #pod Loads L<DateTime::Tiny> and returns the C<value> as a L<DateTime::Tiny> | ||||||||||||||||||||||||||||
| 144 | #pod object. | ||||||||||||||||||||||||||||
| 145 | #pod | ||||||||||||||||||||||||||||
| 146 | #pod =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub as_datetime_tiny { | ||||||||||||||||||||||||||
|
Subroutine
as_datetime_tiny: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 0 | my ($s, $m, $h, $D, $M, $Y) = gmtime($_[0]->epoch); | |||||||||||||||||||||||||||
| 150 | 0 | $M++; | |||||||||||||||||||||||||||
| 151 | 0 | $Y+=1900; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | require DateTime::Tiny; | |||||||||||||||||||||||||||
| 154 | 0 | return DateTime::Tiny->new( | |||||||||||||||||||||||||||
| 155 | year => $Y, month => $M, day => $D, | ||||||||||||||||||||||||||||
| 156 | hour => $h, minute => $m, second => $s | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #pod =method as_mango_time | ||||||||||||||||||||||||||||
| 161 | #pod | ||||||||||||||||||||||||||||
| 162 | #pod Loads L<Mango::BSON::Time> and returns the C<value> as a L<Mango::BSON::Time> | ||||||||||||||||||||||||||||
| 163 | #pod object. | ||||||||||||||||||||||||||||
| 164 | #pod | ||||||||||||||||||||||||||||
| 165 | #pod =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub as_mango_time { | ||||||||||||||||||||||||||
|
Subroutine
as_mango_time: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 0 | require Mango::BSON::Time; | |||||||||||||||||||||||||||
| 169 | 0 | return Mango::BSON::Time->new( $_[0]->{value} ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | #pod =method as_time_moment | ||||||||||||||||||||||||||||
| 173 | #pod | ||||||||||||||||||||||||||||
| 174 | #pod Loads L<Time::Moment> and returns the C<value> as a L<Time::Moment> object. | ||||||||||||||||||||||||||||
| 175 | #pod | ||||||||||||||||||||||||||||
| 176 | #pod =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub as_time_moment { | ||||||||||||||||||||||||||
|
Subroutine
as_time_moment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 0 | require Time::Moment; | |||||||||||||||||||||||||||
| 180 | 0 | return Time::Moment->from_epoch( $_[0]->{value} / 1000 ); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 2 | ▶ | sub _num_cmp { | ||||||||||||||||||||||||||
|
Subroutine
_num_cmp: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 184 | 2 | my ( $self, $other ) = @_; | |||||||||||||||||||||||||||
| 185 | 2 | ▶ | if ( ref($other) eq ref($self) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | return $self->{value} <=> $other->{value}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 2 | return 0+ $self <=> 0+ $other; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | ▶ | sub _str_cmp { | ||||||||||||||||||||||||||
|
Subroutine
_str_cmp: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 192 | 2 | my ( $self, $other ) = @_; | |||||||||||||||||||||||||||
| 193 | 2 | ▶ | if ( ref($other) eq ref($self) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 1 | return $self->{value} cmp $other->{value}; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 1 | return "$self" cmp "$other"; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub op_eq { | ||||||||||||||||||||||||||
|
Subroutine
op_eq: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 200 | 0 | my ( $self, $other ) = @_; | |||||||||||||||||||||||||||
| 201 | 0 | return( ($self <=> $other) == 0 ); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 71 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 71 | q{""} => \&epoch, | |||||||||||||||||||||||||||
| 206 | q{0+} => \&epoch, | ||||||||||||||||||||||||||||
| 207 | q{<=>} => \&_num_cmp, | ||||||||||||||||||||||||||||
| 208 | q{cmp} => \&_str_cmp, | ||||||||||||||||||||||||||||
| 209 | fallback => 1, | ||||||||||||||||||||||||||||
| 210 | 71 | ); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | #pod =method TO_JSON | ||||||||||||||||||||||||||||
| 213 | #pod | ||||||||||||||||||||||||||||
| 214 | #pod Returns a string formatted by L</as_iso8601>. | ||||||||||||||||||||||||||||
| 215 | #pod | ||||||||||||||||||||||||||||
| 216 | #pod If the C<BSON_EXTJSON> option is true, it will instead be compatible with | ||||||||||||||||||||||||||||
| 217 | #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 218 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 219 | #pod | ||||||||||||||||||||||||||||
| 220 | #pod | ||||||||||||||||||||||||||||
| 221 | #pod If the C<BSON_EXTJSON> environment variable is true and the | ||||||||||||||||||||||||||||
| 222 | #pod C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref | ||||||||||||||||||||||||||||
| 223 | #pod compatible with | ||||||||||||||||||||||||||||
| 224 | #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 225 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 226 | #pod | ||||||||||||||||||||||||||||
| 227 | #pod {"$date" : { "$numberLong": "22337203685477580" } } | ||||||||||||||||||||||||||||
| 228 | #pod | ||||||||||||||||||||||||||||
| 229 | #pod If the C<BSON_EXTJSON> and C<BSON_EXTJSON_RELAXED> environment variables are | ||||||||||||||||||||||||||||
| 230 | #pod both true, then it will return a hashref with an ISO-8601 string for dates | ||||||||||||||||||||||||||||
| 231 | #pod after the Unix epoch and before the year 10,000 and a C<$numberLong> style | ||||||||||||||||||||||||||||
| 232 | #pod value otherwise. | ||||||||||||||||||||||||||||
| 233 | #pod | ||||||||||||||||||||||||||||
| 234 | #pod {"$date" : "2012-12-24T12:15:30.500Z"} | ||||||||||||||||||||||||||||
| 235 | #pod {"$date" : { "$numberLong": "-10000000" } } | ||||||||||||||||||||||||||||
| 236 | #pod | ||||||||||||||||||||||||||||
| 237 | #pod =cut | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 24 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 240 | return $_[0]->as_iso8601 | ||||||||||||||||||||||||||||
| 241 | 24 | ▶ | if ! $ENV{BSON_EXTJSON}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | return { '$date' => { '$numberLong' => "$_[0]->{value}"} } | ||||||||||||||||||||||||||||
| 244 | 22 | ▶ | if ! $ENV{BSON_EXTJSON_RELAXED}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Relaxed form is human readable for positive epoch to year 10k | ||||||||||||||||||||||||||||
| 247 | 6 | my $year = (gmtime($_[0]->epoch))[5]; | |||||||||||||||||||||||||||
| 248 | 6 | $year += 1900; | |||||||||||||||||||||||||||
| 249 | 6 | ▶ | if ($year >= 1970 and $year <= 9999) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$year >= 1970 and $year <= 9999
MC/DC50%1/2
$year >= 1970 and $year <= 9999
$year >= 1970
$year <= 9999
Truth table66%2/3
A and B
A= $year >= 1970B= $year <= 9999
| |||||||||||||||||||||||||||||
| 250 | 4 | return { '$date' => $_[0]->as_iso8601 }; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||
| 253 | 2 | return { '$date' => { '$numberLong' => "$_[0]->{value}" } }; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 71 | 1; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =pod | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =head1 NAME | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | BSON::Time - BSON type wrapper for date and time | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head1 VERSION | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | version v1.12.2 | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | use BSON::Types ':all'; | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | bson_time(); # now | ||||||||||||||||||||||||||||
| 276 | bson_time( $secs ); # floating point seconds since epoch | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | This module provides a BSON type wrapper for a 64-bit date-time value in | ||||||||||||||||||||||||||||
| 281 | the form of milliseconds since the Unix epoch (UTC only). | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | On a Perl without 64-bit integer support, the value must be a | ||||||||||||||||||||||||||||
| 284 | L<Math::BigInt> object. | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =head2 value | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | A integer representing milliseconds since the Unix epoch. The default | ||||||||||||||||||||||||||||
| 291 | is 0. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =head1 METHODS | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head2 epoch | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Returns the number of seconds since the epoch (i.e. a floating-point value). | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =head2 as_iso8601 | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Returns the C<value> as an ISO-8601 formatted string of the form | ||||||||||||||||||||||||||||
| 302 | C<YYYY-MM-DDThh:mm:ss.sssZ>. The fractional seconds will be omitted if | ||||||||||||||||||||||||||||
| 303 | they are zero. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head2 as_datetime | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Loads L<DateTime> and returns the C<value> as a L<DateTime> object. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head2 as_datetime_tiny | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Loads L<DateTime::Tiny> and returns the C<value> as a L<DateTime::Tiny> | ||||||||||||||||||||||||||||
| 312 | object. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head2 as_mango_time | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Loads L<Mango::BSON::Time> and returns the C<value> as a L<Mango::BSON::Time> | ||||||||||||||||||||||||||||
| 317 | object. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head2 as_time_moment | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Loads L<Time::Moment> and returns the C<value> as a L<Time::Moment> object. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Returns a string formatted by L</as_iso8601>. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | If the C<BSON_EXTJSON> option is true, it will instead be compatible with | ||||||||||||||||||||||||||||
| 328 | MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 329 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | If the C<BSON_EXTJSON> environment variable is true and the | ||||||||||||||||||||||||||||
| 332 | C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref | ||||||||||||||||||||||||||||
| 333 | compatible with | ||||||||||||||||||||||||||||
| 334 | MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 335 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | {"$date" : { "$numberLong": "22337203685477580" } } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | If the C<BSON_EXTJSON> and C<BSON_EXTJSON_RELAXED> environment variables are | ||||||||||||||||||||||||||||
| 340 | both true, then it will return a hashref with an ISO-8601 string for dates | ||||||||||||||||||||||||||||
| 341 | after the Unix epoch and before the year 10,000 and a C<$numberLong> style | ||||||||||||||||||||||||||||
| 342 | value otherwise. | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | {"$date" : "2012-12-24T12:15:30.500Z"} | ||||||||||||||||||||||||||||
| 345 | {"$date" : { "$numberLong": "-10000000" } } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =for Pod::Coverage op_eq BUILDARGS | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 OVERLOADING | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Both numification (C<0+>) and stringification (C<"">) are overloaded to | ||||||||||||||||||||||||||||
| 352 | return the result of L</epoch>. Numeric comparison and string comparison | ||||||||||||||||||||||||||||
| 353 | are overloaded based on those and fallback overloading is enabled. | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =over 4 | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =item * | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | David Golden <david@mongodb.com> | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =item * | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Stefan G. <minimalist@lavabit.com> | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =back | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc. | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =cut | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | __END__ | ||||||||||||||||||||||||||||