github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/micro_time.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1
    18  
    19  import (
    20  	"encoding/json"
    21  	"time"
    22  )
    23  
    24  const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
    25  
    26  // MicroTime is version of Time with microsecond level precision.
    27  //
    28  // +protobuf.options.marshal=false
    29  // +protobuf.as=Timestamp
    30  // +protobuf.options.(gogoproto.goproto_stringer)=false
    31  type MicroTime struct {
    32  	time.Time `protobuf:"-"`
    33  }
    34  
    35  // DeepCopy returns a deep-copy of the MicroTime value.  The underlying time.Time
    36  // type is effectively immutable in the time API, so it is safe to
    37  // copy-by-assign, despite the presence of (unexported) Pointer fields.
    38  func (t *MicroTime) DeepCopyInto(out *MicroTime) {
    39  	*out = *t
    40  }
    41  
    42  // NewMicroTime returns a wrapped instance of the provided time
    43  func NewMicroTime(time time.Time) MicroTime {
    44  	return MicroTime{time}
    45  }
    46  
    47  // DateMicro returns the MicroTime corresponding to the supplied parameters
    48  // by wrapping time.Date.
    49  func DateMicro(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) MicroTime {
    50  	return MicroTime{time.Date(year, month, day, hour, min, sec, nsec, loc)}
    51  }
    52  
    53  // NowMicro returns the current local time.
    54  func NowMicro() MicroTime {
    55  	return MicroTime{time.Now()}
    56  }
    57  
    58  // IsZero returns true if the value is nil or time is zero.
    59  func (t *MicroTime) IsZero() bool {
    60  	if t == nil {
    61  		return true
    62  	}
    63  	return t.Time.IsZero()
    64  }
    65  
    66  // Before reports whether the time instant t is before u.
    67  func (t *MicroTime) Before(u *MicroTime) bool {
    68  	if t != nil && u != nil {
    69  		return t.Time.Before(u.Time)
    70  	}
    71  	return false
    72  }
    73  
    74  // Equal reports whether the time instant t is equal to u.
    75  func (t *MicroTime) Equal(u *MicroTime) bool {
    76  	if t == nil && u == nil {
    77  		return true
    78  	}
    79  	if t != nil && u != nil {
    80  		return t.Time.Equal(u.Time)
    81  	}
    82  	return false
    83  }
    84  
    85  // BeforeTime reports whether the time instant t is before second-lever precision u.
    86  func (t *MicroTime) BeforeTime(u *Time) bool {
    87  	if t != nil && u != nil {
    88  		return t.Time.Before(u.Time)
    89  	}
    90  	return false
    91  }
    92  
    93  // EqualTime reports whether the time instant t is equal to second-lever precision u.
    94  func (t *MicroTime) EqualTime(u *Time) bool {
    95  	if t == nil && u == nil {
    96  		return true
    97  	}
    98  	if t != nil && u != nil {
    99  		return t.Time.Equal(u.Time)
   100  	}
   101  	return false
   102  }
   103  
   104  // UnixMicro returns the local time corresponding to the given Unix time
   105  // by wrapping time.Unix.
   106  func UnixMicro(sec int64, nsec int64) MicroTime {
   107  	return MicroTime{time.Unix(sec, nsec)}
   108  }
   109  
   110  // UnmarshalJSON implements the json.Unmarshaller interface.
   111  func (t *MicroTime) UnmarshalJSON(b []byte) error {
   112  	if len(b) == 4 && string(b) == "null" {
   113  		t.Time = time.Time{}
   114  		return nil
   115  	}
   116  
   117  	var str string
   118  	err := json.Unmarshal(b, &str)
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	pt, err := time.Parse(RFC3339Micro, str)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	t.Time = pt.Local()
   129  	return nil
   130  }
   131  
   132  // UnmarshalQueryParameter converts from a URL query parameter value to an object
   133  func (t *MicroTime) UnmarshalQueryParameter(str string) error {
   134  	if len(str) == 0 {
   135  		t.Time = time.Time{}
   136  		return nil
   137  	}
   138  	// Tolerate requests from older clients that used JSON serialization to build query params
   139  	if len(str) == 4 && str == "null" {
   140  		t.Time = time.Time{}
   141  		return nil
   142  	}
   143  
   144  	pt, err := time.Parse(RFC3339Micro, str)
   145  	if err != nil {
   146  		return err
   147  	}
   148  
   149  	t.Time = pt.Local()
   150  	return nil
   151  }
   152  
   153  // MarshalJSON implements the json.Marshaler interface.
   154  func (t MicroTime) MarshalJSON() ([]byte, error) {
   155  	if t.IsZero() {
   156  		// Encode unset/nil objects as JSON's "null".
   157  		return []byte("null"), nil
   158  	}
   159  
   160  	return json.Marshal(t.UTC().Format(RFC3339Micro))
   161  }
   162  
   163  // OpenAPISchemaType is used by the kube-openapi generator when constructing
   164  // the OpenAPI spec of this type.
   165  //
   166  // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
   167  func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} }
   168  
   169  // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
   170  // the OpenAPI spec of this type.
   171  func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" }
   172  
   173  // MarshalQueryParameter converts to a URL query parameter value
   174  func (t MicroTime) MarshalQueryParameter() (string, error) {
   175  	if t.IsZero() {
   176  		// Encode unset/nil objects as an empty string
   177  		return "", nil
   178  	}
   179  
   180  	return t.UTC().Format(RFC3339Micro), nil
   181  }