25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Increment number of seconds on timestamp overflow

Increment the number of seconds and set the fractional part to zero if
the latter is too large.
This commit is contained in:
Holger Weiss 2015-01-21 11:20:26 +01:00
parent 18d9f18642
commit 528aabf49c

View File

@ -718,14 +718,15 @@ now_to_utc_string({MegaSecs, Secs, MicroSecs}, Precision) ->
calendar:now_to_universal_time({MegaSecs, Secs,
MicroSecs}),
Max = round(math:pow(10, Precision)),
FracOfSec = case round(MicroSecs / math:pow(10, 6 - Precision)) of
Max -> Max - 1; % don't overflow io_lib:format
X -> X
end,
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0B.~*."
".0BZ",
[Year, Month, Day, Hour, Minute, Second,
Precision, FracOfSec])).
case round(MicroSecs / math:pow(10, 6 - Precision)) of
Max ->
now_to_utc_string({MegaSecs, Secs + 1, 0}, Precision);
FracOfSec ->
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT"
"~2..0B:~2..0B:~2..0B.~*..0BZ",
[Year, Month, Day, Hour, Minute, Second,
Precision, FracOfSec]))
end.
-spec now_to_local_string(erlang:timestamp()) -> binary().