blib/lib/DateTime/Format/JSON/MicrosoftDateFormat.pm

filter: Statementstmt 100% 36 / 36 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 55 / 55 CC 5 SCAR 6.9
CC
5
Cov
100%
import
3.0
parse_datetime
3.0
BEGIN
1.0
CRAP 2.0
↑ Summary
1 package DateTime::Format::JSON::MicrosoftDateFormat;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.05';
7 5 our $PACKAGE = __PACKAGE__;
8  
9 =head1 NAME
10  
11 DateTime::Format::JSON::MicrosoftDateFormat - Parse and format JSON MicrosoftDateFormat strings
12  
13 =head1 SYNOPSIS
14  
15 use DateTime::Format::JSON::MicrosoftDateFormat;
16  
17 my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new;
18 my $dt = $formatter->parse_datetime("/Date(1392089278000-0600)/"); #2014-02-10T21:27:58Z
19 my $dt = $formatter->parse_datetime("/Date(1392067678000)/"); #2014-02-10T21:27:58Z
20  
21 say $formatter->format_datetime($dt); #/Date(1392067678000)/
22  
23 =head2 The Perl One Liner
24  
25 perl -MDateTime::Format::JSON::MicrosoftDateFormat -e 'print DateTime::Format::JSON::MicrosoftDateFormat->new->parse_datetime(shift), "\n";' '/Date(1392606509000-0500)/'
26  
27 =head1 DESCRIPTION
28  
29 This module understands the JSON MicrosoftDateFormat date/time format. e.g. /Date(1392067678000)/
30  
31 =head1 USAGE
32  
33 Note: The usage of MicrosoftDateFormat is deprecated for new JSON services. Please use ISO 8601 for all new development.
34  
35 From RFC 7493 (The I-JSON Message Format):
36  
37 Protocols often contain data items that are designed to contain timestamps or time durations. It
38 is RECOMMENDED that all such data items be expressed as string values in ISO 8601 format,
39 as specified in RFC 3339, with the additional restrictions that uppercase rather than lowercase
40 letters be used, that the timezone be included not defaulted, and that optional trailing seconds
41 be included even when their value is "00". It is also RECOMMENDED that all data items
42 containing time durations conform to the "duration" production in Appendix A of RFC 3339, with
43 the same additional restrictions.
44  
45 =head2 import
46  
47 Installs the TO_JSON method into the DateTime namespace when requested
48  
49 use DateTime::Format::JSON::MicrosoftDateFormat (to_json => 1); #TO_JSON method installed in DateTime package
50 use DateTime::Format::JSON::MicrosoftDateFormat; #TO_JSON method not installed by default
51  
52 Use the imported DateTime::TO_JSON method and the JSON->convert_blessed options to seamlessly convert DateTime objects to the JSON MicrosoftDateFormat for use in creating encoded JSON structures.
53  
54 use JSON;
55 use DateTime;
56 use DateTime::Format::JSON::MicrosoftDateFormat (to_json=>1);
57 my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new;
58 my $json = JSON->new->convert_blessed->pretty;
59  
60 my $dt = DateTime->now(formatter=>$formatter);
61 print $json->encode({now=>$dt}); #prints {"now" : "/Date(1392747671000)/"}
62  
63 =cut
64  
65 7 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
66 7 my $self=shift;
67 7 ▶ die("$PACKAGE import: Expecting parameters to be hash") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 2 5 7
68 5 my %opt=@_;
69 5 ▶ if ($opt{"to_json"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"to_json"}) 1 4 5
70 1 ▶ *DateTime::TO_JSON=sub {my $self=shift; return "$self"};
Subroutine __ANON__: called CC=1 SCAR=0.0
71 }
72 }
73  
74 =head1 CONSTRUCTOR
75  
76 =head2 new
77  
78 =cut
79  
80 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
81 5 my $class = shift;
82 5 return bless {}, $class;
83 }
84  
85 =head1 METHODS
86  
87 =head2 parse_datetime
88  
89 Returns a DateTime object from the given string
90  
91 use DateTime::Format::JSON::MicrosoftDateFormat;
92 my $parser = DateTime::Format::JSON::MicrosoftDateFormat->new;
93 my $dt = $parser->parse_datetime("/Date(1392606509000)/");
94 print "$dt\n";
95  
96 =cut
97  
98 18 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=3 SCAR=11.0
Pod documented
99 18 my $self = shift;
100 18 my $string = shift;
101 #/Date(1392089278000)/
102 #/Date(1392089278000-0600)/
103 #/Date(1392089278000+0600)/
104 18 ▶ $string =~ m{^/Date\(([+-]?\d+)(?:([+-])(\d\d)(\d\d))?\)/$} or die "Invalid JSON MicrosoftDateFormat string ($string)";
Branch: 1 branch
BranchTrueFalseTotal
$string =~ m[^/Date\(([+-]?\d+)(?:([+-])(\d\d)(\d\d))?\)/$] or die "Invalid JSON MicrosoftDateFormat string ($string)" 15 3 18
105 15 my $milliseconds = $1; #[+-]\d+
106 15 my $direction = $2; #[+-]
107 15 my $hh = $3; #\d\d
108 15 my $mi = $4; #\d\d
109 15 my $dt=DateTime->from_epoch(epoch => $milliseconds / 1000);
110 15 ▶ if (defined $direction) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $direction) 10 5 15
111 10 my $minutes = ($direction."1") * ($mi + $hh * 60);
112 10 $dt->add(minutes => $minutes);
113 }
114 15 return $dt;
115 }
116  
117 =head2 format_datetime
118  
119 Returns a JSON formatted date string for the passed DateTime object
120  
121 my $dt = DateTime->now;
122 my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new;
123 print $formatter->format_datetime($dt), "\n";
124  
125 However, format_datetime is typically used like this...
126  
127 use DateTime;
128 use DateTime::Format::JSON::MicrosoftDateFormat;
129 my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new;
130  
131 my $dt = DateTime->now;
132 $dt->set_formatter($formatter);
133 print "$dt\n"; #prints /Date(1392747078000)/
134  
135 Note: The format_datetime method returns all dates as UTC and does does not support time zone offset in output as it is not well supported in the Microsoft stack e.g. /Date(1392747078000-0500)/
136  
137 =cut
138  
139 5 ▶sub format_datetime {
Subroutine format_datetime: called CC=1 SCAR=0.0
Pod documented
140 5 my $self = shift;
141 5 my $dt = shift;
142 5 return sprintf("/Date(%s)/", $dt->epoch * 1000 + $dt->millisecond);
143 }
144  
145 =head1 BUGS
146  
147 Please log on RT and send an email to the author.
148  
149 =head1 SUPPORT
150  
151 DavisNetworks.com supports all Perl applications including this package.
152  
153 =head1 AUTHOR
154  
155 Michael R. Davis
156 CPAN ID: MRDVT
157 Satellite Tracking of People, LLC
158 mdavis@stopllc.com
159 http://www.stopllc.com/
160  
161 =head1 COPYRIGHT
162  
163 This program is free software licensed under the...
164  
165 The General Public License (GPL)
166 Version 2, June 1991
167  
168 The full text of the license can be found in the LICENSE file included with this module.
169  
170 =head1 SEE ALSO
171  
172 L<DateTime>
173  
174 =cut
175  
176 5 1;
↑ Summary