mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
Implement a simpler unversioned.Time serialization
Time is the number of seconds since the unix epoch and is consistent with our JSON fields.
This commit is contained in:
parent
529bd2bf98
commit
1f012a2bb3
@ -28,8 +28,9 @@ import (
|
|||||||
// of the factory methods that the time package offers.
|
// of the factory methods that the time package offers.
|
||||||
//
|
//
|
||||||
// +protobuf.options.marshal=false
|
// +protobuf.options.marshal=false
|
||||||
|
// +protobuf.as=Timestamp
|
||||||
type Time struct {
|
type Time struct {
|
||||||
time.Time `protobuf:"Timestamp,1,req,name=time"`
|
time.Time `protobuf:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTime returns a wrapped instance of the provided time
|
// NewTime returns a wrapped instance of the provided time
|
||||||
|
@ -22,15 +22,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProtoTime is a struct that is equivalent to Time, but intended for
|
// Timestamp is a struct that is equivalent to Time, but intended for
|
||||||
// protobuf marshalling/unmarshalling. It is generated into a serialization
|
// protobuf marshalling/unmarshalling. It is generated into a serialization
|
||||||
// that matches Time. Do not use in Go structs.
|
// that matches Time. Do not use in Go structs.
|
||||||
type ProtoTime struct {
|
|
||||||
// Represents the time of an event.
|
|
||||||
Timestamp Timestamp `json:"timestamp"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Timestamp is a protobuf Timestamp compatible representation of time.Time
|
|
||||||
type Timestamp struct {
|
type Timestamp struct {
|
||||||
// Represents seconds of UTC time since Unix epoch
|
// Represents seconds of UTC time since Unix epoch
|
||||||
// 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to
|
// 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to
|
||||||
@ -39,20 +33,18 @@ type Timestamp struct {
|
|||||||
// Non-negative fractions of a second at nanosecond resolution. Negative
|
// Non-negative fractions of a second at nanosecond resolution. Negative
|
||||||
// second values with fractions must still have non-negative nanos values
|
// second values with fractions must still have non-negative nanos values
|
||||||
// that count forward in time. Must be from 0 to 999,999,999
|
// that count forward in time. Must be from 0 to 999,999,999
|
||||||
// inclusive.
|
// inclusive. This field may be limited in precision depending on context.
|
||||||
Nanos int32 `json:"nanos"`
|
Nanos int32 `json:"nanos"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProtoTime returns the Time as a new ProtoTime value.
|
// Timestamp returns the Time as a new Timestamp value.
|
||||||
func (m *Time) ProtoTime() *ProtoTime {
|
func (m *Time) ProtoTime() *Timestamp {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return &ProtoTime{}
|
return &Timestamp{}
|
||||||
}
|
}
|
||||||
return &ProtoTime{
|
return &Timestamp{
|
||||||
Timestamp: Timestamp{
|
Seconds: m.Time.Unix(),
|
||||||
Seconds: m.Time.Unix(),
|
Nanos: int32(m.Time.Nanosecond()),
|
||||||
Nanos: int32(m.Time.Nanosecond()),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,11 +53,11 @@ func (m *Time) Size() (n int) { return m.ProtoTime().Size() }
|
|||||||
|
|
||||||
// Reset implements the protobuf marshalling interface.
|
// Reset implements the protobuf marshalling interface.
|
||||||
func (m *Time) Unmarshal(data []byte) error {
|
func (m *Time) Unmarshal(data []byte) error {
|
||||||
p := ProtoTime{}
|
p := Timestamp{}
|
||||||
if err := p.Unmarshal(data); err != nil {
|
if err := p.Unmarshal(data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.Time = time.Unix(p.Timestamp.Seconds, int64(p.Timestamp.Nanos))
|
m.Time = time.Unix(p.Seconds, int64(p.Nanos))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user