@@ -10,12 +10,17 @@ import (
10
10
"github.com/kataras/iris/v12/x/timex"
11
11
)
12
12
13
- // SimpleDateLayout represents the "year-month-day" Go time format.
14
- const SimpleDateLayout = "2006-01-02"
13
+ const (
14
+ // SimpleDateLayout represents the "year-month-day" Go time format.
15
+ SimpleDateLayout = "2006-01-02"
16
+ simpleDateLayoutPostgres = "2006-1-2"
17
+ )
15
18
16
19
// SimpleDate holds a json "year-month-day" time.
17
20
type SimpleDate time.Time
18
21
22
+ var _ Exampler = (* SimpleDate )(nil )
23
+
19
24
// SimpleDateFromTime accepts a "t" Time and returns
20
25
// a SimpleDate. If format fails, it returns the zero value of time.Time.
21
26
func SimpleDateFromTime (t time.Time ) SimpleDate {
@@ -24,6 +29,10 @@ func SimpleDateFromTime(t time.Time) SimpleDate {
24
29
}
25
30
26
31
// ParseSimpleDate reads from "s" and returns the SimpleDate time.
32
+ //
33
+ // The function supports the following formats:
34
+ // - "2024-01-01"
35
+ // - "2024-1-1"
27
36
func ParseSimpleDate (s string ) (SimpleDate , error ) {
28
37
if s == "" || s == "null" {
29
38
return SimpleDate {}, nil
@@ -36,10 +45,16 @@ func ParseSimpleDate(s string) (SimpleDate, error) {
36
45
37
46
tt , err = time .Parse (SimpleDateLayout , s )
38
47
if err != nil {
39
- return SimpleDate {}, err
48
+ // After v5.0.0-alpha.3 of pgx this is coming as "1993-1-1" instead of the stored
49
+ // value "1993-01-01".
50
+ var err2 error
51
+ tt , err2 = time .Parse (simpleDateLayoutPostgres , s )
52
+ if err2 != nil {
53
+ return SimpleDate {}, fmt .Errorf ("%s: %w" , err2 .Error (), err )
54
+ }
40
55
}
41
56
42
- return SimpleDate (tt . UTC () ), nil
57
+ return SimpleDate (tt ), nil
43
58
}
44
59
45
60
// UnmarshalJSON binds the json "data" to "t" with the `SimpleDateLayout`.
@@ -49,11 +64,10 @@ func (t *SimpleDate) UnmarshalJSON(data []byte) error {
49
64
}
50
65
51
66
data = trimQuotes (data )
52
- if len (data ) == 0 {
53
- return nil // as an excepption here, allow empty "" on simple dates, as the server would render it on a response.
54
- }
55
-
56
67
dataStr := string (data )
68
+ if len (dataStr ) == 0 {
69
+ return nil // do not allow empty "" on simple dates.
70
+ }
57
71
58
72
tt , err := time .Parse (SimpleDateLayout , dataStr )
59
73
if err != nil {
@@ -74,6 +88,14 @@ func (t SimpleDate) MarshalJSON() ([]byte, error) {
74
88
return emptyQuoteBytes , nil
75
89
}
76
90
91
+ // Examples returns a list of example values.
92
+ func (t SimpleDate ) ListExamples () any {
93
+ return []string {
94
+ "2024-01-01" ,
95
+ "2024-1-1" ,
96
+ }
97
+ }
98
+
77
99
// IsZero reports whether "t" is zero time.
78
100
// It completes the pg.Zeroer interface.
79
101
func (t SimpleDate ) IsZero () bool {
@@ -85,6 +107,7 @@ func (t SimpleDate) Add(d time.Duration) SimpleDate {
85
107
return SimpleDateFromTime (t .ToTime ().Add (d ))
86
108
}
87
109
110
+ // CountPastDays returns the count of days between "t" and "pastDate".
88
111
func (t SimpleDate ) CountPastDays (pastDate SimpleDate ) int {
89
112
t1 , t2 := t .ToTime (), pastDate .ToTime ()
90
113
return int (t1 .Sub (t2 ).Hours () / 24 )
@@ -113,6 +136,7 @@ func (t SimpleDate) ToTime() time.Time {
113
136
return time .Time (t )
114
137
}
115
138
139
+ // Value completes the pg and native sql driver.Valuer interface.
116
140
func (t SimpleDate ) Value () (driver.Value , error ) {
117
141
return t .String (), nil
118
142
}
0 commit comments