+
Skip to content

Add Support For Millisecond Timestamps #1850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载