Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package google | 5 package google |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 ) | 9 ) |
| 10 | 10 |
| 11 const ( | 11 const ( |
| 12 nanosecondsInASecond = int64(time.Second / time.Nanosecond) | 12 nanosecondsInASecond = int64(time.Second / time.Nanosecond) |
| 13 ) | 13 ) |
| 14 | 14 |
| 15 // NewTimestamp creates a new Timestamp protobuf from a time.Time type. | 15 // NewTimestamp creates a new Timestamp protobuf from a time.Time type. |
| 16 func NewTimestamp(t time.Time) *Timestamp { | 16 func NewTimestamp(t time.Time) *Timestamp { |
| 17 if t.IsZero() { | 17 if t.IsZero() { |
| 18 return nil | 18 return nil |
| 19 } | 19 } |
| 20 | 20 |
| 21 return &Timestamp{ | 21 return &Timestamp{ |
| 22 Seconds: t.Unix(), | 22 Seconds: t.Unix(), |
| 23 Nanos: int32(t.Nanosecond()), | 23 Nanos: int32(t.Nanosecond()), |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 // FromTime sets t to the timestamp of the given time.Time value. | |
| 28 func (t *Timestamp) FromTime(other time.Time) { | |
|
Vadim Sh.
2016/04/11 16:52:03
what if 'other' is Zero?
other.Unix() returns som
nodir
2016/04/11 22:16:16
Done.
| |
| 29 t.Seconds = other.Unix() | |
| 30 t.Nanos = int32(other.Nanosecond()) | |
| 31 } | |
| 32 | |
| 27 // Time returns the time.Time associated with a Timestamp protobuf. | 33 // Time returns the time.Time associated with a Timestamp protobuf. |
| 28 func (t *Timestamp) Time() time.Time { | 34 func (t *Timestamp) Time() time.Time { |
| 29 if t == nil { | 35 if t == nil { |
| 30 return time.Time{} | 36 return time.Time{} |
| 31 } | 37 } |
| 32 return time.Unix(t.Seconds, int64(t.Nanos)).UTC() | 38 return time.Unix(t.Seconds, int64(t.Nanos)).UTC() |
| 33 } | 39 } |
| 34 | 40 |
| 35 // NewDuration creates a new Duration protobuf from a time.Duration. | 41 // NewDuration creates a new Duration protobuf from a time.Duration. |
| 36 func NewDuration(d time.Duration) *Duration { | 42 func NewDuration(d time.Duration) *Duration { |
| 37 if d == 0 { | 43 if d == 0 { |
| 38 return nil | 44 return nil |
| 39 } | 45 } |
| 40 | 46 |
| 41 nanos := d.Nanoseconds() | 47 nanos := d.Nanoseconds() |
| 42 return &Duration{ | 48 return &Duration{ |
| 43 Seconds: nanos / nanosecondsInASecond, | 49 Seconds: nanos / nanosecondsInASecond, |
| 44 Nanos: int32(nanos % nanosecondsInASecond), | 50 Nanos: int32(nanos % nanosecondsInASecond), |
| 45 } | 51 } |
| 46 } | 52 } |
| 47 | 53 |
| 48 // Duration returns the time.Duration associated with a Duration protobuf. | 54 // Duration returns the time.Duration associated with a Duration protobuf. |
| 49 func (d *Duration) Duration() time.Duration { | 55 func (d *Duration) Duration() time.Duration { |
| 50 if d == nil { | 56 if d == nil { |
| 51 return 0 | 57 return 0 |
| 52 } | 58 } |
| 53 return (time.Duration(d.Seconds) * time.Second) + (time.Duration(d.Nanos ) * time.Nanosecond) | 59 return (time.Duration(d.Seconds) * time.Second) + (time.Duration(d.Nanos ) * time.Nanosecond) |
| 54 } | 60 } |
| OLD | NEW |