-
Notifications
You must be signed in to change notification settings - Fork 4
/
date_time.go
187 lines (168 loc) · 4.1 KB
/
date_time.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
package cal
import (
"encoding/json"
"errors"
"time"
"cloud.google.com/go/civil"
"github.com/invopop/gobl/pkg/here"
"github.com/invopop/jsonschema"
"github.com/invopop/validation"
)
// DateTime represents a combination of date and time without location
// specific information nor support for millisecond precision.
type DateTime struct {
civil.DateTime
}
// NewDateTime provides a pointer to a new date time instance.
func NewDateTime(year int, month time.Month, day, hour, minute, second int) *DateTime {
dt := MakeDateTime(year, month, day, hour, minute, second)
return &dt
}
// MakeDateTime provides a new date time instance.
func MakeDateTime(year int, month time.Month, day, hour, minute, second int) DateTime {
return DateTime{
civil.DateTime{
Date: civil.Date{
Year: year,
Month: month,
Day: day,
},
Time: civil.Time{
Hour: hour,
Minute: minute,
Second: second,
},
},
}
}
// ThisSecond produces a new date time instance for the current UTC time
// to the nearest second.
func ThisSecond() DateTime {
t := time.Now().UTC()
return DateTime{
civil.DateTimeOf(t),
}
}
// ThisSecondIn provides a new date time using the current time from the provided
// location as a reference.
func ThisSecondIn(loc *time.Location) DateTime {
t := time.Now().In(loc)
return DateTime{
civil.DateTimeOf(t),
}
}
// DateTimeOf returns the DateTime from the provided time.
func DateTimeOf(t time.Time) DateTime {
return DateTime{
civil.DateTimeOf(t),
}
}
// Clone returns a new pointer to a copy of the date time.
func (dt *DateTime) Clone() *DateTime {
dt2 := *dt
return &dt2
}
// Validate ensures the date time object looks valid
func (dt DateTime) Validate() error {
if dt.IsZero() {
return nil
}
if !dt.DateTime.IsValid() {
return errors.New("invalid date time")
}
return nil
}
// In returns a new time.Time instance with the provided location.
func (dt DateTime) In(loc *time.Location) time.Time {
return dt.DateTime.In(loc)
}
// TimeZ returns a new time.Time instance with the UTC location.
func (dt DateTime) TimeZ() time.Time {
return dt.In(time.UTC)
}
// JSONSchema returns a custom json schema for the date time.
func (DateTime) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Format: "date-time",
Title: "Date Time",
Description: here.Doc(`
Civil date time in simplified ISO format with no time zone
information, for example: 2021-05-26T13:45:00
`),
}
}
// UnmarshalJSON is used to parse a date from json and ensures that
// we can handle invalid data reasonably.
func (dt *DateTime) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
// Zero dates are not great, put pass validation.
if s == "0000-00-00T00:00:00" {
*dt = DateTime{}
return nil
}
dtn, err := civil.ParseDateTime(s)
if err != nil {
return err
}
*dt = DateTime{dtn}
return nil
}
type dateTimeValidationRule struct {
notZero bool
after *DateTime
before *DateTime
}
// Validate is used to check a date time's value.
func (d *dateTimeValidationRule) Validate(value interface{}) error {
in, ok := value.(DateTime)
if !ok {
inp, ok := value.(*DateTime)
if !ok {
return nil
}
if inp == nil {
return nil
}
in = *inp
}
if d.notZero {
if in.IsZero() {
return errors.New("required")
}
}
if d.after != nil {
if !in.DateTime.After(d.after.DateTime) {
return errors.New("too early")
}
}
if d.before != nil {
if !in.DateTime.Before(d.before.DateTime) {
return errors.New("too late")
}
}
return nil
}
// DateTimeNotZero ensures the date is not a zero value.
func DateTimeNotZero() validation.Rule {
return &dateTimeValidationRule{
notZero: true,
}
}
// DateTimeAfter returns a validation rule which checks to ensure the date
// is *after* the provided date.
func DateTimeAfter(dt DateTime) validation.Rule {
return &dateTimeValidationRule{
after: &dt,
}
}
// DateTimeBefore is used during validation to ensure the date is before
// the value passed in.
func DateTimeBefore(dt DateTime) validation.Rule {
return &dateTimeValidationRule{
before: &dt,
}
}