Skip to content

Commit

Permalink
Add Support For Millisecond Timestamps (#1850)
Browse files Browse the repository at this point in the history
Fixes: #1849.
  • Loading branch information
lindluni authored Apr 8, 2021
1 parent 7add3a8 commit d711542
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion github/enterprise_audit_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestEnterpriseService_GetAuditLog(t *testing.T) {
}
startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
timestamp := time.Unix(1615077308538, 0)
timestamp := time.Unix(0, 1615077308538*1e6)

want := []*AuditEntry{
{
Expand Down
3 changes: 2 additions & 1 deletion github/orgs_audit_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestOrganizationService_GetAuditLog(t *testing.T) {
}
startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
timestamp := time.Unix(1615077308538, 0)
timestamp := time.Unix(0, 1615077308538*1e6)

want := []*AuditEntry{
{
Timestamp: &Timestamp{timestamp},
Expand Down
3 changes: 3 additions & 0 deletions github/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
i, err := strconv.ParseInt(str, 10, 64)
if err == nil {
t.Time = time.Unix(i, 0)
if t.Time.Year() > 3000 {
t.Time = time.Unix(0, i*1e6)
}
} else {
t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str)
}
Expand Down
13 changes: 9 additions & 4 deletions github/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

const (
emptyTimeStr = `"0001-01-01T00:00:00Z"`
referenceTimeStr = `"2006-01-02T15:04:05Z"`
referenceTimeStrFractional = `"2006-01-02T15:04:05.000Z"` // This format was returned by the Projects API before October 1, 2017.
referenceUnixTimeStr = `1136214245`
emptyTimeStr = `"0001-01-01T00:00:00Z"`
referenceTimeStr = `"2006-01-02T15:04:05Z"`
referenceTimeStrFractional = `"2006-01-02T15:04:05.000Z"` // This format was returned by the Projects API before October 1, 2017.
referenceUnixTimeStr = `1136214245`
referenceUnixTimeStrMilliSeconds = `1136214245000` // Millisecond-granular timestamps were introduced in the Audit log API.
)

var (
Expand Down Expand Up @@ -59,12 +60,14 @@ func TestTimestamp_Unmarshal(t *testing.T) {
}{
{"Reference", referenceTimeStr, Timestamp{referenceTime}, false, true},
{"ReferenceUnix", referenceUnixTimeStr, Timestamp{referenceTime}, false, true},
{"ReferenceUnixMillisecond", referenceUnixTimeStrMilliSeconds, Timestamp{referenceTime}, false, true},
{"ReferenceFractional", referenceTimeStrFractional, Timestamp{referenceTime}, false, true},
{"Empty", emptyTimeStr, Timestamp{}, false, true},
{"UnixStart", `0`, Timestamp{unixOrigin}, false, true},
{"Mismatch", referenceTimeStr, Timestamp{}, false, false},
{"MismatchUnix", `0`, Timestamp{}, false, false},
{"Invalid", `"asdf"`, Timestamp{referenceTime}, true, false},
{"OffByMillisecond", `1136214245001`, Timestamp{referenceTime}, false, false},
}
for _, tc := range testCases {
var got Timestamp
Expand Down Expand Up @@ -144,11 +147,13 @@ func TestWrappedTimestamp_Unmarshal(t *testing.T) {
}{
{"Reference", referenceTimeStr, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
{"ReferenceUnix", referenceUnixTimeStr, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
{"ReferenceUnixMillisecond", referenceUnixTimeStrMilliSeconds, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
{"Empty", emptyTimeStr, WrappedTimestamp{0, Timestamp{}}, false, true},
{"UnixStart", `0`, WrappedTimestamp{0, Timestamp{unixOrigin}}, false, true},
{"Mismatch", referenceTimeStr, WrappedTimestamp{0, Timestamp{}}, false, false},
{"MismatchUnix", `0`, WrappedTimestamp{0, Timestamp{}}, false, false},
{"Invalid", `"asdf"`, WrappedTimestamp{0, Timestamp{referenceTime}}, true, false},
{"OffByMillisecond", `1136214245001`, WrappedTimestamp{0, Timestamp{referenceTime}}, false, false},
}
for _, tc := range testCases {
var got Timestamp
Expand Down

0 comments on commit d711542

Please sign in to comment.