-
Notifications
You must be signed in to change notification settings - Fork 2
/
date.go
171 lines (131 loc) · 2.76 KB
/
date.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
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
package date
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
const unpaddedDate = "2006-1-2"
type FormatError string
func (e FormatError) Error() string {
return fmt.Sprintf("date '%s' incorrectly formatted", string(e))
}
type Date struct {
year string
month string
day string
t time.Time
err error
}
type TimeOrDate interface {
IsZero() bool
Format(string) string
Day() int
Month() time.Month
Year() int
}
func New(year, month, day string) Date {
t, err := time.Parse(unpaddedDate, year+"-"+month+"-"+day)
return Date{
year: year,
month: month,
day: day,
t: t,
err: err,
}
}
func Today() Date {
return FromTime(time.Now())
}
func FromTime(t time.Time) Date {
if t.IsZero() {
return Date{}
}
return Date{
year: t.Format("2006"),
month: t.Format("1"),
day: t.Format("2"),
t: t.Truncate(24 * time.Hour),
}
}
func (d Date) Year() int {
return d.t.Year()
}
func (d Date) Month() time.Month {
return d.t.Month()
}
func (d Date) Day() int {
return d.t.Day()
}
func (d Date) YearString() string {
return d.year
}
func (d Date) MonthString() string {
return d.month
}
func (d Date) DayString() string {
return d.day
}
func (d Date) Valid() bool {
return d.err == nil
}
func (d Date) IsZero() bool {
return d.t.IsZero()
}
func (d Date) Format(format string) string {
return d.t.Format(format)
}
func (d Date) String() string {
return d.t.Format(unpaddedDate)
}
func (d Date) Equals(other Date) bool {
return d.String() == other.String()
}
func (d Date) Before(other Date) bool {
return d.t.Before(other.t)
}
func (d Date) After(other Date) bool {
return d.t.After(other.t)
}
func (d Date) AddDate(years, months, days int) Date {
return FromTime(d.t.AddDate(years, months, days))
}
func (d *Date) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
datePart, _, _ := strings.Cut(string(text), "T")
parts := strings.Split(datePart, "-")
if len(parts) != 3 {
return FormatError(text)
}
*d = New(parts[0], parts[1], parts[2])
return nil
}
func (d Date) MarshalText() ([]byte, error) {
if d.IsZero() {
return []byte{}, nil
}
return []byte(d.t.Format(time.DateOnly)), nil
}
func (d *Date) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error {
var s string
if err := attributevalue.Unmarshal(av, &s); err != nil {
return err
}
return d.UnmarshalText([]byte(s))
}
func (d Date) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
text := ""
if !d.IsZero() {
text = d.t.Format(unpaddedDate)
}
return attributevalue.Marshal(text)
}
func (d Date) Time() time.Time {
return d.t
}
func (d Date) Hash() (uint64, error) {
return uint64(d.t.Unix()), nil
}