-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
205 lines (170 loc) · 3.85 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
198
199
200
201
202
203
204
205
package times
import (
"database/sql/driver"
"encoding"
"fmt"
"time"
)
var (
_ encoding.TextMarshaler = (*Date)(nil)
_ encoding.TextUnmarshaler = (*Date)(nil)
)
type Date struct {
year int
month int
day int
weekday int
t time.Time
}
func NewDate(year, month, day int) *Date {
t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
return DateWithTime(t)
}
func DateWithUnix(seconds int64) *Date {
t := time.Unix(seconds, 0)
return DateWithTime(t)
}
func DateWithTime(t time.Time) *Date {
return &Date{
year: t.Year(),
month: int(t.Month()),
day: t.Day(),
weekday: int(t.Weekday()),
t: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()),
}
}
func Today() *Date {
return DateWithTime(time.Now())
}
func Tomorrow() *Date {
return Today().Add(0, 0, 1)
}
func Yesterday() *Date {
return Today().Add(0, 0, -1)
}
func (d *Date) Year() int {
return d.year
}
func (d *Date) Month() int {
return d.month
}
func (d *Date) Day() int {
return d.day
}
func (d *Date) Weekday() int {
return d.weekday
}
func (d *Date) Unix() int64 {
return d.t.Unix()
}
func (d *Date) Add(years, months, days int) *Date {
return DateWithTime(d.t.AddDate(years, months, days))
}
func (d *Date) Time(hours, minutes int) *Time {
return &Time{
t: d.t.Add(time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute),
}
}
func (d *Date) Equals(date *Date) bool {
return d.year == date.year && d.month == date.month && d.day == date.day
}
func (d *Date) Before(date *Date) bool {
return d.Unix() < date.Unix()
}
func (d *Date) After(date *Date) bool {
return d.Unix() > date.Unix()
}
func (d *Date) Begin() time.Time {
return d.t
}
func (d *Date) End() time.Time {
return time.Date(d.year, time.Month(d.month), d.day, 23, 59, 59, 999999999, d.t.Location())
}
func (d *Date) IsToday() bool {
return Today().Equals(d)
}
func (d *Date) IsTomorrow() bool {
return Tomorrow().Equals(d)
}
func (d *Date) IsYesterday() bool {
return Yesterday().Equals(d)
}
func (d *Date) String() string {
return fmt.Sprintf("%d/%d/%d", d.year, d.month, d.day)
}
func (d *Date) PrettyText() string {
s := fmt.Sprintf("%s %d", time.Month(d.month).String()[:3], d.day)
if d.year == time.Now().Year() {
return s
}
return fmt.Sprintf("%s, %d", s, d.year)
}
func (d *Date) MarshalText() (text []byte, err error) {
return []byte(d.String()), nil
}
func (d *Date) UnmarshalText(text []byte) error {
s := string(text)
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*d = *DateWithTime(t)
return nil
}
func (d *Date) Scan(src interface{}) error {
t, ok := src.(time.Time)
if !ok {
return fmt.Errorf("expect time.Time instead of %T", src)
}
*d = *DateWithTime(t)
return nil
}
func (d *Date) Value() (driver.Value, error) {
if d == nil {
return nil, nil
}
return d.t, nil
}
func (d *Date) NextRepeat(r Repeat) *Date {
switch r {
case Daily:
return d.Add(0, 0, 1)
case Weekly:
return d.Add(0, 0, 7)
case Monthly:
return d.Add(0, 1, 0)
case Yearly:
return d.Add(1, 0, 0)
default:
return nil
}
}
func (d *Date) ShortText() string {
if abs(d.t.Sub(time.Now())) <= 2*Day {
switch {
case d.IsYesterday():
return Localize("Yesterday")
case d.IsToday():
return Localize("Today")
case d.IsTomorrow():
return Localize("Tomorrow")
}
}
return fmt.Sprintf("%s %s", GetWeekdaySymbol(d.weekday), d.PrettyText())
}
func (d *Date) LongText() string {
if abs(d.t.Sub(time.Now())) <= 2*Day {
switch {
case d.IsYesterday():
return Localize("Yesterday") + " " + d.PrettyText()
case d.IsToday():
return Localize("Today") + " " + d.PrettyText()
case d.IsTomorrow():
return Localize("Tomorrow") + " " + d.PrettyText()
}
}
return fmt.Sprintf("%s %s", GetWeekdaySymbol(d.weekday), d.PrettyText())
}
func (d *Date) Range() *Range {
return NewRange(d.Begin(), d.End().Add(time.Nanosecond))
}