timeduration.go 3.9 KB

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