From 84f4b7fb4023f270b3bb13f9a7d55bb6f2763557 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Tue, 3 Mar 2015 10:43:04 -0800 Subject: [PATCH] assume all datetimes are UTC time and add timezone designator to generated iso8601 strings --- README.md | 3 +-- src/jsx_encoder.erl | 3 +++ src/jsx_parser.erl | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c2a2081..fa52a75 100644 --- a/README.md +++ b/README.md @@ -257,8 +257,7 @@ see below | `datetime()` erlang datetime tuples (`{{Year, Month, Day}, {Hour, Min, Sec}}`) as returned from `erlang:localtime/0` are automatically encoded as [iso8601][iso8601] - strings. no conversion is attempted of json [iso8601][iso8601] strings in - decoded json + strings and are assumed to be UTC time. no conversion is attempted of json [iso8601][iso8601] strings in decoded json ### incomplete input ### diff --git a/src/jsx_encoder.erl b/src/jsx_encoder.erl index 12b1c67..e89328c 100644 --- a/src/jsx_encoder.erl +++ b/src/jsx_encoder.erl @@ -54,6 +54,9 @@ encode(Term, EntryPoint) -> encode_(Term, EntryPoint). encode_([], _EntryPoint) -> [start_array, end_array]; encode_([{}], _EntryPoint) -> [start_object, end_object]; +%% datetime special case +encode_([{{_,_,_},{_,_,_}} = DateTime|Rest], EntryPoint) -> + [start_array] ++ [DateTime] ++ unhitch(Rest, EntryPoint); encode_([{_, _}|_] = Term, EntryPoint) -> [start_object] ++ unzip(Term, EntryPoint); encode_(Term, EntryPoint) when is_list(Term) -> diff --git a/src/jsx_parser.erl b/src/jsx_parser.erl index a274574..322494c 100644 --- a/src/jsx_parser.erl +++ b/src/jsx_parser.erl @@ -112,7 +112,7 @@ value([{raw, Raw}|Tokens], Handler, Stack, Config) when is_binary(Raw) -> value([{{Year, Month, Day}, {Hour, Min, Sec}}|Tokens], Handler, Stack, Config) when is_integer(Year), is_integer(Month), is_integer(Day), is_integer(Hour), is_integer(Min), is_integer(Sec) -> value([{string, unicode:characters_to_binary(io_lib:format( - "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B", + "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ", [Year, Month, Day, Hour, Min, Sec] ))}|Tokens], Handler, @@ -1145,7 +1145,7 @@ fix_key_test_() -> datetime_test_() -> [ {"datetime", ?_assertEqual( - [start_array, {string, <<"2014-08-13T23:12:34">>}, end_array, end_json], + [start_array, {string, <<"2014-08-13T23:12:34Z">>}, end_array, end_json], parse([start_array, {{2014,08,13},{23,12,34}}, end_array, end_json], []) )} ].