-
Notifications
You must be signed in to change notification settings - Fork 0
/
offset.go
136 lines (102 loc) · 2.15 KB
/
offset.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
package chrono
import (
"time"
"github.com/s6n-labs/go-chrono/opt"
)
type FixedOffset struct {
localMinusUTC int32
}
func (o FixedOffset) Fix() FixedOffset {
return o
}
func (o FixedOffset) LocalMinusUTC() int32 {
return o.localMinusUTC
}
func (o FixedOffset) UTCMinusLocal() int32 {
return -o.localMinusUTC
}
func FixedOffsetEast(secs int32) opt.Option[FixedOffset] {
if secs <= -86_400 || secs >= 86_400 {
return opt.None[FixedOffset]()
}
return opt.Some(FixedOffset{
localMinusUTC: secs,
})
}
func FixedOffsetWest(secs int32) opt.Option[FixedOffset] {
if secs <= -86_400 || secs >= 86_400 {
return opt.None[FixedOffset]()
}
return opt.Some(FixedOffset{
localMinusUTC: -secs,
})
}
type Offset interface {
Fix() FixedOffset
}
type LocalResult[T Offset] interface {
Single() opt.Option[T]
Earliest() opt.Option[T]
Latest() opt.Option[T]
}
type None[T Offset] struct{}
func (n None[T]) Single() opt.Option[T] {
return opt.None[T]()
}
func (n None[T]) Earliest() opt.Option[T] {
return opt.None[T]()
}
func (n None[T]) Latest() opt.Option[T] {
return opt.None[T]()
}
type Single[T Offset] struct {
offset T
}
func (s Single[T]) Single() opt.Option[T] {
return opt.Some(s.offset)
}
func (s Single[T]) Earliest() opt.Option[T] {
return opt.Some(s.offset)
}
func (s Single[T]) Latest() opt.Option[T] {
return opt.Some(s.offset)
}
type Ambiguous[T Offset] struct {
min T
max T
}
func (a Ambiguous[T]) Single() opt.Option[T] {
return opt.None[T]()
}
func (a Ambiguous[T]) Earliest() opt.Option[T] {
return opt.Some(a.min)
}
func (a Ambiguous[T]) Latest() opt.Option[T] {
return opt.Some(a.max)
}
type TimeZoneAbstract interface {
Offset
}
type TimeZone interface {
TimeZoneAbstract
}
type TimeZoneImpl[T TimeZoneAbstract] struct{}
type UTC struct {
TimeZoneImpl[UTC]
}
func (u UTC) Fix() FixedOffset {
return FixedOffsetEast(0).Unwrap()
}
func (u UTC) String() string {
return "UTC"
}
type Local struct {
TimeZoneImpl[FixedOffset]
}
func (l Local) Fix() FixedOffset {
_, offset := time.Now().In(time.Local).Zone()
return FixedOffsetEast(int32(offset)).Unwrap()
}
func (l Local) String() string {
return time.Local.String()
}