Skip to content

Commit 384ca4d

Browse files
committed
minor: add some x/jsonx.ISO8601 shortcuts
more features designed for the past 2-3 months to come, this is just a hotfix
1 parent 1d106d8 commit 384ca4d

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

x/jsonx/iso8601.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ type ISO8601 time.Time
6565
var _ Exampler = (*ISO8601)(nil)
6666

6767
// ParseISO8601 reads from "s" and returns the ISO8601 time.
68+
//
69+
// The function supports the following formats:
70+
// - 2024-01-02T15:04:05.999999Z
71+
// - 2024-01-02T15:04:05+07:00
72+
// - 2024-04-08T08:05:04.830140+00:00
73+
// - 2024-01-02T15:04:05Z
74+
// - 2024-04-08T08:05:04.830140
75+
// - 2024-01-02T15:04:05
6876
func ParseISO8601(s string) (ISO8601, error) {
6977
if s == "" || s == "null" {
7078
return ISO8601{}, nil
@@ -216,10 +224,12 @@ func (t ISO8601) MarshalJSON() ([]byte, error) {
216224
// Examples returns a list of example values.
217225
func (t ISO8601) ListExamples() any {
218226
return []string{
219-
"2006-01-02T15:04:05",
220-
"2022-08-09T00:00:00.000000",
221-
"2022-08-10T03:21:00.000000+03:00",
222-
"2023-02-04T09:48:14+00:00",
227+
"2024-01-02T15:04:05.999999Z",
228+
"2024-01-02T15:04:05+07:00",
229+
"2024-04-08T08:05:04.830140+00:00",
230+
"2024-01-02T15:04:05Z",
231+
"2024-04-08T08:05:04.830140",
232+
"2024-01-02T15:04:05",
223233
}
224234
}
225235

@@ -235,6 +245,26 @@ func (t ISO8601) IsZero() bool {
235245
return time.Time(t).IsZero()
236246
}
237247

248+
// After reports whether the time instant "t" is after "u".
249+
func (t ISO8601) After(u ISO8601) bool {
250+
return t.ToTime().After(u.ToTime())
251+
}
252+
253+
// Equal reports whether the time instant "t" is equal to "u".
254+
func (t ISO8601) Equal(u ISO8601) bool {
255+
return t.ToTime().Equal(u.ToTime())
256+
}
257+
258+
// Add returns the time "t" with the duration added.
259+
func (t ISO8601) Add(d time.Duration) ISO8601 {
260+
return ISO8601(t.ToTime().Add(d))
261+
}
262+
263+
// Sub returns the duration between "t" and "u".
264+
func (t ISO8601) Sub(u ISO8601) time.Duration {
265+
return t.ToTime().Sub(u.ToTime())
266+
}
267+
238268
// String returns the text representation of the "t" using the ISO8601 time layout.
239269
func (t ISO8601) String() string {
240270
tt := t.ToTime()

x/jsonx/iso8601_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func TestParseISO8601(t *testing.T) {
4949
want: ISO8601(time.Date(2024, 01, 02, 15, 04, 05, 0, time.UTC)),
5050
wantErr: false,
5151
},
52+
{
53+
name: "Timestamp with Zulu time with microseconds",
54+
input: "2024-04-08T08:05:04.830140",
55+
want: ISO8601(time.Date(2024, 04, 8, 8, 05, 04, 830140*1000, time.UTC)),
56+
wantErr: false,
57+
},
5258
{
5359
name: "Basic ISO8601 layout",
5460
input: "2024-01-02T15:04:05",

0 commit comments

Comments
 (0)