/
date.go
49 lines (42 loc) · 1.33 KB
/
date.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
package types
import (
"encoding/json"
"time"
)
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#DateFormat
const DateFormat = "2006-01-02"
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#Date
type Date struct {
time.Time
}
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#MarshalJSON
func (d Date) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Time.Format(DateFormat))
}
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#UnmarshalJSON
func (d *Date) UnmarshalJSON(data []byte) error {
var dateStr string
err := json.Unmarshal(data, &dateStr)
if err != nil {
return err
}
parsed, err := time.Parse(DateFormat, dateStr)
if err != nil {
return err
}
d.Time = parsed
return nil
}
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#String
func (d Date) String() string {
return d.Time.Format(DateFormat)
}
// Deprecated: This has been replaced by https://pkg.go.dev/github.com/oapi-codegen/runtime/types#UnmarshalText
func (d *Date) UnmarshalText(data []byte) error {
parsed, err := time.Parse(DateFormat, string(data))
if err != nil {
return err
}
d.Time = parsed
return nil
}