MSPF-532: get rid of rfc3339 lib (#131)

This commit is contained in:
Sergei Shuvatov 2020-02-17 19:36:29 +03:00 committed by GitHub
parent d55dbf551a
commit 3ddd842a33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 33 deletions

View File

@ -22,7 +22,6 @@
{deps, [
{cowboy, "2.7.0"},
{hackney, "1.15.2"},
{rfc3339, "0.2.2"},
{gproc , "0.8.0"},
{cache , "2.2.0"},
{thrift, {git, "https://github.com/rbkmoney/thrift_erlang.git", {branch, "master"}}},

View File

@ -27,7 +27,6 @@
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},1},
{<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.3.0">>},2},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.7.1">>},1},
{<<"rfc3339">>,{pkg,<<"rfc3339">>,<<"0.2.2">>},0},
{<<"snowflake">>,
{git,"https://github.com/rbkmoney/snowflake.git",
{ref,"0a598108f6582affe3b4ae550fc5b9f2062e318a"}},
@ -52,7 +51,6 @@
{<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>},
{<<"parse_trans">>, <<"09765507A3C7590A784615CFD421D101AEC25098D50B89D7AA1D66646BC571C1">>},
{<<"ranch">>, <<"6B1FAB51B49196860B733A49C07604465A47BDB78AA10C1C16A3D199F7F8C881">>},
{<<"rfc3339">>, <<"1552DF616ACA368D982E9F085A0E933B6688A3F4938A671798978EC2C0C58730">>},
{<<"ssl_verify_fun">>, <<"6EAF7AD16CB568BB01753DBBD7A95FF8B91C7979482B95F38443FE2C8852A79B">>},
{<<"unicode_util_compat">>, <<"D869E4C68901DD9531385BB0C8C40444EBF624E60B6962D95952775CAC5E90CD">>}]}
].

View File

@ -10,7 +10,6 @@
cowboy,
hackney,
thrift,
rfc3339,
gproc,
cache
]},

View File

@ -52,33 +52,27 @@ from_timeout(TimeoutMillisec) ->
binary().
to_binary(Deadline = undefined) ->
erlang:error(bad_deadline, [Deadline]);
to_binary(Deadline = {{Date, Time}, Millisec}) ->
try rfc3339:format({Date, Time, Millisec * 1000, 0}) of
{ok, DeadlineBin} when is_binary(DeadlineBin) ->
DeadlineBin;
Error ->
%% rfc3339:format/1 has a broken spec and ugly (if not to say broken) code,
%% so just throw any non succeess case here.
erlang:error({bad_deadline, Error}, [Deadline])
to_binary(Deadline) ->
try
Millis = to_unixtime_ms(Deadline),
Str = calendar:system_time_to_rfc3339(Millis, [{unit, millisecond}, {offset, "Z"}]),
erlang:list_to_binary(Str)
catch
error:Error:Stacktrace ->
erlang:error({bad_deadline, {Error, Stacktrace}}, [Deadline])
end.
%% Suppress dialyzer warning until rfc3339 spec will be fixed.
%% see https://github.com/talentdeficit/rfc3339/pull/5
-dialyzer([{nowarn_function, [from_binary/1]}, no_match]).
-spec from_binary(binary()) ->
deadline().
from_binary(Bin) ->
case rfc3339:parse(Bin) of
{ok, {_Date, _Time, _Usec, TZ}} when TZ =/= 0 andalso TZ =/= undefined ->
erlang:error({bad_deadline, not_utc}, [Bin]);
{ok, {Date, Time, undefined, _TZ}} ->
{to_calendar_datetime(Date, Time), 0};
{ok, {Date, Time, Usec, _TZ}} ->
{to_calendar_datetime(Date, Time), Usec div 1000};
{error, Error} ->
ok = assert_is_utc(Bin),
Str = erlang:binary_to_list(Bin),
try
Millis = calendar:rfc3339_to_system_time(Str, [{unit, millisecond}]),
Datetime = calendar:system_time_to_universal_time(Millis, millisecond),
{Datetime, Millis rem 1000}
catch
error:Error ->
erlang:error({bad_deadline, Error}, [Bin])
end.
@ -94,6 +88,17 @@ from_unixtime_ms(DeadlineMillisec) ->
%% Internal functions
%%
-spec assert_is_utc(binary()) ->
ok | no_return().
assert_is_utc(Bin) ->
Size = erlang:byte_size(Bin) - 1,
case Bin of
<<_:Size/bytes, "Z">> ->
ok;
_ ->
erlang:error({bad_deadline, not_utc}, [Bin])
end.
-spec unow() ->
millisec().
unow() ->
@ -101,11 +106,3 @@ unow() ->
% erlang:system_time/1 may have a various difference with global time to prevent time warp.
% see http://erlang.org/doc/apps/erts/time_correction.html#time-warp-modes for details
os:system_time(millisecond).
to_calendar_datetime(Date, Time = {H, _, S}) when H =:= 24 orelse S =:= 60 ->
%% Type specifications for hours and seconds differ in calendar and rfc3339,
%% so make a proper calendar:datetime() here.
Sec = calendar:datetime_to_gregorian_seconds({Date, Time}),
calendar:gregorian_seconds_to_datetime(Sec);
to_calendar_datetime(Date, Time) ->
{Date, Time}.

View File

@ -602,7 +602,7 @@ deadline_to_from_timeout_test(_) ->
deadline_to_from_binary_test(_) ->
Deadline = {{{2010, 4, 11}, {22, 35, 41}}, 29},
DeadlineBin = <<"2010-04-11T22:35:41.029000Z">>,
DeadlineBin = <<"2010-04-11T22:35:41.029Z">>,
DeadlineBin = woody_deadline:to_binary(Deadline),
Deadline = woody_deadline:from_binary(DeadlineBin),
@ -611,10 +611,17 @@ deadline_to_from_binary_test(_) ->
Deadline1 = woody_deadline:from_binary(DeadlineBin1),
Deadline2 = {{{2010, 4, 11}, {22, 35, 41}}, 0},
DeadlineBin2 = <<"2010-04-11T22:35:41Z">>,
DeadlineBin2 = <<"2010-04-11T22:35:41.000Z">>,
Deadline2 = woody_deadline:from_binary(woody_deadline:to_binary(Deadline2)),
DeadlineBin2 = woody_deadline:to_binary(Deadline2),
Deadline3 = <<"2010-04-11T22:35:41+00:30">>,
try woody_deadline:from_binary(Deadline3)
catch
error:{bad_deadline, not_utc} ->
ok
end,
try woody_deadline:to_binary({{baddate, {22, 35, 41}}, 29})
catch
error:{bad_deadline, _} ->