OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package clockflag |
| 6 |
| 7 import ( |
| 8 "time" |
| 9 ) |
| 10 |
| 11 // Time is a flag- and JSON-compatible Time which parses from RFC3339 strings. |
| 12 type Time time.Time |
| 13 |
| 14 // Time returns the Time value associated with this Time. |
| 15 func (t Time) Time() time.Time { |
| 16 return time.Time(t) |
| 17 } |
| 18 |
| 19 // Set implements flag.Value. |
| 20 func (t *Time) Set(value string) error { |
| 21 timeValue, err := time.Parse(time.RFC3339Nano, value) |
| 22 if err != nil { |
| 23 return err |
| 24 } |
| 25 *t = Time(timeValue.UTC()) |
| 26 return nil |
| 27 } |
| 28 |
| 29 // String implement flag.Value. |
| 30 func (t *Time) String() string { |
| 31 return time.Time(*t).String() |
| 32 } |
| 33 |
| 34 // UnmarshalJSON implements json.Unmarshaler. |
| 35 // |
| 36 // Unmarshals a JSON entry into the underlying type. The entry is expected to co
ntain |
| 37 // a string corresponding to one of the enum's keys. |
| 38 func (t *Time) UnmarshalJSON(data []byte) error { |
| 39 var value time.Time |
| 40 if err := value.UnmarshalJSON(data); err != nil { |
| 41 return err |
| 42 } |
| 43 *t = Time(value.UTC()) |
| 44 return nil |
| 45 } |
| 46 |
| 47 // MarshalJSON implements json.Marshaler. |
| 48 // |
| 49 // Marshals a Time into an RFC3339 time string. |
| 50 func (t Time) MarshalJSON() ([]byte, error) { |
| 51 return t.Time().UTC().MarshalJSON() |
| 52 } |
OLD | NEW |