forked from maddyblue/sqlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
executable file
·61 lines (51 loc) · 1.43 KB
/
duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package optional
import "time"
// MakeTimeValue returns an Duration with a set value.
func MakeTimeValue(value time.Duration) Duration {
return Duration{ValuePlusOne: value + 1}
}
// HasValue returns true if a value was set.
func (d Duration) HasValue() bool {
return d.ValuePlusOne != 0
}
// Value returns the current value, or 0 if HasValue() is false.
func (d Duration) Value() time.Duration {
if d.ValuePlusOne == 0 {
return 0
}
return d.ValuePlusOne - 1
}
func (d Duration) String() string {
if !d.HasValue() {
return "<unset>"
}
return d.Value().String()
}
// Clear the value.
func (d *Duration) Clear() {
d.ValuePlusOne = 0
}
// Set the value.
func (d *Duration) Set(value time.Duration) {
*d = MakeTimeValue(value)
}
// Add modifies the value by adding a delta.
func (d *Duration) Add(delta time.Duration) {
*d = MakeTimeValue(d.Value() + delta)
}
// MaybeAdd adds the given value, if it is set. Does nothing if other is not
// set.
func (d *Duration) MaybeAdd(other Duration) {
if other.HasValue() {
*d = MakeTimeValue(d.Value() + other.Value())
}
}