timeduration.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package provisioner
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/pkg/errors"
  6. )
  7. var now = func() time.Time {
  8. return time.Now().UTC()
  9. }
  10. // TimeDuration is a type that represents a time but the JSON unmarshaling can
  11. // use a time using the RFC 3339 format or a time.Duration string. If a duration
  12. // is used, the time will be set on the first call to TimeDuration.Time.
  13. type TimeDuration struct {
  14. t time.Time
  15. d time.Duration
  16. }
  17. // NewTimeDuration returns a TimeDuration with the defined time.
  18. func NewTimeDuration(t time.Time) TimeDuration {
  19. return TimeDuration{t: t}
  20. }
  21. // ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or
  22. // time.Duration string.
  23. func ParseTimeDuration(s string) (TimeDuration, error) {
  24. if s == "" {
  25. return TimeDuration{}, nil
  26. }
  27. // Try to use the unquoted RFC 3339 format
  28. var t time.Time
  29. if err := t.UnmarshalText([]byte(s)); err == nil {
  30. return TimeDuration{t: t.UTC()}, nil
  31. }
  32. // Try to use the time.Duration string format
  33. if d, err := time.ParseDuration(s); err == nil {
  34. return TimeDuration{d: d}, nil
  35. }
  36. return TimeDuration{}, errors.Errorf("failed to parse %s", s)
  37. }
  38. // SetDuration initializes the TimeDuration with the given duration string. If
  39. // the time was set it will re-set to zero.
  40. func (t *TimeDuration) SetDuration(d time.Duration) {
  41. t.t, t.d = time.Time{}, d
  42. }
  43. // SetTime initializes the TimeDuration with the given time. If the duration is
  44. // set it will be re-set to zero.
  45. func (t *TimeDuration) SetTime(tt time.Time) {
  46. t.t, t.d = tt, 0
  47. }
  48. // IsZero returns true the TimeDuration represents the zero value, false
  49. // otherwise.
  50. func (t *TimeDuration) IsZero() bool {
  51. return t.t.IsZero() && t.d == 0
  52. }
  53. // Equal returns if t and other are equal.
  54. func (t *TimeDuration) Equal(other *TimeDuration) bool {
  55. return t.t.Equal(other.t) && t.d == other.d
  56. }
  57. // MarshalJSON implements the json.Marshaler interface. If the time is set it
  58. // will return the time in RFC 3339 format if not it will return the duration
  59. // string.
  60. func (t TimeDuration) MarshalJSON() ([]byte, error) {
  61. switch {
  62. case t.t.IsZero():
  63. if t.d == 0 {
  64. return []byte(`""`), nil
  65. }
  66. return json.Marshal(t.d.String())
  67. default:
  68. return t.t.MarshalJSON()
  69. }
  70. }
  71. // UnmarshalJSON implements the json.Unmarshaler interface. The time is expected
  72. // to be a quoted string in RFC 3339 format or a quoted time.Duration string.
  73. func (t *TimeDuration) UnmarshalJSON(data []byte) error {
  74. var s string
  75. if err := json.Unmarshal(data, &s); err != nil {
  76. return errors.Wrapf(err, "error unmarshaling %s", data)
  77. }
  78. // Empty TimeDuration
  79. if s == "" {
  80. *t = TimeDuration{}
  81. return nil
  82. }
  83. // Try to use the unquoted RFC 3339 format
  84. var tt time.Time
  85. if err := tt.UnmarshalText([]byte(s)); err == nil {
  86. *t = TimeDuration{t: tt}
  87. return nil
  88. }
  89. // Try to use the time.Duration string format
  90. if d, err := time.ParseDuration(s); err == nil {
  91. *t = TimeDuration{d: d}
  92. return nil
  93. }
  94. return errors.Errorf("failed to parse %s", data)
  95. }
  96. // Time calculates the time if needed and returns it.
  97. func (t *TimeDuration) Time() time.Time {
  98. return t.RelativeTime(now())
  99. }
  100. // Unix calculates the time if needed it and returns the Unix time in seconds.
  101. func (t *TimeDuration) Unix() int64 {
  102. return t.RelativeTime(now()).Unix()
  103. }
  104. // RelativeTime returns the embedded time.Time or the base time plus the
  105. // duration if this is not zero.
  106. func (t *TimeDuration) RelativeTime(base time.Time) time.Time {
  107. switch {
  108. case t == nil:
  109. return time.Time{}
  110. case t.t.IsZero():
  111. if t.d == 0 {
  112. return time.Time{}
  113. }
  114. t.t = base.Add(t.d)
  115. return t.t.UTC()
  116. default:
  117. return t.t.UTC()
  118. }
  119. }
  120. // String implements the fmt.Stringer interface.
  121. func (t *TimeDuration) String() string {
  122. return t.Time().String()
  123. }