...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package strfmt
16
17 import (
18 "database/sql/driver"
19 "encoding/json"
20 "errors"
21 "fmt"
22 "time"
23
24 "go.mongodb.org/mongo-driver/bson"
25 )
26
27 func init() {
28 d := Date{}
29
30 Default.Add("date", &d, IsDate)
31 }
32
33
34 func IsDate(str string) bool {
35 _, err := time.Parse(RFC3339FullDate, str)
36 return err == nil
37 }
38
39 const (
40
41
42 RFC3339FullDate = "2006-01-02"
43 )
44
45
46
47
48 type Date time.Time
49
50
51 func (d Date) String() string {
52 return time.Time(d).Format(RFC3339FullDate)
53 }
54
55
56 func (d *Date) UnmarshalText(text []byte) error {
57 if len(text) == 0 {
58 return nil
59 }
60 dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)
61 if err != nil {
62 return err
63 }
64 *d = Date(dd)
65 return nil
66 }
67
68
69 func (d Date) MarshalText() ([]byte, error) {
70 return []byte(d.String()), nil
71 }
72
73
74 func (d *Date) Scan(raw interface{}) error {
75 switch v := raw.(type) {
76 case []byte:
77 return d.UnmarshalText(v)
78 case string:
79 return d.UnmarshalText([]byte(v))
80 case time.Time:
81 *d = Date(v)
82 return nil
83 case nil:
84 *d = Date{}
85 return nil
86 default:
87 return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
88 }
89 }
90
91
92 func (d Date) Value() (driver.Value, error) {
93 return driver.Value(d.String()), nil
94 }
95
96
97 func (d Date) MarshalJSON() ([]byte, error) {
98 return json.Marshal(time.Time(d).Format(RFC3339FullDate))
99 }
100
101
102 func (d *Date) UnmarshalJSON(data []byte) error {
103 if string(data) == jsonNull {
104 return nil
105 }
106 var strdate string
107 if err := json.Unmarshal(data, &strdate); err != nil {
108 return err
109 }
110 tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)
111 if err != nil {
112 return err
113 }
114 *d = Date(tt)
115 return nil
116 }
117
118 func (d Date) MarshalBSON() ([]byte, error) {
119 return bson.Marshal(bson.M{"data": d.String()})
120 }
121
122 func (d *Date) UnmarshalBSON(data []byte) error {
123 var m bson.M
124 if err := bson.Unmarshal(data, &m); err != nil {
125 return err
126 }
127
128 if data, ok := m["data"].(string); ok {
129 rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
130 if err != nil {
131 return err
132 }
133 *d = Date(rd)
134 return nil
135 }
136
137 return errors.New("couldn't unmarshal bson bytes value as Date")
138 }
139
140
141 func (d *Date) DeepCopyInto(out *Date) {
142 *out = *d
143 }
144
145
146 func (d *Date) DeepCopy() *Date {
147 if d == nil {
148 return nil
149 }
150 out := new(Date)
151 d.DeepCopyInto(out)
152 return out
153 }
154
155
156 func (d Date) GobEncode() ([]byte, error) {
157 return d.MarshalBinary()
158 }
159
160
161 func (d *Date) GobDecode(data []byte) error {
162 return d.UnmarshalBinary(data)
163 }
164
165
166 func (d Date) MarshalBinary() ([]byte, error) {
167 return time.Time(d).MarshalBinary()
168 }
169
170
171 func (d *Date) UnmarshalBinary(data []byte) error {
172 var original time.Time
173
174 err := original.UnmarshalBinary(data)
175 if err != nil {
176 return err
177 }
178
179 *d = Date(original)
180
181 return nil
182 }
183
184
185 func (d Date) Equal(d2 Date) bool {
186 return time.Time(d).Equal(time.Time(d2))
187 }
188
View as plain text