-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
time_of_day.go
122 lines (102 loc) · 4.02 KB
/
time_of_day.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package timeofday
import (
"math/rand"
"time"
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// TimeOfDay represents a time of day (no date), stored as microseconds since
// midnight.
type TimeOfDay int64
const (
// Min is the minimum TimeOfDay value (midnight).
Min = TimeOfDay(0)
// Max is the maximum TimeOfDay value (1 microsecond before midnight).
Max = TimeOfDay(microsecondsPerDay - 1)
microsecondsPerSecond = 1e6
microsecondsPerMinute = 60 * microsecondsPerSecond
microsecondsPerHour = 60 * microsecondsPerMinute
microsecondsPerDay = 24 * microsecondsPerHour
nanosPerMicro = 1000
secondsPerDay = 24 * 60 * 60
)
// normalize adjusts the underlying int64 value to fall between Min and Max.
// This is essentially just modding by microsecondsPerDay, with some extra
// finagling to avoid negative values.
func normalize(t TimeOfDay) TimeOfDay {
return (t%microsecondsPerDay + microsecondsPerDay) % microsecondsPerDay
}
// New creates a TimeOfDay representing the specified time.
func New(hour, min, sec, micro int) TimeOfDay {
hours := time.Duration(hour) * time.Hour
minutes := time.Duration(min) * time.Minute
seconds := time.Duration(sec) * time.Second
micros := time.Duration(micro) * time.Microsecond
return normalize(TimeOfDay(int64((hours + minutes + seconds + micros) / time.Microsecond)))
}
func (t TimeOfDay) String() string {
micros := t.Microsecond()
if micros > 0 {
s := fmt.Sprintf("%02d:%02d:%02d.%06d", t.Hour(), t.Minute(), t.Second(), micros)
return strings.TrimRight(s, "0")
}
return fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
}
// FromTime constructs a TimeOfDay from a time.Time, ignoring the date portion.
func FromTime(t time.Time) TimeOfDay {
_, offset := t.Zone()
unixSeconds := t.Unix() + int64(offset)
nanos := (unixSeconds%secondsPerDay)*int64(time.Second) + int64(t.Nanosecond())
return normalize(TimeOfDay(nanos / nanosPerMicro))
}
// ToTime converts a TimeOfDay to a time.Time, using the Unix epoch as the date.
func (t TimeOfDay) ToTime() time.Time {
return timeutil.Unix(0, int64(t)*nanosPerMicro)
}
// Random generates a random TimeOfDay.
func Random(rng *rand.Rand) TimeOfDay {
out := TimeOfDay(rng.Int63n(microsecondsPerDay))
return out
}
// Add adds a Duration to a TimeOfDay, wrapping into the next day if necessary.
func (t TimeOfDay) Add(d duration.Duration) TimeOfDay {
return normalize(TimeOfDay(int64(t) + d.Nanos/nanosPerMicro))
}
// Difference returns the interval between t1 and t2, which may be negative.
func Difference(t1 TimeOfDay, t2 TimeOfDay) duration.Duration {
return duration.Duration{Nanos: int64(t1-t2) * nanosPerMicro}
}
// Hour returns the hour specified by t, in the range [0, 23].
func (t TimeOfDay) Hour() int {
return int(int64(t)%microsecondsPerDay) / microsecondsPerHour
}
// Minute returns the minute offset within the hour specified by t, in the
// range [0, 59].
func (t TimeOfDay) Minute() int {
return int(int64(t)%microsecondsPerHour) / microsecondsPerMinute
}
// Second returns the second offset within the minute specified by t, in the
// range [0, 59].
func (t TimeOfDay) Second() int {
return int(int64(t)%microsecondsPerMinute) / microsecondsPerSecond
}
// Microsecond returns the microsecond offset within the second specified by t,
// in the range [0, 999999].
func (t TimeOfDay) Microsecond() int {
return int(int64(t) % microsecondsPerSecond)
}