time.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. )
  18. // Time is a wrapper around time.Time which supports correct
  19. // marshaling to YAML and JSON. Wrappers are provided for many
  20. // of the factory methods that the time package offers.
  21. //
  22. // +protobuf.options.marshal=false
  23. // +protobuf.as=Timestamp
  24. // +protobuf.options.(gogoproto.goproto_stringer)=false
  25. type Time struct {
  26. time.Time `protobuf:"-"`
  27. }
  28. // DeepCopyInto creates a deep-copy of the Time value. The underlying time.Time
  29. // type is effectively immutable in the time API, so it is safe to
  30. // copy-by-assign, despite the presence of (unexported) Pointer fields.
  31. func (t *Time) DeepCopyInto(out *Time) {
  32. *out = *t
  33. }
  34. // NewTime returns a wrapped instance of the provided time
  35. func NewTime(time time.Time) Time {
  36. return Time{time}
  37. }
  38. // Date returns the Time corresponding to the supplied parameters
  39. // by wrapping time.Date.
  40. func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
  41. return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
  42. }
  43. // Now returns the current local time.
  44. func Now() Time {
  45. return Time{time.Now()}
  46. }
  47. // IsZero returns true if the value is nil or time is zero.
  48. func (t *Time) IsZero() bool {
  49. if t == nil {
  50. return true
  51. }
  52. return t.Time.IsZero()
  53. }
  54. // Before reports whether the time instant t is before u.
  55. func (t *Time) Before(u *Time) bool {
  56. if t != nil && u != nil {
  57. return t.Time.Before(u.Time)
  58. }
  59. return false
  60. }
  61. // Equal reports whether the time instant t is equal to u.
  62. func (t *Time) Equal(u *Time) bool {
  63. if t == nil && u == nil {
  64. return true
  65. }
  66. if t != nil && u != nil {
  67. return t.Time.Equal(u.Time)
  68. }
  69. return false
  70. }
  71. // Unix returns the local time corresponding to the given Unix time
  72. // by wrapping time.Unix.
  73. func Unix(sec int64, nsec int64) Time {
  74. return Time{time.Unix(sec, nsec)}
  75. }
  76. // Rfc3339Copy returns a copy of the Time at second-level precision.
  77. func (t Time) Rfc3339Copy() Time {
  78. copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
  79. return Time{copied}
  80. }
  81. // UnmarshalJSON implements the json.Unmarshaller interface.
  82. func (t *Time) UnmarshalJSON(b []byte) error {
  83. if len(b) == 4 && string(b) == "null" {
  84. t.Time = time.Time{}
  85. return nil
  86. }
  87. var str string
  88. err := json.Unmarshal(b, &str)
  89. if err != nil {
  90. return err
  91. }
  92. pt, err := time.Parse(time.RFC3339, str)
  93. if err != nil {
  94. return err
  95. }
  96. t.Time = pt.Local()
  97. return nil
  98. }
  99. // UnmarshalQueryParameter converts from a URL query parameter value to an object
  100. func (t *Time) UnmarshalQueryParameter(str string) error {
  101. if len(str) == 0 {
  102. t.Time = time.Time{}
  103. return nil
  104. }
  105. // Tolerate requests from older clients that used JSON serialization to build query params
  106. if len(str) == 4 && str == "null" {
  107. t.Time = time.Time{}
  108. return nil
  109. }
  110. pt, err := time.Parse(time.RFC3339, str)
  111. if err != nil {
  112. return err
  113. }
  114. t.Time = pt.Local()
  115. return nil
  116. }
  117. // MarshalJSON implements the json.Marshaler interface.
  118. func (t Time) MarshalJSON() ([]byte, error) {
  119. if t.IsZero() {
  120. // Encode unset/nil objects as JSON's "null".
  121. return []byte("null"), nil
  122. }
  123. buf := make([]byte, 0, len(time.RFC3339)+2)
  124. buf = append(buf, '"')
  125. // time cannot contain non escapable JSON characters
  126. buf = t.UTC().AppendFormat(buf, time.RFC3339)
  127. buf = append(buf, '"')
  128. return buf, nil
  129. }
  130. // ToUnstructured implements the value.UnstructuredConverter interface.
  131. func (t Time) ToUnstructured() interface{} {
  132. if t.IsZero() {
  133. return nil
  134. }
  135. buf := make([]byte, 0, len(time.RFC3339))
  136. buf = t.UTC().AppendFormat(buf, time.RFC3339)
  137. return string(buf)
  138. }
  139. // OpenAPISchemaType is used by the kube-openapi generator when constructing
  140. // the OpenAPI spec of this type.
  141. //
  142. // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
  143. func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
  144. // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
  145. // the OpenAPI spec of this type.
  146. func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
  147. // MarshalQueryParameter converts to a URL query parameter value
  148. func (t Time) MarshalQueryParameter() (string, error) {
  149. if t.IsZero() {
  150. // Encode unset/nil objects as an empty string
  151. return "", nil
  152. }
  153. return t.UTC().Format(time.RFC3339), nil
  154. }