DateTime::Format::JSON::MicrosoftDateFormat - Parse and format JSON MicrosoftDateFormat strings
use DateTime::Format::JSON::MicrosoftDateFormat; my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new; my $dt = $formatter->parse_datetime("/Date(1392089278000-0600)/"); #2014-02-10T21:27:58Z my $dt = $formatter->parse_datetime("/Date(1392067678000)/"); #2014-02-10T21:27:58Z say $formatter->format_datetime($dt); #/Date(1392067678000)/
perl -MDateTime::Format::JSON::MicrosoftDateFormat -e 'print DateTime::Format::JSON::MicrosoftDateFormat->new->parse_datetime(shift), "\n";' '/Date(1392606509000-0500)/'
This module understands the JSON MicrosoftDateFormat date/time format. e.g. /Date(1392067678000)/
Note: The usage of MicrosoftDateFormat is deprecated for new JSON services. Please use ISO 8601 for all new development.
From RFC 7493 (The I-JSON Message Format):
Protocols often contain data items that are designed to contain timestamps or time durations. It is RECOMMENDED that all such data items be expressed as string values in ISO 8601 format, as specified in RFC 3339, with the additional restrictions that uppercase rather than lowercase letters be used, that the timezone be included not defaulted, and that optional trailing seconds be included even when their value is "00". It is also RECOMMENDED that all data items containing time durations conform to the "duration" production in Appendix A of RFC 3339, with the same additional restrictions.
Installs the TO_JSON method into the DateTime namespace when requested
use DateTime::Format::JSON::MicrosoftDateFormat (to_json => 1); #TO_JSON method installed in DateTime package use DateTime::Format::JSON::MicrosoftDateFormat; #TO_JSON method not installed by default
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.
use JSON; use DateTime; use DateTime::Format::JSON::MicrosoftDateFormat (to_json=>1); my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new; my $json = JSON->new->convert_blessed->pretty; my $dt = DateTime->now(formatter=>$formatter); print $json->encode({now=>$dt}); #prints {"now" : "/Date(1392747671000)/"}
Returns a DateTime object from the given string
use DateTime::Format::JSON::MicrosoftDateFormat; my $parser = DateTime::Format::JSON::MicrosoftDateFormat->new; my $dt = $parser->parse_datetime("/Date(1392606509000)/"); print "$dt\n";
Returns a JSON formatted date string for the passed DateTime object
my $dt = DateTime->now; my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new; print $formatter->format_datetime($dt), "\n";
However, format_datetime is typically used like this...
use DateTime; use DateTime::Format::JSON::MicrosoftDateFormat; my $formatter = DateTime::Format::JSON::MicrosoftDateFormat->new; my $dt = DateTime->now; $dt->set_formatter($formatter); print "$dt\n"; #prints /Date(1392747078000)/
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)/
Please log on RT and send an email to the author.
DavisNetworks.com supports all Perl applications including this package.
Michael R. Davis CPAN ID: MRDVT Satellite Tracking of People, LLC mdavis@stopllc.com http://www.stopllc.com/
This program is free software licensed under the...
The General Public License (GPL) Version 2, June 1991
The full text of the license can be found in the LICENSE file included with this module.
DateTime
To install DateTime::Format::JSON::MicrosoftDateFormat, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DateTime::Format::JSON::MicrosoftDateFormat
CPAN shell
perl -MCPAN -e shell install DateTime::Format::JSON::MicrosoftDateFormat
For more information on module installation, please visit the detailed CPAN module installation guide.