-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocaldate.go
More file actions
199 lines (175 loc) · 3.99 KB
/
Copy pathlocaldate.go
File metadata and controls
199 lines (175 loc) · 3.99 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package localdate
import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/jackc/pgx/v5/pgtype"
"math"
"time"
)
type LocalDate struct {
Days int32
Valid bool
}
const (
daysInfinity = math.MaxInt32
daysNegInfinity = math.MinInt32
)
func NewLocalDate(year int, month time.Month, day int) LocalDate {
t := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
epochDays := int32(t.Unix() / 86400)
return LocalDate{Days: epochDays, Valid: true}
}
func InfinityDate() LocalDate {
return LocalDate{Days: daysInfinity, Valid: true}
}
func NegInfinityDate() LocalDate {
return LocalDate{Days: daysNegInfinity, Valid: true}
}
func (d LocalDate) Time() time.Time {
if d.IsInfinity() {
return time.Time{}
}
return time.Unix(int64(d.Days)*86400, 0).UTC()
}
func (d LocalDate) IsInfinity() bool {
return d.Days == daysInfinity
}
func (d LocalDate) IsNegInfinity() bool {
return d.Days == daysNegInfinity
}
func (d LocalDate) InfinityModifier() int32 {
switch d.Days {
case daysInfinity:
return 1
case daysNegInfinity:
return -1
default:
return 0
}
}
func (d LocalDate) MarshalJSON() ([]byte, error) {
if d.IsInfinity() {
return []byte(`"infinity"`), nil
}
if d.IsNegInfinity() {
return []byte(`"-infinity"`), nil
}
return json.Marshal(d.Time().Format("2006-01-02"))
}
func (d *LocalDate) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "infinity":
d.Days = daysInfinity
return nil
case "-infinity":
d.Days = daysNegInfinity
return nil
default:
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*d = NewLocalDate(t.Year(), t.Month(), t.Day())
return nil
}
}
// SQL scanning
func (d *LocalDate) Scan(value interface{}) error {
switch v := value.(type) {
case time.Time:
*d = NewLocalDate(v.Year(), v.Month(), v.Day())
return nil
case string:
switch v {
case "infinity":
d.Days = daysInfinity
return nil
case "-infinity":
d.Days = daysNegInfinity
return nil
default:
t, err := time.Parse("2006-01-02", v)
if err != nil {
return err
}
*d = NewLocalDate(t.Year(), t.Month(), t.Day())
return nil
}
case nil:
return nil
default:
return fmt.Errorf("unsupported Scan, storing %T into LocalDate", value)
}
}
// SQL value
func (d LocalDate) Value() (driver.Value, error) {
if d.IsInfinity() {
return "infinity", nil
}
if d.IsNegInfinity() {
return "-infinity", nil
}
return d.Time(), nil
}
// pgtype conversion
func (d LocalDate) PgDate() pgtype.Date {
if !d.Valid {
return pgtype.Date{}
}
if modifier := d.InfinityModifier(); modifier != 0 {
return pgtype.Date{
Valid: true,
InfinityModifier: pgtype.InfinityModifier(modifier),
}
}
return pgtype.Date{
Valid: true,
Time: d.Time(),
InfinityModifier: pgtype.Finite,
}
}
func Today() LocalDate {
now := time.Now().UTC()
return NewLocalDate(now.Year(), now.Month(), now.Day())
}
func At(at string) (LocalDate, error) {
t, err := time.Parse("2006-01-02", at)
if err != nil {
return LocalDate{}, err
}
return NewLocalDate(t.Year(), t.Month(), t.Day()), nil
}
func ToLocalDate(t time.Time) LocalDate {
return NewLocalDate(t.Year(), t.Month(), t.Day())
}
func IsEqual(a, b LocalDate) bool {
return a == b
}
func IsAfter(a, b LocalDate) bool {
return a.Days > b.Days
}
func IsBefore(a, b LocalDate) bool {
return a.Days < b.Days
}
func AddDays(a LocalDate, n int) LocalDate {
if a.IsInfinity() || a.IsNegInfinity() {
return a
}
return LocalDate{Days: a.Days + int32(n), Valid: true}
}
// AddDate wraps/replicate the behavior of time.Time and will handle leap years in the same way
func (t LocalDate) AddDate(years int, months int, days int) LocalDate {
if t.IsInfinity() || t.IsNegInfinity() {
return t
}
year, month, day := t.Time().Date()
return NewLocalDate(year+years, month+time.Month(months), day+days)
}
func IsBetween(needle, from, to LocalDate) bool {
return needle.Days >= from.Days && needle.Days <= to.Days
}