Skip to content

Commit 2c9db42

Browse files
Ed Andersonwillnorris
authored andcommitted
Added support for unix nano and milliseconds
fixes #44
1 parent 55faf79 commit 2c9db42

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ v, _ := query.Values(opt)
3131
fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
3232
```
3333

34+
See the [package godocs][] for complete documentation on supported types and
35+
formatting options.
36+
3437
[go-github]: https://github.com/google/go-github/commit/994f6f8405f052a117d2d0b500054341048fbb08
38+
[package godocs]: https://pkg.go.dev/github.com/google/go-querystring/query
3539

3640
## Alternatives ##
3741

query/encode.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ type Encoder interface {
8282
//
8383
// time.Time values default to encoding as RFC3339 timestamps. Including the
8484
// "unix" option signals that the field should be encoded as a Unix time (see
85-
// time.Unix())
85+
// time.Unix()). The "unixmilli" and "unixnano" options will encode the number
86+
// of milliseconds and nanoseconds, respectively, since January 1, 1970 (see
87+
// time.UnixNano()).
8688
//
8789
// Slice and Array values default to encoding as multiple URL values of the
8890
// same name. Including the "comma" option signals that the field should be
@@ -270,6 +272,12 @@ func valueString(v reflect.Value, opts tagOptions) string {
270272
if opts.Contains("unix") {
271273
return strconv.FormatInt(t.Unix(), 10)
272274
}
275+
if opts.Contains("unixmilli") {
276+
return strconv.FormatInt((t.UnixNano() / 1e6), 10)
277+
}
278+
if opts.Contains("unixnano") {
279+
return strconv.FormatInt(t.UnixNano(), 10)
280+
}
273281
return t.Format(time.RFC3339)
274282
}
275283

query/encode_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ func TestValues_BasicTypes(t *testing.T) {
7272
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
7373
url.Values{"V": {"946730096"}},
7474
},
75+
{
76+
struct {
77+
V time.Time `url:",unixmilli"`
78+
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
79+
url.Values{"V": {"946730096000"}},
80+
},
81+
{
82+
struct {
83+
V time.Time `url:",unixnano"`
84+
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
85+
url.Values{"V": {"946730096000000000"}},
86+
},
7587
}
7688

7789
for _, tt := range tests {

0 commit comments

Comments
 (0)