From: Holger Weiss Date: Wed, 21 Jan 2015 10:20:26 +0000 (+0100) Subject: Increment number of seconds on timestamp overflow X-Git-Tag: 15.02~17^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=528aabf49c9a5c4de36e9d8e2ca83c089a6a6d88;p=ejabberd 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. --- diff --git a/src/jlib.erl b/src/jlib.erl index 6397335e5..593134176 100644 --- a/src/jlib.erl +++ b/src/jlib.erl @@ -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().