Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading