The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DateTime::Format::JSON::MicrosoftDateFormat - Parse and format JSON MicrosoftDateFormat strings

SYNOPSIS

  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)/

DESCRIPTION

This module understands the JSON MicrosoftDateFormat date/time format. e.g. /Date(1392067678000)/

USAGE

import

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)/"}

CONSTRUCTOR

new

METHODS

parse_datetime

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";

format_datetime

Returns a JSON formatted date string for the passed DateTime object

  my $dt=DateTime->now;
  my $formatter=DateTime::Format::JSON::MicrosoftDateFormat->new;
  $formatter->format_datetime($dt);

However, format_datetime is typically use 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)/

BUGS

Please log on RT and send an email to the author.

SUPPORT

DavisNetworks.com supports all Perl applications including this package.

AUTHOR

  Michael R. Davis
  CPAN ID: MRDVT
  Satellite Tracking of People, LLC
  mdavis@stopllc.com
  http://www.stopllc.com/

COPYRIGHT

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.

SEE ALSO

DateTime