timestamp.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. package github
  6. import (
  7. "strconv"
  8. "time"
  9. )
  10. // Timestamp represents a time that can be unmarshalled from a JSON string
  11. // formatted as either an RFC3339 or Unix timestamp. This is necessary for some
  12. // fields since the GitHub API is inconsistent in how it represents times. All
  13. // exported methods of time.Time can be called on Timestamp.
  14. type Timestamp struct {
  15. time.Time
  16. }
  17. func (t Timestamp) String() string {
  18. return t.Time.String()
  19. }
  20. // UnmarshalJSON implements the json.Unmarshaler interface.
  21. // Time is expected in RFC3339 or Unix format.
  22. func (t *Timestamp) UnmarshalJSON(data []byte) (err error) {
  23. str := string(data)
  24. i, err := strconv.ParseInt(str, 10, 64)
  25. if err == nil {
  26. (*t).Time = time.Unix(i, 0)
  27. } else {
  28. (*t).Time, err = time.Parse(`"`+time.RFC3339+`"`, str)
  29. }
  30. return
  31. }
  32. // Equal reports whether t and u are equal based on time.Equal
  33. func (t Timestamp) Equal(u Timestamp) bool {
  34. return t.Time.Equal(u.Time)
  35. }