Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: go/src/infra/libs/clock/time.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/src/infra/libs/clock/time.go
diff --git a/go/src/infra/libs/clock/time.go b/go/src/infra/libs/clock/time.go
new file mode 100644
index 0000000000000000000000000000000000000000..0f290b4a1439bde9c636e408c452e7438f945c72
--- /dev/null
+++ b/go/src/infra/libs/clock/time.go
@@ -0,0 +1,52 @@
+// Copyright (c) 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 clock
+
+import (
+ "time"
+)
+
+// TimeFlag is a flag- and JSON-compatible Time which parses from RFC3339 strings.
+type TimeFlag time.Time
+
+// Time returns the Time value associated with this TimeFlag.
+func (t TimeFlag) Time() time.Time {
+ return time.Time(t)
+}
+
+// Set implements flag.Value.
+func (t *TimeFlag) Set(value string) error {
+ timeValue, err := time.Parse(time.RFC3339Nano, value)
+ if err != nil {
+ return err
+ }
+ *t = TimeFlag(timeValue.UTC())
+ return nil
+}
+
+// String implement flag.Value.
+func (t *TimeFlag) 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 *TimeFlag) UnmarshalJSON(data []byte) error {
+ var value time.Time
+ if err := value.UnmarshalJSON(data); err != nil {
+ return err
+ }
+ *t = TimeFlag(value.UTC())
+ return nil
+}
+
+// MarshalJSON implements json.Marshaler.
+//
+// Marshals a TimeFlag into an RFC3339 time string.
+func (t TimeFlag) MarshalJSON() ([]byte, error) {
+ return t.Time().UTC().MarshalJSON()
+}

Powered by Google App Engine
This is Rietveld 408576698