-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
datetime.go
105 lines (91 loc) · 2.37 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
package types
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/spf13/cast"
)
// DefaultDateLayout specifies the default app date strings layout.
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"
// NowDateTime returns new DateTime instance with the current local time.
func NowDateTime() DateTime {
return DateTime{t: time.Now()}
}
// ParseDateTime creates a new DateTime from the provided value
// (could be [cast.ToTime] supported string, [time.Time], etc.).
func ParseDateTime(value any) (DateTime, error) {
d := DateTime{}
err := d.Scan(value)
return d, err
}
// DateTime represents a [time.Time] instance in UTC that is wrapped
// and serialized using the app default date layout.
type DateTime struct {
t time.Time
}
// Time returns the internal [time.Time] instance.
func (d DateTime) Time() time.Time {
return d.t
}
// IsZero checks whether the current DateTime instance has zero time value.
func (d DateTime) IsZero() bool {
return d.Time().IsZero()
}
// String serializes the current DateTime instance into a formatted
// UTC date string.
//
// The zero value is serialized to an empty string.
func (d DateTime) String() string {
t := d.Time()
if t.IsZero() {
return ""
}
return t.UTC().Format(DefaultDateLayout)
}
// MarshalJSON implements the [json.Marshaler] interface.
func (d DateTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
// UnmarshalJSON implements the [json.Unmarshaler] interface.
func (d *DateTime) UnmarshalJSON(b []byte) error {
var raw string
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
return d.Scan(raw)
}
// Value implements the [driver.Valuer] interface.
func (d DateTime) Value() (driver.Value, error) {
return d.String(), nil
}
// Scan implements [sql.Scanner] interface to scan the provided value
// into the current DateTime instance.
func (d *DateTime) Scan(value any) error {
switch v := value.(type) {
case time.Time:
d.t = v
case DateTime:
d.t = v.Time()
case string:
if v == "" {
d.t = time.Time{}
} else {
t, err := time.Parse(DefaultDateLayout, v)
if err != nil {
// check for other common date layouts
t = cast.ToTime(v)
}
d.t = t
}
case int, int64, int32, uint, uint64, uint32:
d.t = cast.ToTime(v)
default:
str := cast.ToString(v)
if str == "" {
d.t = time.Time{}
} else {
d.t = cast.ToTime(str)
}
}
return nil
}