]> granicus.if.org Git - ejabberd/commitdiff
Increment number of seconds on timestamp overflow
authorHolger Weiss <holger@zedat.fu-berlin.de>
Wed, 21 Jan 2015 10:20:26 +0000 (11:20 +0100)
committerHolger Weiss <holger@zedat.fu-berlin.de>
Wed, 21 Jan 2015 10:20:26 +0000 (11:20 +0100)
Increment the number of seconds and set the fractional part to zero if
the latter is too large.

src/jlib.erl

index 6397335e5ff9c7911cfc765c05d7793787f9ac67..59313417656584fecd56051e81b1205cdfee5934 100644 (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().