Skip to content

Commit

Permalink
time: support conversion RFC3339 time dates to unix time
Browse files Browse the repository at this point in the history
  • Loading branch information
oncilla committed Aug 4, 2021
1 parent 0f53054 commit bf99ecf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/time/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/time/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ import "time"
t1: time.Time & "1937-01-01T12:00:27.87+00:20"
t2: time.Time & "no time"
t3: time.Unix(1500000000, 123456)
t4: time.ToUnix("2017-07-14T02:40:00.000123456Z")
t5: time.ToUnix("07-14-2017T02:40:00.000123456Z")
t6: time.ToUnixNano("2017-07-14T02:40:00.000123456Z")
t7: time.ToUnixNano("07-14-2017T02:40:00.000123456Z")
-- out/time --
Errors:
t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time":
./in.cue:4:17
error in call to time.ToUnix: invalid time "07-14-2017T02:40:00.000123456Z":
./in.cue:7:5
error in call to time.ToUnixNano: invalid time "07-14-2017T02:40:00.000123456Z":
./in.cue:9:5

Result:
t1: "1937-01-01T12:00:27.87+00:20"
t2: _|_ // t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time"
t3: "2017-07-14T02:40:00.000123456Z"
t4: 1500000000
t5: _|_ // error in call to time.ToUnix: invalid time "07-14-2017T02:40:00.000123456Z"
t6: 1500000000000123456
t7: _|_ // error in call to time.ToUnixNano: invalid time "07-14-2017T02:40:00.000123456Z"

35 changes: 35 additions & 0 deletions pkg/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,38 @@ func Unix(sec int64, nsec int64) string {
t := time.Unix(sec, nsec)
return t.UTC().Format(time.RFC3339Nano)
}

// Unix returns t as a Unix time, the number of seconds elapsed since January 1,
// 1970 UTC. The result does not depend on the location associated with t.
// Unix-like operating systems often record time as a 32-bit count of seconds,
// but since the method here returns a 64-bit value it is valid for billions of
// years into the past or future.

// ToUnix returns the provided RFC3339 time-date as a Unix time, the number of
// seconds elapsed since January 1, 1970 UTC.
func ToUnix(value string) (int64, error) {
t, err := parse(value)
if err != nil {
return 0, err
}
return t.Unix(), nil
}

// ToUnix returns the provided RFC3339 time-date as a Unix time, the number of
// nanoseconds elapsed since January 1, 1970 UTC. The result is undefined if the
// Unix time in nanoseconds cannot be represented by an int64 (a date before the
// year 1678 or after 2262).
func ToUnixNano(value string) (int64, error) {
t, err := parse(value)
if err != nil {
return 0, err
}
return t.UnixNano(), nil
}

func parse(value string) (time.Time, error) {
if _, err := Time(value); err != nil {
return time.Time{}, err
}
return time.Parse(time.RFC3339Nano, value)
}

0 comments on commit bf99ecf

Please sign in to comment.