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

Unified Diff: go/src/infra/libs/clock/clockflag/duration.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Added coverage files. 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
« no previous file with comments | « go/src/infra/libs/clock/clock.infra_testing ('k') | go/src/infra/libs/clock/clockflag/duration_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/clock/clockflag/duration.go
diff --git a/go/src/infra/libs/clock/clockflag/duration.go b/go/src/infra/libs/clock/clockflag/duration.go
new file mode 100644
index 0000000000000000000000000000000000000000..bddccb657f181645286d39fc3b3386a466703a1f
--- /dev/null
+++ b/go/src/infra/libs/clock/clockflag/duration.go
@@ -0,0 +1,61 @@
+// 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 (
+ "errors"
+ "flag"
+ "fmt"
+ "time"
+)
+
+// Duration is a Flag- and JSON-compatible Duration value.
+type Duration time.Duration
+
+var _ flag.Value = (*Duration)(nil)
+
+// Set implements flag.Value.
+func (d *Duration) Set(value string) error {
+ duration, err := time.ParseDuration(value)
+ if err != nil {
+ return err
+ }
+ *d = Duration(duration)
+ return nil
+}
+
+func (d *Duration) String() string {
+ return time.Duration(*d).String()
+}
+
+// Duration returns the Duration value of the flag.
+func (d Duration) Duration() time.Duration {
+ return time.Duration(d)
+}
+
+// IsZero tests if this Duration is the zero value.
+func (d Duration) IsZero() bool {
+ return time.Duration(d) == time.Duration(0)
+}
+
+// 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 (d *Duration) UnmarshalJSON(data []byte) error {
+ // Strip off leading and trailing quotes.
+ s := string(data)
+ if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
+ return errors.New("Duration JSON must be a valid JSON string.")
+ }
+ return d.Set(s[1 : len(s)-1])
+}
+
+// MarshalJSON implements json.Marshaler.
+//
+// Marshals a Duration into a duration string.
+func (d Duration) MarshalJSON() ([]byte, error) {
+ return []byte(fmt.Sprintf(`"%s"`, d.Duration().String())), nil
+}
« no previous file with comments | « go/src/infra/libs/clock/clock.infra_testing ('k') | go/src/infra/libs/clock/clockflag/duration_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698