From 3d38d3b77c9f76e976f6aa56e382a585c92403b4 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Sat, 3 Oct 2026 03:31:10 +0700 Subject: [PATCH] query: encode unix milliseconds before 1970 UnixNano divided by 1e6 truncates toward zero, so a time one nanosecond after 1969-12-31 23:59:59 was encoded as -999 instead of -1000. Signed-off-by: Sasha Mitchell --- query/encode.go | 5 ++++- query/encode_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/query/encode.go b/query/encode.go index ffb2097..19deae1 100644 --- a/query/encode.go +++ b/query/encode.go @@ -302,7 +302,10 @@ func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) strin return strconv.FormatInt(t.Unix(), 10) } if opts.Contains("unixmilli") { - return strconv.FormatInt((t.UnixNano() / 1e6), 10) + // UnixNano divided by 1e6 truncates toward zero, so a time + // in the millisecond before 1970 is encoded one millisecond high. + ms := t.Unix()*1e3 + int64(t.Nanosecond())/1e6 + return strconv.FormatInt(ms, 10) } if opts.Contains("unixnano") { return strconv.FormatInt(t.UnixNano(), 10) diff --git a/query/encode_test.go b/query/encode_test.go index 241e4f1..06f22a9 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -78,6 +78,14 @@ func TestValues_BasicTypes(t *testing.T) { }{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)}, url.Values{"V": {"946730096000"}}, }, + { + // One nanosecond after 1969-12-31 23:59:59 is still the + // millisecond that starts at -1000. + struct { + V time.Time `url:",unixmilli"` + }{time.Date(1969, 12, 31, 23, 59, 59, 1, time.UTC)}, + url.Values{"V": {"-1000"}}, + }, { struct { V time.Time `url:",unixnano"`