github.com/google/go-github/v68@v68.0.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 // GetTime returns std time.Time. 26 func (t *Timestamp) GetTime() *time.Time { 27 if t == nil { 28 return nil 29 } 30 return &t.Time 31 } 32 33 // UnmarshalJSON implements the json.Unmarshaler interface. 34 // Time is expected in RFC3339 or Unix format. 35 func (t *Timestamp) UnmarshalJSON(data []byte) (err error) { 36 str := string(data) 37 i, err := strconv.ParseInt(str, 10, 64) 38 if err == nil { 39 t.Time = time.Unix(i, 0) 40 if t.Time.Year() > 3000 { 41 t.Time = time.Unix(0, i*1e6) 42 } 43 } else { 44 t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str) 45 } 46 return 47 } 48 49 // Equal reports whether t and u are equal based on time.Equal. 50 func (t Timestamp) Equal(u Timestamp) bool { 51 return t.Time.Equal(u.Time) 52 }