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