-
Notifications
You must be signed in to change notification settings - Fork 6
/
time.go
124 lines (104 loc) · 2.58 KB
/
time.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package enc
import (
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/Aize-Public/forego/ctx"
)
type Time time.Time
var _ Node = Time{}
var _ json.Marshaler = Time{}
var _ json.Unmarshaler = &Time{}
func (this Time) native() any {
return time.Time(this)
}
func (this Time) GoString() string {
return fmt.Sprintf("enc.Time{%v}", time.Time(this))
}
func (this Time) String() string {
return time.Time(this).Format(time.RFC3339Nano)
//return fmt.Sprintf("%v", time.Time(this).UTC().Truncate(0))
}
func (this Time) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(this).Format(time.RFC3339Nano))
}
func (this *Time) Parse(s string) error {
t, err := time.Parse(time.RFC3339Nano, s)
if err == nil {
*this = Time(t)
return nil
}
t, err2 := time.Parse(time.RFC3339, s) // fallback
if err2 == nil {
*this = Time(t)
return nil
}
return err
}
func (this *Time) UnmarshalJSON(j []byte) error {
var s string
err := json.Unmarshal(j, &s)
if err != nil {
return err
}
return this.Parse(s)
}
func (this Time) unmarshalInto(c ctx.C, handler Handler, into reflect.Value) error {
v := reflect.ValueOf(this)
if v.CanConvert(into.Type()) {
into.Set(v)
return nil
}
return ctx.NewErrorf(c, "can't unmarshal %s %T into %v", handler.path, this, into.Type())
}
// Use this object if you want to get `1s` from time.Second
// the built in json library encode time.Second as 1000000000
// empty string is used for zero duration
type Duration time.Duration
var _ Node = Duration(time.Second)
var _ json.Marshaler = Duration(0)
var _ json.Unmarshaler = (*Duration)(nil)
func (this Duration) native() any {
return time.Duration(this)
}
func (this Duration) GoString() string {
return fmt.Sprintf("enc.Duration{%v}", time.Duration(this))
}
func (this Duration) String() string {
return fmt.Sprintf("%v", time.Duration(this))
}
func (this Duration) unmarshalInto(c ctx.C, handler Handler, into reflect.Value) error {
v := reflect.ValueOf(this)
if v.CanConvert(into.Type()) {
into.Set(v)
return nil
}
return ctx.NewErrorf(c, "can't unmarshal %s %T into %v", handler.path, this, into.Type())
}
func (this Duration) MarshalJSON() ([]byte, error) {
if this == 0 {
return json.Marshal("")
}
return json.Marshal(this.String())
}
func (this *Duration) Parse(s string) error {
if s == "" {
*this = Duration(0)
return nil
}
d, err := time.ParseDuration(s)
if err != nil {
return err
}
*this = Duration(d)
return nil
}
func (this *Duration) UnmarshalJSON(in []byte) error {
var s string
err := json.Unmarshal(in, &s)
if err != nil {
return err
}
return this.Parse(s)
}