File Coverage

blib/lib/Net/GPSD3/Return/Unknown/Timestamp.pm
Criterion Covered Total %
statement 33 39 84.6
branch 11 24 45.8
condition 4 11 36.3
subroutine 8 8 100.0
pod 3 3 100.0
total 59 85 69.4


line stmt bran cond sub pod time code
1             package Net::GPSD3::Return::Unknown::Timestamp;
2 5     5   35 use strict;
  5         7  
  5         174  
3 5     5   26 use warnings;
  5         9  
  5         136  
4 5     5   45 use base qw{Net::GPSD3::Return::Unknown};
  5         9  
  5         449  
5 5     5   4667 use DateTime::Format::W3CDTF;
  5         4789  
  5         148  
6 5     5   31 use DateTime;
  5         11  
  5         2634  
7              
8             our $VERSION='0.18';
9              
10             =head1 NAME
11              
12             Net::GPSD3::Return::Unknown::Timestamp - Net::GPSD3 Return Base Class with Timestamp
13              
14             =head1 SYNOPSIS
15              
16             package XXX;
17             use base qw{Net::GPSD3::Return::Unknown::Timestamp};
18              
19             =head1 DESCRIPTION
20              
21             Provides a time, timestamp and datetime methods to a GPSD3 Return object.
22              
23             =head1 METHODS
24              
25             =head2 time
26              
27             Seconds since the Unix epoch, UTC. The value may have a fractional part of up to .01sec precision.
28              
29             Note: In 2.96 (protocol 3.4) the TPV->time format changed from unix epoch to W3C, but this method attempts to hide that change from the user.
30              
31             Since the POSIX standard for the Unix epoch does not use leap seconds but GPS system does I do not recommend that you use this method for time display or storage. This method is purely here for backwards compatibility.
32              
33             =cut
34              
35             sub time {
36 3     3 1 6 my $self=shift;
37             #protocol < 3.4
38 3 50       31 $self->timestamp unless defined $self->{"_time"};
39 3 50       28 $self->{"_time"}=$self->datetime->hires_epoch unless defined $self->{"_time"};
40 3         40 return $self->{"_time"};
41             }
42              
43             =head2 timestamp
44              
45             W3C formated timestamp value either directly from the protocol >= 3.4 or calculated < 3.4. The value may have a fractional part of up to .01sec precision.
46              
47             Note: I expect that in protocol 3.5 the value will be passed directly as TPV->timestamp
48              
49             =cut
50              
51             sub timestamp {
52 7     7 1 26 my $self=shift;
53 7 100       22 unless (defined $self->{"_timestamp"}) {
54 3         15 my $qr=qr/\A\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?Z\Z/;
55 3 100 66     53 if (defined($self->{"timestamp"}) and $self->{"timestamp"} =~ $qr) {
    50 33        
    0 0        
56             #protocol 3.5 (expected)
57 2         10 $self->{"_timestamp"}=$self->{"timestamp"}
58             } elsif (defined($self->{"time"}) and $self->{"time"} =~ $qr) {
59             #protocol 3.4
60 1         6 $self->{"_timestamp"}=$self->{"time"};
61             } elsif (defined($self->{"time"}) and $self->{"time"} =~ m/^\d{1,}(?:\.\d*)?$/) {
62             #protocol < 3.4
63 0 0       0 $self->{"_time"}=$self->{"time"} unless defined $self->{"_time"};
64 0         0 $self->{"_timestamp"}=$self->datetime->strftime(q{%FT%T.%2NZ}); #%2N truncates should round DateTime 0.66
65             } else {
66 0         0 die("Error: Either TPV->timestamp or TPV->time must be defined.");
67             }
68             }
69 7         21 return $self->{"_timestamp"};
70             }
71              
72             =head2 datetime
73              
74             Returns a L object
75              
76             =cut
77              
78             sub datetime {
79 11     11 1 423 my $self=shift;
80 11 100       37 unless (defined($self->{"datetime"})) {
81 3   50     19 my $timestamp=$self->{"_timestamp"} ||
82             $self->{"timestamp"} || #protocol >= 3.5 (expected)
83             undef;
84 3 50       8 if (defined $timestamp) {
    0          
    0          
85 3         21 $self->{"datetime"}=DateTime::Format::W3CDTF->new->parse_datetime($timestamp);
86 3 50       1585 $self->{"_time"}=$self->datetime->hires_epoch unless defined $self->{"_time"};
87             } elsif (defined $self->{"_time"}) {
88 0         0 $self->{"datetime"}=DateTime->from_epoch(epoch=>$self->{"_time"}, time_zone=>"UTC");
89             } elsif ($self->timestamp) { #infinate loop potential
90 0         0 $self->{"datetime"}=DateTime::Format::W3CDTF->new->parse_datetime($self->timestamp);
91             } else {
92 0         0 die("Error: Either TPV->timestamp or TPV->time must be defined.");
93             }
94             }
95 11         107 return $self->{"datetime"};
96             }
97              
98             =head1 BUGS
99              
100             Log on RT and Send to gpsd-dev email list
101              
102             =head1 SUPPORT
103              
104             DavisNetworks.com supports all Perl applications including this package.
105              
106             Try gpsd-dev email list
107              
108             =head1 AUTHOR
109              
110             Michael R. Davis
111             CPAN ID: MRDVT
112             STOP, LLC
113             domain=>michaelrdavis,tld=>com,account=>perl
114             http://www.stopllc.com/
115              
116             =head1 COPYRIGHT
117              
118             This program is free software licensed under the...
119              
120             The BSD License
121              
122             The full text of the license can be found in the LICENSE file included with this module.
123              
124             =head1 SEE ALSO
125              
126             L, L, L, L
127              
128             =cut
129              
130             1;