-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
64 lines (54 loc) · 1.19 KB
/
types.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
package aplos
import (
"encoding/json"
"fmt"
"time"
)
// Time wraps the standard library's time.Time and supports the format returned
// by the Aplos API for time fields.
type Time struct {
time.Time
}
func (t *Time) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("failed to unmarshal JSON field as a string: %w", err)
}
tmp, err := time.Parse("2006-01-02T15:04:05.999-0700", s)
if err != nil {
return fmt.Errorf("failed to parse time: %w", err)
}
*t = Time{tmp}
return err
}
type Date struct {
Year int
Month time.Month
Day int
}
func (d *Date) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("failed to unmarshal JSON field as a string: %w", err)
}
tmp, err := time.Parse("2006-01-02", s)
if err != nil {
return fmt.Errorf("failed to parse date: %w", err)
}
y, m, day := tmp.Date()
*d = Date{
Year: y,
Month: m,
Day: day,
}
return err
}
func (d Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}