-
Notifications
You must be signed in to change notification settings - Fork 20
/
timeofday.go
71 lines (59 loc) · 1.94 KB
/
timeofday.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
package utils
import (
"time"
)
// TimeOfDay represents a local time of day value
type TimeOfDay struct {
Hour int
Minute int
Second int
Nanos int
}
// NewTimeOfDay creates a new time of day
func NewTimeOfDay(hour, minute, second, nanos int) TimeOfDay {
return TimeOfDay{Hour: hour, Minute: minute, Second: second, Nanos: nanos}
}
// ExtractTimeOfDay extracts the time of day from the give datetime
func ExtractTimeOfDay(dt time.Time) TimeOfDay {
return NewTimeOfDay(dt.Hour(), dt.Minute(), dt.Second(), dt.Nanosecond())
}
// Equal determines equality for this type
func (t TimeOfDay) Equal(other TimeOfDay) bool {
return t.Hour == other.Hour && t.Minute == other.Minute && t.Second == other.Second && t.Nanos == other.Nanos
}
// Compare compares this time of day to another
func (t TimeOfDay) Compare(other TimeOfDay) int {
if t.Hour != other.Hour {
return t.Hour - other.Hour
}
if t.Minute != other.Minute {
return t.Minute - other.Minute
}
if t.Second != other.Second {
return t.Second - other.Second
}
return t.Nanos - other.Nanos
}
// Combine combines this time and a date to make a datetime
func (t TimeOfDay) Combine(date Date, tz *time.Location) time.Time {
return time.Date(date.Year, time.Month(date.Month), date.Day, t.Hour, t.Minute, t.Second, t.Nanos, tz)
}
// Format formats this time of day as a string
func (t TimeOfDay) Format(layout string) string {
// upgrade us to a date time so we can use standard time.Time formatting
return t.Combine(ZeroDate, time.UTC).Format(layout)
}
// String returns the ISO8601 representation
func (t TimeOfDay) String() string {
return t.Format(iso8601Time)
}
// ZeroTimeOfDay is our uninitialized time of day value
var ZeroTimeOfDay = TimeOfDay{}
// ParseTimeOfDay parses the given string into a time of day
func ParseTimeOfDay(layout string, value string) (TimeOfDay, error) {
dt, err := time.Parse(layout, value)
if err != nil {
return ZeroTimeOfDay, err
}
return ExtractTimeOfDay(dt), nil
}