github.com/google/go-github/v49@v49.1.0/github/timestamp.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "strconv" 10 "time" 11 ) 12 13 // Timestamp represents a time that can be unmarshalled from a JSON string 14 // formatted as either an RFC3339 or Unix timestamp. This is necessary for some 15 // fields since the GitHub API is inconsistent in how it represents times. All 16 // exported methods of time.Time can be called on Timestamp. 17 type Timestamp struct { 18 time.Time 19 } 20 21 func (t Timestamp) String() string { 22 return t.Time.String() 23 } 24 25 // UnmarshalJSON implements the json.Unmarshaler interface. 26 // Time is expected in RFC3339 or Unix format. 27 func (t *Timestamp) UnmarshalJSON(data []byte) (err error) { 28 str := string(data) 29 i, err := strconv.ParseInt(str, 10, 64) 30 if err == nil { 31 t.Time = time.Unix(i, 0) 32 if t.Time.Year() > 3000 { 33 t.Time = time.Unix(0, i*1e6) 34 } 35 } else { 36 t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str) 37 } 38 return 39 } 40 41 // Equal reports whether t and u are equal based on time.Equal 42 func (t Timestamp) Equal(u Timestamp) bool { 43 return t.Time.Equal(u.Time) 44 }