/
date.go
68 lines (56 loc) · 1.17 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
package main
import (
"fmt"
"time"
)
// Date is a calendar day in the UTC timezone
type Date struct {
Year, Month, Day int
}
func (d *Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}
func (d Date) Time() time.Time {
if d.IsZero() {
return time.Time{}
}
return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.UTC)
}
func (d Date) Next() Date {
return DateFromTime(d.Time().AddDate(0, 0, 1))
}
func (d Date) Previous() Date {
return DateFromTime(d.Time().AddDate(0, 0, -1))
}
func (d Date) After(d2 Date) bool {
return d.Time().After(d2.Time())
}
func (d Date) IsZero() bool {
return d == Date{}
}
func DateFromTs(ts int64) Date {
dt := time.Unix(ts, 0).UTC()
return DateFromTime(dt)
}
func DateFromTime(dt time.Time) Date {
dtu := dt.UTC()
return Date{
Year: dtu.Year(),
Month: int(dtu.Month()),
Day: dtu.Day(),
}
}
func DateFromString(s string) (Date, error) {
dt, err := time.ParseInLocation("2006-01-02", s, time.UTC)
if err != nil {
return Date{}, err
}
return Date{
Year: dt.Year(),
Month: int(dt.Month()),
Day: dt.Day(),
}, nil
}
func Today() Date {
return DateFromTime(time.Now())
}