time.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 v1
  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. // DeepCopyInto creates 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) DeepCopyInto(out *Time) {
  33. *out = *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. if t == nil && u == nil {
  66. return true
  67. }
  68. if t != nil && u != nil {
  69. return t.Time.Equal(u.Time)
  70. }
  71. return false
  72. }
  73. // Unix returns the local time corresponding to the given Unix time
  74. // by wrapping time.Unix.
  75. func Unix(sec int64, nsec int64) Time {
  76. return Time{time.Unix(sec, nsec)}
  77. }
  78. // Rfc3339Copy returns a copy of the Time at second-level precision.
  79. func (t Time) Rfc3339Copy() Time {
  80. copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
  81. return Time{copied}
  82. }
  83. // UnmarshalJSON implements the json.Unmarshaller interface.
  84. func (t *Time) UnmarshalJSON(b []byte) error {
  85. if len(b) == 4 && string(b) == "null" {
  86. t.Time = time.Time{}
  87. return nil
  88. }
  89. var str string
  90. err := json.Unmarshal(b, &str)
  91. if err != nil {
  92. return err
  93. }
  94. pt, err := time.Parse(time.RFC3339, str)
  95. if err != nil {
  96. return err
  97. }
  98. t.Time = pt.Local()
  99. return nil
  100. }
  101. // UnmarshalQueryParameter converts from a URL query parameter value to an object
  102. func (t *Time) UnmarshalQueryParameter(str string) error {
  103. if len(str) == 0 {
  104. t.Time = time.Time{}
  105. return nil
  106. }
  107. // Tolerate requests from older clients that used JSON serialization to build query params
  108. if len(str) == 4 && str == "null" {
  109. t.Time = time.Time{}
  110. return nil
  111. }
  112. pt, err := time.Parse(time.RFC3339, str)
  113. if err != nil {
  114. return err
  115. }
  116. t.Time = pt.Local()
  117. return nil
  118. }
  119. // MarshalJSON implements the json.Marshaler interface.
  120. func (t Time) MarshalJSON() ([]byte, error) {
  121. if t.IsZero() {
  122. // Encode unset/nil objects as JSON's "null".
  123. return []byte("null"), nil
  124. }
  125. return json.Marshal(t.UTC().Format(time.RFC3339))
  126. }
  127. // OpenAPISchemaType is used by the kube-openapi generator when constructing
  128. // the OpenAPI spec of this type.
  129. //
  130. // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
  131. func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
  132. // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
  133. // the OpenAPI spec of this type.
  134. func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
  135. // MarshalQueryParameter converts to a URL query parameter value
  136. func (t Time) MarshalQueryParameter() (string, error) {
  137. if t.IsZero() {
  138. // Encode unset/nil objects as an empty string
  139. return "", nil
  140. }
  141. return t.UTC().Format(time.RFC3339), nil
  142. }
  143. // Fuzz satisfies fuzz.Interface.
  144. func (t *Time) Fuzz(c fuzz.Continue) {
  145. if t == nil {
  146. return
  147. }
  148. // Allow for about 1000 years of randomness. Leave off nanoseconds
  149. // because JSON doesn't represent them so they can't round-trip
  150. // properly.
  151. t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0)
  152. }
  153. var _ fuzz.Interface = &Time{}