-
Notifications
You must be signed in to change notification settings - Fork 6
/
convert.go
139 lines (120 loc) · 3.76 KB
/
convert.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
// Copyright 2024 Hardfin, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package date
import (
"database/sql"
"fmt"
"time"
)
// ConvertConfig helps customize the behavior of conversion functions like
// `NullTimeFromPtr()`.
//
// It allows setting the fields in a `time.Time{}` **other** than year, month,
// and day (i.e. the fields that aren't present in a date). By default, these
// are:
// - hour=0
// - minute=0
// - second=0
// - nanosecond=0
// - timezone/loc=time.UTC
type ConvertConfig struct {
Hour int
Minute int
Second int
Nanosecond int
Timezone *time.Location
}
// ConvertOption defines a function that will be applied to a convert config.
type ConvertOption func(*ConvertConfig)
// OptConvertHour returns an option that sets the hour on a convert config.
func OptConvertHour(hour int) ConvertOption {
return func(cc *ConvertConfig) {
cc.Hour = hour
}
}
// OptConvertMinute returns an option that sets the minute on a convert config.
func OptConvertMinute(minute int) ConvertOption {
return func(cc *ConvertConfig) {
cc.Minute = minute
}
}
// OptConvertSecond returns an option that sets the second on a convert config.
func OptConvertSecond(second int) ConvertOption {
return func(cc *ConvertConfig) {
cc.Second = second
}
}
// OptConvertNanosecond returns an option that sets the nanosecond on a convert
// config.
func OptConvertNanosecond(nanosecond int) ConvertOption {
return func(cc *ConvertConfig) {
cc.Nanosecond = nanosecond
}
}
// OptConvertTimezone returns an option that sets the timezone on a convert
// config.
func OptConvertTimezone(tz *time.Location) ConvertOption {
return func(cc *ConvertConfig) {
cc.Timezone = tz
}
}
// NullDateFromPtr converts a `Date` pointer into a `NullDate`.
func NullDateFromPtr(d *Date) NullDate {
if d == nil {
return NullDate{Valid: false}
}
return NullDate{Date: *d, Valid: true}
}
// NullTimeFromPtr converts a date to a native Go `sql.NullTime`; the
// convention in Go is that a **date-only** is parsed (via `time.DateOnly`) as
// `time.Date(YYYY, MM, DD, 0, 0, 0, 0, time.UTC)`.
func NullTimeFromPtr(d *Date, opts ...ConvertOption) sql.NullTime {
if d == nil {
return sql.NullTime{Valid: false}
}
t := d.ToTime(opts...)
return sql.NullTime{Time: t, Valid: true}
}
// FromString parses a string of the form YYYY-MM-DD into a `Date{}`.
func FromString(s string) (Date, error) {
t, err := time.Parse(time.DateOnly, s)
if err != nil {
return Date{}, err
}
year, month, day := t.Date()
d := Date{Year: year, Month: month, Day: day}
return d, nil
}
// FromTime validates that a `time.Time{}` contains a date and converts it to a
// `Date{}`.
func FromTime(t time.Time) (Date, error) {
_, offset := t.Zone()
if t.Hour() != 0 ||
t.Minute() != 0 ||
t.Second() != 0 ||
t.Nanosecond() != 0 ||
offset != 0 {
return Date{}, fmt.Errorf("timestamp contains more than just date information; %s", t.Format(time.RFC3339Nano))
}
year, month, day := t.Date()
d := Date{Year: year, Month: month, Day: day}
return d, nil
}
// InTimezone translates a timestamp into a timezone and then captures the date
// in that timezone.
func InTimezone(t time.Time, tz *time.Location) Date {
tLocal := t.In(tz)
year, month, day := tLocal.Date()
return Date{Year: year, Month: month, Day: day}
}