time.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package unversioned
  14. import (
  15. "encoding/json"
  16. "time"
  17. "github.com/google/gofuzz"
  18. )
  19. // Time is a wrapper around time.Time which supports correct
  20. // marshaling to YAML and JSON. Wrappers are provided for many
  21. // of the factory methods that the time package offers.
  22. //
  23. // +protobuf.options.marshal=false
  24. // +protobuf.as=Timestamp
  25. // +protobuf.options.(gogoproto.goproto_stringer)=false
  26. type Time struct {
  27. time.Time `protobuf:"-"`
  28. }
  29. // DeepCopy returns a deep-copy of the Time value. The underlying time.Time
  30. // type is effectively immutable in the time API, so it is safe to
  31. // copy-by-assign, despite the presence of (unexported) Pointer fields.
  32. func (t Time) DeepCopy() Time {
  33. return t
  34. }
  35. // String returns the representation of the time.
  36. func (t Time) String() string {
  37. return t.Time.String()
  38. }
  39. // NewTime returns a wrapped instance of the provided time
  40. func NewTime(time time.Time) Time {
  41. return Time{time}
  42. }
  43. // Date returns the Time corresponding to the supplied parameters
  44. // by wrapping time.Date.
  45. func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
  46. return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
  47. }
  48. // Now returns the current local time.
  49. func Now() Time {
  50. return Time{time.Now()}
  51. }
  52. // IsZero returns true if the value is nil or time is zero.
  53. func (t *Time) IsZero() bool {
  54. if t == nil {
  55. return true
  56. }
  57. return t.Time.IsZero()
  58. }
  59. // Before reports whether the time instant t is before u.
  60. func (t Time) Before(u Time) bool {
  61. return t.Time.Before(u.Time)
  62. }
  63. // Equal reports whether the time instant t is equal to u.
  64. func (t Time) Equal(u Time) bool {
  65. return t.Time.Equal(u.Time)
  66. }
  67. // Unix returns the local time corresponding to the given Unix time
  68. // by wrapping time.Unix.
  69. func Unix(sec int64, nsec int64) Time {
  70. return Time{time.Unix(sec, nsec)}
  71. }
  72. // Rfc3339Copy returns a copy of the Time at second-level precision.
  73. func (t Time) Rfc3339Copy() Time {
  74. copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
  75. return Time{copied}
  76. }
  77. // UnmarshalJSON implements the json.Unmarshaller interface.
  78. func (t *Time) UnmarshalJSON(b []byte) error {
  79. if len(b) == 4 && string(b) == "null" {
  80. t.Time = time.Time{}
  81. return nil
  82. }
  83. var str string
  84. json.Unmarshal(b, &str)
  85. pt, err := time.Parse(time.RFC3339, str)
  86. if err != nil {
  87. return err
  88. }
  89. t.Time = pt.Local()
  90. return nil
  91. }
  92. // UnmarshalQueryParameter converts from a URL query parameter value to an object
  93. func (t *Time) UnmarshalQueryParameter(str string) error {
  94. if len(str) == 0 {
  95. t.Time = time.Time{}
  96. return nil
  97. }
  98. // Tolerate requests from older clients that used JSON serialization to build query params
  99. if len(str) == 4 && str == "null" {
  100. t.Time = time.Time{}
  101. return nil
  102. }
  103. pt, err := time.Parse(time.RFC3339, str)
  104. if err != nil {
  105. return err
  106. }
  107. t.Time = pt.Local()
  108. return nil
  109. }
  110. // MarshalJSON implements the json.Marshaler interface.
  111. func (t Time) MarshalJSON() ([]byte, error) {
  112. if t.IsZero() {
  113. // Encode unset/nil objects as JSON's "null".
  114. return []byte("null"), nil
  115. }
  116. return json.Marshal(t.UTC().Format(time.RFC3339))
  117. }
  118. // MarshalQueryParameter converts to a URL query parameter value
  119. func (t Time) MarshalQueryParameter() (string, error) {
  120. if t.IsZero() {
  121. // Encode unset/nil objects as an empty string
  122. return "", nil
  123. }
  124. return t.UTC().Format(time.RFC3339), nil
  125. }
  126. // Fuzz satisfies fuzz.Interface.
  127. func (t *Time) Fuzz(c fuzz.Continue) {
  128. if t == nil {
  129. return
  130. }
  131. // Allow for about 1000 years of randomness. Leave off nanoseconds
  132. // because JSON doesn't represent them so they can't round-trip
  133. // properly.
  134. t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0)
  135. }
  136. var _ fuzz.Interface = &Time{}