-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
197 lines (168 loc) · 4.61 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
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
package datetime
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
)
const dateLayout = "2006-01-02"
// EmptyDate is a not initialized Date.
var EmptyDate = Date{}
// Date is a data structure to store date without time.
type Date struct {
time.Time
}
// NewDate returns new date from year, month and day.
func NewDate(year, month, day int) Date {
return Date{time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)}
}
// NewDateFromString returns new date from yyyy-mm-dd string.
func NewDateFromString(date string) (Date, error) {
d, err := time.Parse(dateLayout, date)
if err != nil {
return Date{}, err
}
return NewDateFromTime(d), nil
}
// NewDateFromTime returns new date from time.Time.
func NewDateFromTime(t time.Time) Date {
return Date{time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)}
}
// NowDate returns current active day.
func NowDate(tz *time.Location) Date {
now := time.Now().In(tz)
return NewDate(now.Year(), int(now.Month()), now.Day())
}
// Today returns current active day according to dayStart time.
func Today(dayStart Time, tz *time.Location) Date {
now := time.Now().In(tz)
if now.Hour() < dayStart.Hour() || (now.Hour() == dayStart.Hour() && now.Minute() < dayStart.Minute()) {
now = now.AddDate(0, 0, -1)
}
return NewDate(now.Year(), int(now.Month()), now.Day())
}
// ParseDate tries to parse date (yyyy-mm-dd) using separators: ["-", " ", ".", "-", "_"].
func ParseDate(s string) (Date, error) {
if s == "" {
return Date{}, errors.New("date is empty")
}
seps := []string{"-", " ", ".", "-", "_", "/"}
for _, sep := range seps {
splitted := strings.Split(s, sep)
if len(splitted) == 3 {
year, err := strconv.Atoi(splitted[0])
if err != nil {
return Date{}, fmt.Errorf("parse year=%s: %w", splitted[0], err)
}
month, err := strconv.Atoi(splitted[1])
if err != nil {
return Date{}, fmt.Errorf("parse month=%s: %w", splitted[1], err)
}
day, err := strconv.Atoi(splitted[2])
if err != nil {
return Date{}, fmt.Errorf("parse day=%s: %w", splitted[2], err)
}
return NewDate(year, month, day), nil
}
}
return Date{}, fmt.Errorf("invalid date=%s", s)
}
// SortDates sorts dates.
func SortDates(dates []Date, desc bool) {
sort.Slice(dates, func(i, j int) bool {
if desc {
return dates[i].After(dates[j].Time)
}
return dates[i].Before(dates[j].Time)
})
}
// String returns date in yyyy-mm-dd format.
func (d Date) String() string {
return d.Format(dateLayout)
}
// Round returns new Date instance with Round(0).
func (d Date) Round() Date {
return Date{d.Time.Round(0)}
}
// NextDay returns Date instance for the next day.
func (d Date) NextDay() Date {
d.Time = d.AddDate(0, 0, 1)
return NewDateFromTime(d.Time)
}
// PrevDay returns Date instance for the previous day.
func (d Date) PrevDay() Date {
d.Time = d.AddDate(0, 0, -1)
return NewDateFromTime(d.Time)
}
// IsZero returns true if date is empty.
func (d Date) IsZero() bool {
return d.Time.IsZero()
}
// EqualDate returns true if dates are equal.
func (d Date) EqualDate(other Date) bool {
return d.Day() == other.Day() && d.Month() == other.Month() && d.Year() == other.Year()
}
// Range returns number of days between two dates.
func (d Date) Range(other Date) int {
d1 := d.Unix()
d2 := other.Unix()
r := int(d2 - d1)
if r < 0 {
r *= -1
}
return r / 86400
}
// IsToday returns true if provided argument is today.
func (d Date) IsToday(dayStart Time, tz *time.Location) bool {
return d.EqualDate(Today(dayStart, tz))
}
// IsArgNextDay returns true if provided argument is after Date.
func (d Date) IsArgNextDay(t Date) bool {
if d.Year() < t.Year() {
return true
} else if d.Year() > t.Year() {
return false
}
if d.Month() < t.Month() {
return true
} else if d.Month() > t.Month() {
return false
}
if d.Day() < t.Day() {
return true
} else if d.Day() > t.Day() {
return false
}
return false
}
// MarshalJSON implements json.Marshaler interface to marshal Date to JSON.
func (d Date) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler interface to unmarshal Date from JSON.
func (d *Date) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if s == "" {
return nil
}
res, err := NewDateFromString(s)
if err != nil {
return err
}
d.Time = res.Time
return nil
}
// TransformDatesToString transforms slice of dates to slice of strings.
func TransformDatesToString(dates []Date) []string {
out := make([]string, 0, len(dates))
for _, d := range dates {
out = append(out, d.String())
}
return out
}