Chromium Code Reviews| Index: go/src/infra/libs/clock/clockflag/time.go |
| diff --git a/go/src/infra/libs/clock/clockflag/time.go b/go/src/infra/libs/clock/clockflag/time.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d755c00a2c67fa4d6b78cf8fb4cae8fd44c2a0a |
| --- /dev/null |
| +++ b/go/src/infra/libs/clock/clockflag/time.go |
| @@ -0,0 +1,59 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package clockflag |
| + |
| +import ( |
| + "encoding/json" |
| + "flag" |
| + "time" |
| +) |
| + |
| +// Time is a flag- and JSON-compatible Time which parses from RFC3339 strings. |
| +type Time time.Time |
| + |
| +var ( |
| + _ flag.Value = (*Time)(nil) |
| + _ json.Unmarshaler = (*Time)(nil) |
| + _ json.Marshaler = (*Time)(nil) |
| +) |
|
iannucci
2015/06/03 17:37:19
or
var _ interface {
flag.Value
json.Unmarshale
dnj
2015/06/03 18:21:26
Oh cool, didn't think of this notation. Done.
|
| + |
| +// Time returns the Time value associated with this Time. |
| +func (t Time) Time() time.Time { |
| + return time.Time(t) |
| +} |
| + |
| +// Set implements flag.Value. |
| +func (t *Time) Set(value string) error { |
| + timeValue, err := time.Parse(time.RFC3339Nano, value) |
| + if err != nil { |
| + return err |
| + } |
| + *t = Time(timeValue.UTC()) |
| + return nil |
| +} |
| + |
| +func (t *Time) String() string { |
| + return time.Time(*t).String() |
| +} |
| + |
| +// UnmarshalJSON implements json.Unmarshaler. |
| +// |
| +// Unmarshals a JSON entry into the underlying type. The entry is expected to contain |
| +// a string corresponding to one of the enum's keys. |
| +func (t *Time) UnmarshalJSON(data []byte) error { |
| + var value time.Time |
| + if err := value.UnmarshalJSON(data); err != nil { |
| + return err |
| + } |
| + *t = Time(value.UTC()) |
| + return nil |
| +} |
| + |
| +// MarshalJSON implements json.Marshaler. |
| +// |
| +// Marshals a Time into an RFC3339 time string. |
| +func (t Time) MarshalJSON() ([]byte, error) { |
| + return t.Time().UTC().MarshalJSON() |
| +} |