-
Notifications
You must be signed in to change notification settings - Fork 0
/
offset_time.go
137 lines (118 loc) Β· 4.09 KB
/
offset_time.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package chrono
// OffsetTime has the same semantics as LocalTime, but with the addition of a timezone offset.
type OffsetTime struct {
v, o int64
}
// OffsetTimeOf returns an OffsetTime that represents the specified hour, minute, second,
// and nanosecond offset within the specified second.
// The supplied offset is applied to the returned OffsetTime in the same manner as OffsetOf.
// A valid time is between 00:00:00 and 99:59:59.999999999. If an invalid time is specified, this function panics.
func OffsetTimeOf(hour, min, sec, nsec, offsetHours, offsetMins int) OffsetTime {
v, err := makeTime(hour, min, sec, nsec)
if err != nil {
panic(err.Error())
}
return OffsetTime{
v: v,
o: makeOffset(offsetHours, offsetMins),
}
}
// OfTimeOffset combines a LocalTime and Offset into an OffsetTime.
func OfTimeOffset(time LocalTime, offset Offset) OffsetTime {
return OffsetTime{
v: time.v,
o: int64(offset),
}
}
// BusinessHour returns the hour specified by t.
// If the hour is greater than 23, that hour is returned without normalization.
func (t OffsetTime) BusinessHour() int {
return timeBusinessHour(t.v)
}
// Clock returns the hour, minute and second represented by t.
// If hour is greater than 23, the returned value is normalized so as to fit within
// the 24-hour clock as specified by ISO 8601, e.g. 25 is returned as 01.
func (t OffsetTime) Clock() (hour, min, sec int) {
hour, min, sec, _ = fromTime(int64(t.v))
return
}
// Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
func (t OffsetTime) Nanosecond() int {
return timeNanoseconds(t.v)
}
// Sub returns the duration t-u.
func (t OffsetTime) Sub(u OffsetTime) Extent {
return Extent(t.utc() - u.utc())
}
// Add returns the time t+v, maintaining the offset of t.
// If the result exceeds the maximum representable time, this function panics.
// If the result would be earlier than 00:00:00, the returned time is moved to the previous day,
// e.g. 01:00:00 - 2 hours = 23:00:00.
func (t OffsetTime) Add(v Extent) OffsetTime {
out, err := addTime(t.v, int64(v))
if err != nil {
panic(err.Error())
}
return OffsetTime{
v: out,
o: t.o,
}
}
// CanAdd returns false if Add would panic if passed the same argument.
func (t OffsetTime) CanAdd(v Extent) bool {
_, err := addTime(t.v, int64(v))
return err == nil
}
// Compare compares t with t2. If t is before t2, it returns -1;
// if t is after t2, it returns 1; if they're the same, it returns 0.
func (t OffsetTime) Compare(t2 OffsetTime) int {
return compareTimes(t.utc(), t2.utc())
}
func (t OffsetTime) String() string {
hour, min, sec, nsec := fromTime(t.v)
return simpleTimeStr(hour, min, sec, nsec, &t.o)
}
// In returns a copy of t, adjusted to the supplied offset.
func (t OffsetTime) In(offset Offset) OffsetTime {
return OffsetTime{
v: t.v - int64(t.o) + int64(offset),
o: int64(offset),
}
}
// UTC is a shortcut for t.In(UTC).
func (t OffsetTime) UTC() OffsetTime {
return OffsetTime{v: t.utc()}
}
// Local returns the LocalTime represented by t.
func (t OffsetTime) Local() LocalTime {
return LocalTime{v: t.v}
}
// Offset returns the offset of t.
func (t OffsetTime) Offset() Offset {
return Offset(t.o)
}
func (t OffsetTime) utc() int64 {
return t.v - int64(t.o)
}
// Format returns a textual representation of the time value formatted according to the layout defined by the argument.
// See the constants section of the documentation to see how to represent the layout format.
// Date format specifiers encountered in the layout results in a panic.
func (t OffsetTime) Format(layout string) string {
out, err := formatDateTimeOffset(layout, nil, &t.v, &t.o)
if err != nil {
panic(err.Error())
}
return out
}
// Parse a formatted string and store the value it represents in t.
// See the constants section of the documentation to see how to represent the layout format.
// Date format specifiers encountered in the layout results in a panic.
func (t *OffsetTime) Parse(layout, value string) error {
v, o := t.v, t.o
if err := parseDateAndTime(layout, value, nil, &v, &o); err != nil {
return err
}
t.v = v
t.o = o
return nil
}