line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DateTime::Format::JSON::MicrosoftDateFormat; |
2
|
5
|
|
|
5
|
|
704324
|
use strict; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
222
|
|
3
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
149
|
|
4
|
5
|
|
|
5
|
|
3279
|
use DateTime; |
|
5
|
|
|
|
|
473571
|
|
|
5
|
|
|
|
|
2066
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
7
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
This module understands the JSON MicrosoftDateFormat date/time format. e.g. /Date(1392067678000)/ |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 USAGE |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 import |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Installs the TO_JSON method into the DateTime namespace when requested |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use DateTime::Format::JSON::MicrosoftDateFormat (to_json => 1); #TO_JSON method installed in DateTime package |
34
|
|
|
|
|
|
|
use DateTime::Format::JSON::MicrosoftDateFormat; #TO_JSON method not installed by default |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
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. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use JSON; |
39
|
|
|
|
|
|
|
use DateTime; |
40
|
|
|
|
|
|
|
use DateTime::Format::JSON::MicrosoftDateFormat (to_json=>1); |
41
|
|
|
|
|
|
|
my $formatter=DateTime::Format::JSON::MicrosoftDateFormat->new; |
42
|
|
|
|
|
|
|
my $json=JSON->new->convert_blessed->pretty; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $dt=DateTime->now(formatter=>$formatter); |
45
|
|
|
|
|
|
|
print $json->encode({now=>$dt}); #prints {"now" : "/Date(1392747671000)/"} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub import { |
50
|
5
|
|
|
5
|
|
51
|
my $self=shift; |
51
|
5
|
50
|
|
|
|
30
|
die("$PACKAGE import: Expecting parameters to be hash") if @_ % 2; |
52
|
5
|
|
|
|
|
13
|
my %opt=@_; |
53
|
5
|
100
|
|
|
|
6935
|
if ($opt{"to_json"}) { |
54
|
1
|
|
|
1
|
|
2077
|
*DateTime::TO_JSON=sub {shift->_stringify}; |
|
1
|
|
|
|
|
8231
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 new |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub new { |
65
|
5
|
|
|
5
|
1
|
80
|
my $class = shift; |
66
|
5
|
|
|
|
|
28
|
return bless {}, $class; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 METHODS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 parse_datetime |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Returns a DateTime object from the given string |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
use DateTime::Format::JSON::MicrosoftDateFormat; |
76
|
|
|
|
|
|
|
my $parser=DateTime::Format::JSON::MicrosoftDateFormat->new; |
77
|
|
|
|
|
|
|
my $dt=$parser->parse_datetime("/Date(1392606509000)/"); |
78
|
|
|
|
|
|
|
print "$dt\n"; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub parse_datetime { |
83
|
15
|
|
|
15
|
1
|
15717
|
my $self = shift; |
84
|
15
|
|
|
|
|
27
|
my $string = shift; |
85
|
|
|
|
|
|
|
#/Date(1392089278000)/ |
86
|
|
|
|
|
|
|
#/Date(1392089278000-0600)/ |
87
|
|
|
|
|
|
|
#/Date(1392089278000+0600)/ |
88
|
15
|
50
|
|
|
|
89
|
$string =~ m{^/Date\(([+-]?\d+)(?:([+-])(\d\d)(\d\d))?\)/$} or die "Invalid JSON MicrosoftDateFormat string ($string)"; |
89
|
15
|
|
|
|
|
29
|
my $milliseconds = $1; #[+-]\d+ |
90
|
15
|
|
|
|
|
24
|
my $direction = $2; #[+-] |
91
|
15
|
|
|
|
|
23
|
my $hh = $3; #\d\d |
92
|
15
|
|
|
|
|
24
|
my $mi = $4; #\d\d |
93
|
15
|
|
|
|
|
79
|
my $dt=DateTime->from_epoch(epoch => $milliseconds / 1000); |
94
|
15
|
100
|
|
|
|
3497
|
if (defined $direction) { |
95
|
10
|
|
|
|
|
32
|
my $minutes = ($direction."1") * ($mi + $hh * 60); |
96
|
10
|
|
|
|
|
37
|
$dt->add(minutes => $minutes); |
97
|
|
|
|
|
|
|
} |
98
|
15
|
|
|
|
|
3007
|
return $dt; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 format_datetime |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Returns a JSON formatted date string for the passed DateTime object |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $dt=DateTime->now; |
106
|
|
|
|
|
|
|
my $formatter=DateTime::Format::JSON::MicrosoftDateFormat->new; |
107
|
|
|
|
|
|
|
$formatter->format_datetime($dt); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
However, format_datetime is typically use like this... |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
use DateTime; |
112
|
|
|
|
|
|
|
use DateTime::Format::JSON::MicrosoftDateFormat; |
113
|
|
|
|
|
|
|
my $formatter=DateTime::Format::JSON::MicrosoftDateFormat->new; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
my $dt=DateTime->now; |
116
|
|
|
|
|
|
|
$dt->set_formatter($formatter); |
117
|
|
|
|
|
|
|
print "$dt\n"; #prints /Date(1392747078000)/ |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
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)/ |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub format_datetime { |
124
|
9
|
|
|
9
|
1
|
46110
|
my $self = shift; |
125
|
9
|
|
|
|
|
18
|
my $dt = shift; |
126
|
9
|
|
|
|
|
35
|
return sprintf("/Date(%s)/", $dt->epoch * 1000 + $dt->millisecond); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 BUGS |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Please log on RT and send an email to the author. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 SUPPORT |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
DavisNetworks.com supports all Perl applications including this package. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 AUTHOR |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Michael R. Davis |
140
|
|
|
|
|
|
|
CPAN ID: MRDVT |
141
|
|
|
|
|
|
|
Satellite Tracking of People, LLC |
142
|
|
|
|
|
|
|
mdavis@stopllc.com |
143
|
|
|
|
|
|
|
http://www.stopllc.com/ |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 COPYRIGHT |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This program is free software licensed under the... |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
The General Public License (GPL) |
150
|
|
|
|
|
|
|
Version 2, June 1991 |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
The full text of the license can be found in the LICENSE file included with this module. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SEE ALSO |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |