-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
86 lines (76 loc) · 1.71 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
package scalars
import (
"fmt"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/gobuffalo/buffalo/binding/decoders"
"github.com/gobuffalo/nulls"
)
func MarshalTime(t time.Time) graphql.Marshaler {
return stringWriter(t.Format(time.RFC3339))
}
var decodeTime = decoders.TimeDecoderFn()
func UnmarshalTime(v interface{}) (t time.Time, err error) {
switch v := v.(type) {
case string:
var r interface{}
r, err = decodeTime([]string{v})
if err != nil {
break
}
t = r.(time.Time)
case int:
t = time.Unix(0, int64(v))
default:
err = fmt.Errorf("%T is not a bool", v)
}
return
}
func MarshalNullsTime(t nulls.Time) graphql.Marshaler {
if t.Valid {
return MarshalTime(t.Time)
}
return nullWriter
}
func UnmarshalNullsTime(v interface{}) (nulls.Time, error) {
if v == nil {
return nulls.Time{}, nil
}
t, err := UnmarshalTime(v)
if err != nil {
return nulls.Time{}, err
}
return nulls.Time{Valid: true, Time: t}, nil
}
var weekdays = map[string]time.Weekday{
"Sun": time.Sunday,
"Mon": time.Monday,
"Tue": time.Tuesday,
"Wed": time.Wednesday,
"Thu": time.Thursday,
"Fri": time.Friday,
"Sat": time.Saturday,
"Sunday": time.Sunday,
"Monday": time.Monday,
"Tuesday": time.Tuesday,
"Wednesday": time.Wednesday,
"Thursday": time.Thursday,
"Friday": time.Friday,
"Saturday": time.Saturday,
}
func MarshalWeekday(t time.Weekday) graphql.Marshaler {
return stringWriter(t.String())
}
func UnmarshalWeekday(v interface{}) (w time.Weekday, err error) {
switch v := v.(type) {
case string:
var ok bool
w, ok = weekdays[v]
if !ok {
err = fmt.Errorf("unknown weekday \"%v\"", v)
}
case int:
w = time.Weekday(v)
}
return
}