From 6475a965058289140d54b873fc18d8242dbee1da Mon Sep 17 00:00:00 2001 From: Bernardo Bruning Date: Tue, 25 Jan 2022 17:02:17 -0300 Subject: [PATCH] Ensures that the seconds are always positive --- upb/json_encode.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/upb/json_encode.c b/upb/json_encode.c index 56f6975b2c..848290fd76 100644 --- a/upb/json_encode.c +++ b/upb/json_encode.c @@ -139,10 +139,6 @@ static void jsonenc_nanos(jsonenc* e, int32_t nanos) { jsonenc_printf(e, ".%.*" PRId32, digits, nanos); } -static int64_t safe_mod(int64_t value, int64_t divisor) { - return (value%divisor + divisor)%divisor; -} - static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg, const upb_MessageDef* m) { const upb_FieldDef* seconds_f = @@ -166,21 +162,21 @@ static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg, * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for * Processing Calendar Dates," Communications of the Association of * Computing Machines, vol. 11 (1968), p. 657. */ - L = seconds / 86400.0 + 68569 + 2440588; + seconds += 62135596800; // Ensure seconds is positive. + L = seconds / 86400 - 719162 + 68569 + 2440588 N = 4 * L / 146097; L = L - (146097 * N + 3) / 4; I = 4000 * (L + 1) / 1461001; L = L - 1461 * I / 4 + 31; J = 80 * L / 2447; K = L - 2447 * J / 80; - L = J / 11; J = J + 2 - 12 * L; I = 100 * (N - 49) + I + L; - sec = safe_mod(seconds, 60); - min = safe_mod((seconds / 60), 60); - hour = safe_mod((seconds / 3600), 24); + sec = seconds % 60; + min = (seconds / 60) % 60; + hour = (seconds / 3600) % 24; jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec); jsonenc_nanos(e, nanos);