File Coverage

blib/lib/BSON/Timestamp.pm
Criterion Covered Total %
statement 62 62 100.0
branch 17 20 85.0
condition 16 22 72.7
subroutine 14 14 100.0
pod 1 3 33.3
total 110 121 90.9


line stmt bran cond sub pod time code
1 71     71   26684 use 5.010001;
  71         241  
2 71     71   336 use strict;
  71         132  
  71         1267  
3 71     71   313 use warnings;
  71         138  
  71         2242  
4              
5             package BSON::Timestamp;
6             # ABSTRACT: BSON type wrapper for timestamps
7              
8 71     71   329 use version;
  71         129  
  71         333  
9             our $VERSION = 'v1.12.0';
10              
11 71     71   5185 use Carp ();
  71         170  
  71         1498  
12 71     71   334 use Tie::IxHash;
  71         151  
  71         1596  
13              
14 71     71   351 use Moo;
  71         120  
  71         373  
15              
16             #pod =attr seconds
17             #pod
18             #pod A value representing seconds since the Unix epoch. The default is
19             #pod current value of C.
20             #pod
21             #pod =attr increment
22             #pod
23             #pod A numeric value to disambiguate timestamps in the same second. The
24             #pod default is 0.
25             #pod
26             #pod =cut
27              
28             has [qw/seconds increment/] => (
29             is => 'ro'
30             );
31              
32 71     71   21621 use namespace::clean -except => 'meta';
  71         147  
  71         437  
33              
34             my $max_int32 = 2147483647;
35             my $max_uint32 = 4_294_967_295;
36              
37             # Support back-compat 'sec' and inc' and legacy constructor shortcut
38             sub BUILDARGS {
39 16950     16950 0 417355 my ($class) = shift;
40              
41 16950         20638 my %args;
42 16950 100 100     67927 if ( @_ && $_[0] !~ /^[s|i]/ ) {
43 16908         32359 $args{seconds} = $_[0];
44 16908         22574 $args{increment} = $_[1];
45             }
46             else {
47 42 50       109 Carp::croak( __PACKAGE__ . "::new called with an odd number of elements\n" )
48             unless @_ % 2 == 0;
49              
50 42         110 %args = @_;
51 42 50 33     190 $args{seconds} = $args{sec} if exists $args{sec} && !exists $args{seconds};
52 42 50 33     99 $args{increment} = $args{inc} if exists $args{inc} && !exists $args{increment};
53             }
54              
55 16950 100       31269 $args{seconds} = time unless defined $args{seconds};
56 16950 100       28198 $args{increment} = 0 unless defined $args{increment};
57 16950         45639 $args{$_} = int( $args{$_} ) for qw/seconds increment/;
58              
59 16950         231207 return \%args;
60             }
61              
62             sub BUILD {
63 16950     16950 0 152537 my ($self) = @_;
64              
65 16950         25748 for my $attr (qw/seconds increment/) {
66 33898         66188 my $v = $self->$attr;
67 33898 100 100     98131 Carp::croak("BSON::Timestamp '$attr' must be uint32")
68             unless $v >= 0 && $v <= $max_uint32;
69             }
70              
71 16946         55836 return;
72             }
73              
74             # For backwards compatibility
75             {
76 71     71   38618 no warnings 'once';
  71         180  
  71         20221  
77             *sec = \&seconds;
78             *inc = \&increment;
79             }
80              
81             #pod =method TO_JSON
82             #pod
83             #pod If the C option is true, returns a hashref compatible with
84             #pod MongoDB's L
85             #pod format, which represents it as a document as follows:
86             #pod
87             #pod {"$timestamp" : { "t":, "i": }}
88             #pod
89             #pod If the C option is false, an error is thrown, as this value
90             #pod can't otherwise be represented in JSON.
91             #pod
92             #pod =cut
93              
94             sub TO_JSON {
95 11 100   11 1 178 if ( $ENV{BSON_EXTJSON} ) {
96 10         16 my %data;
97 10         34 tie %data, 'Tie::IxHash';
98 10         149 $data{t} = int($_[0]->{seconds});
99 10         153 $data{i} = int($_[0]->{increment});
100 10         124 return { '$timestamp' => \%data };
101             }
102              
103 1         152 Carp::croak( "The value '$_[0]' is illegal in JSON" );
104             }
105              
106             sub _cmp {
107 8     8   18 my ( $l, $r, $swap ) = @_;
108 8 100 66     28 if ( !defined($l) || !defined($r) ) {
109 3         309 Carp::carp "Use of uninitialized value in BSON::Timestamp comparison (<=>); will be treated as 0,0";
110             }
111 8   50     147 $l //= { seconds => 0, increment => 0 };
112 8   100     27 $r //= { seconds => 0, increment => 0 };
113 8 100       19 ($r, $l) = ($l, $r) if $swap;
114             my $cmp = ( $l->{seconds} <=> $r->{seconds} )
115 8   100     30 || ( $l->{increment} <=> $r->{increment} );
116 8         52 return $cmp;
117             }
118              
119             use overload (
120 71         482 '<=>' => \&_cmp,
121             fallback => 1,
122 71     71   491 );
  71         149  
123              
124             1;
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             BSON::Timestamp - BSON type wrapper for timestamps
133              
134             =head1 VERSION
135              
136             version v1.12.0
137              
138             =head1 SYNOPSIS
139              
140             use BSON::Types ':all';
141              
142             bson_timestamp( $seconds );
143             bson_timestamp( $seconds, $increment );
144              
145             =head1 DESCRIPTION
146              
147             This module provides a BSON type wrapper for a BSON timestamp value.
148              
149             Generally, it should not be used by end-users, but is provided for
150             backwards compatibility.
151              
152             =head1 ATTRIBUTES
153              
154             =head2 seconds
155              
156             A value representing seconds since the Unix epoch. The default is
157             current value of C.
158              
159             =head2 increment
160              
161             A numeric value to disambiguate timestamps in the same second. The
162             default is 0.
163              
164             =head1 METHODS
165              
166             =head2 TO_JSON
167              
168             If the C option is true, returns a hashref compatible with
169             MongoDB's L
170             format, which represents it as a document as follows:
171              
172             {"$timestamp" : { "t":, "i": }}
173              
174             If the C option is false, an error is thrown, as this value
175             can't otherwise be represented in JSON.
176              
177             =for Pod::Coverage BUILD BUILDARGS sec inc
178              
179             =head1 AUTHORS
180              
181             =over 4
182              
183             =item *
184              
185             David Golden
186              
187             =item *
188              
189             Stefan G.
190              
191             =back
192              
193             =head1 COPYRIGHT AND LICENSE
194              
195             This software is Copyright (c) 2019 by Stefan G. and MongoDB, Inc.
196              
197             This is free software, licensed under:
198              
199             The Apache License, Version 2.0, January 2004
200              
201             =cut
202              
203             __END__