Skip to content

Commit b3adf37

Browse files
committed
Add Support For Millisecond Timestamps
The GitHub Audit Log API introduced timestamps with millisecond granularity. We add support for these timestamps by first parsing the timestamp into a Unix timestamp and then checking to see if the generated timestamp is in the future. If the timestamp is in the future we re-parse it as a Unix timestamp in Milliseconds. Signed-off-by: Brett Logan <lindluni@github.com>
1 parent bf34728 commit b3adf37

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

github/enterprise_audit_log_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestEnterpriseService_GetAuditLog(t *testing.T) {
5555
}
5656
startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
5757
completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
58-
timestamp := time.Unix(1615077308538, 0)
58+
timestamp := time.Unix(0, 0).Add(time.Duration(1615077308538) * time.Millisecond)
5959

6060
want := []*AuditEntry{
6161
{

github/orgs_audit_log_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func TestOrganizationService_GetAuditLog(t *testing.T) {
6262
}
6363
startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
6464
completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
65-
timestamp := time.Unix(1615077308538, 0)
65+
timestamp := time.Unix(0, 0).Add(time.Duration(1615077308538) * time.Millisecond)
66+
6667
want := []*AuditEntry{
6768
{
6869
Timestamp: &Timestamp{timestamp},

github/timestamp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
2929
i, err := strconv.ParseInt(str, 10, 64)
3030
if err == nil {
3131
t.Time = time.Unix(i, 0)
32+
if t.Time.Year() > 3000 {
33+
t.Time = time.Unix(0, 0).Add(time.Duration(i) * time.Millisecond)
34+
}
3235
} else {
3336
t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str)
3437
}

github/timestamp_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

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

2223
var (
@@ -59,12 +60,14 @@ func TestTimestamp_Unmarshal(t *testing.T) {
5960
}{
6061
{"Reference", referenceTimeStr, Timestamp{referenceTime}, false, true},
6162
{"ReferenceUnix", referenceUnixTimeStr, Timestamp{referenceTime}, false, true},
63+
{"ReferenceUnixMillisecond", referenceUnixTimeStrMilliSeconds, Timestamp{referenceTime}, false, true},
6264
{"ReferenceFractional", referenceTimeStrFractional, Timestamp{referenceTime}, false, true},
6365
{"Empty", emptyTimeStr, Timestamp{}, false, true},
6466
{"UnixStart", `0`, Timestamp{unixOrigin}, false, true},
6567
{"Mismatch", referenceTimeStr, Timestamp{}, false, false},
6668
{"MismatchUnix", `0`, Timestamp{}, false, false},
6769
{"Invalid", `"asdf"`, Timestamp{referenceTime}, true, false},
70+
{"OffByMillisecond", `1136214245001`, Timestamp{referenceTime}, false, false},
6871
}
6972
for _, tc := range testCases {
7073
var got Timestamp
@@ -144,11 +147,13 @@ func TestWrappedTimestamp_Unmarshal(t *testing.T) {
144147
}{
145148
{"Reference", referenceTimeStr, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
146149
{"ReferenceUnix", referenceUnixTimeStr, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
150+
{"ReferenceUnixMillisecond", referenceUnixTimeStrMilliSeconds, WrappedTimestamp{0, Timestamp{referenceTime}}, false, true},
147151
{"Empty", emptyTimeStr, WrappedTimestamp{0, Timestamp{}}, false, true},
148152
{"UnixStart", `0`, WrappedTimestamp{0, Timestamp{unixOrigin}}, false, true},
149153
{"Mismatch", referenceTimeStr, WrappedTimestamp{0, Timestamp{}}, false, false},
150154
{"MismatchUnix", `0`, WrappedTimestamp{0, Timestamp{}}, false, false},
151155
{"Invalid", `"asdf"`, WrappedTimestamp{0, Timestamp{referenceTime}}, true, false},
156+
{"OffByMillisecond", `1136214245001`, WrappedTimestamp{0, Timestamp{referenceTime}}, false, false},
152157
}
153158
for _, tc := range testCases {
154159
var got Timestamp

0 commit comments

Comments
 (0)