-
Notifications
You must be signed in to change notification settings - Fork 2
/
datetime.go
116 lines (91 loc) · 2.48 KB
/
datetime.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
package datetime
import (
"fmt"
"sync"
"time"
pbtimestamp "google.golang.org/protobuf/types/known/timestamppb"
pbdatetime "libs.altipla.consulting/protos/datetime"
)
var (
europeMadrid *time.Location
locOnce sync.Once
)
func EuropeMadrid() *time.Location {
locOnce.Do(func() {
var err error
europeMadrid, err = time.LoadLocation("Europe/Madrid")
if err != nil {
panic(fmt.Sprintf("cannot load location Europe/Madrid: %v", err))
}
})
return europeMadrid
}
// DiffDays returns the difference between the two dates in days.
func DiffDays(a, b time.Time) int64 {
diff := b.Sub(a)
if diff < 0 {
return int64((diff - 12*time.Hour) / (24 * time.Hour))
}
return int64((diff + 12*time.Hour) / (24 * time.Hour))
}
// TimeToDate returns only the date part of the time removing any hour, minutes or seconds.
func TimeToDate(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
// SerializeDate converts a Go time.Time to proto considering only the date part.
func SerializeDate(t time.Time) *pbdatetime.Date {
if t.IsZero() {
return nil
}
year, month, day := t.Date()
return &pbdatetime.Date{
Day: int32(day),
Month: int32(month),
Year: int32(year),
}
}
// ParseDate converts a proto to Go time.Time.
func ParseDate(date *pbdatetime.Date) time.Time {
if date == nil {
return time.Time{}
}
return time.Date(int(date.Year), time.Month(date.Month), int(date.Day), 0, 0, 0, 0, time.UTC)
}
// SerializeTimestamp converts a Go time.Time to proto.
func SerializeTimestamp(t time.Time) *pbtimestamp.Timestamp {
if t.IsZero() {
return nil
}
return pbtimestamp.New(t)
}
// ParseTimestamp converts a proto to Go time.Time.
func ParseTimestamp(timestamp *pbtimestamp.Timestamp) time.Time {
if timestamp == nil {
return time.Time{}
}
return timestamp.AsTime()
}
// DateIsAfter returns if date "a" is after date "b".
func DateIsAfter(a, b *pbdatetime.Date) bool {
return !DateIsEqual(a, b) && !DateIsBefore(a, b)
}
// DateIsEqual returns if date "a" is equal to date "b".
func DateIsEqual(a, b *pbdatetime.Date) bool {
return !DateIsBefore(a, b) && !DateIsBefore(b, a)
}
// DateIsAfter returns if date "a" is before date "b".
func DateIsBefore(a, b *pbdatetime.Date) bool {
if a == nil {
return b != nil
}
switch {
case a.Year < b.Year:
return true
case a.Year == b.Year && a.Month < b.Month:
return true
case a.Year == b.Year && a.Month == b.Month && a.Day < b.Day:
return true
}
return false
}