Skip to content

Commit 9d31459

Browse files
authored
graphql.Time unmarshal unix nano time (#486)
1 parent 9379f9d commit 9d31459

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

time.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ func (t *Time) UnmarshalGraphQL(input interface{}) error {
3939
t.Time = time.Unix(int64(input), 0)
4040
return nil
4141
case int64:
42-
t.Time = time.Unix(input, 0)
42+
if input >= 1e10 {
43+
sec := input / 1e9
44+
nsec := input - (sec * 1e9)
45+
t.Time = time.Unix(sec, nsec)
46+
} else {
47+
t.Time = time.Unix(input, 0)
48+
}
4349
return nil
4450
case float64:
4551
t.Time = time.Unix(int64(input), 0)

time_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestTime_ImplementsUnmarshaler(t *testing.T) {
2222
}
2323

2424
func TestTime_ImplementsGraphQLType(t *testing.T) {
25-
gt := new(Time)
25+
gt := &Time{}
2626

2727
if gt.ImplementsGraphQLType("foobar") {
2828
t.Error("Type *Time must not claim to implement GraphQL type 'foobar'")
@@ -36,7 +36,7 @@ func TestTime_ImplementsGraphQLType(t *testing.T) {
3636
func TestTime_MarshalJSON(t *testing.T) {
3737
var err error
3838
var b1, b2 []byte
39-
ref := time.Date(2021, time.April, 20, 12, 3, 23, 0, time.UTC)
39+
ref := time.Date(2021, time.April, 20, 12, 3, 23, 551476231, time.UTC)
4040

4141
if b1, err = json.Marshal(ref); err != nil {
4242
t.Error(err)
@@ -57,8 +57,8 @@ func TestTime_UnmarshalGraphQL(t *testing.T) {
5757
type args struct {
5858
input interface{}
5959
}
60-
61-
ref := time.Date(2021, time.April, 20, 12, 3, 23, 0, time.UTC)
60+
ref := time.Date(2021, time.April, 20, 12, 3, 23, 551476231, time.UTC)
61+
refZeroNano := time.Unix(ref.Unix(), 0)
6262

6363
t.Run("invalid", func(t *testing.T) {
6464
tests := []struct {
@@ -111,46 +111,52 @@ func TestTime_UnmarshalGraphQL(t *testing.T) {
111111
args: args{
112112
input: ref.Format(time.RFC3339),
113113
},
114-
wantEq: ref,
114+
wantEq: refZeroNano,
115115
},
116116
{
117117
name: "bytes",
118118
args: args{
119119
input: []byte(ref.Format(time.RFC3339)),
120120
},
121-
wantEq: ref,
121+
wantEq: refZeroNano,
122122
},
123123
{
124124
name: "int32",
125125
args: args{
126126
input: int32(ref.Unix()),
127127
},
128-
wantEq: ref,
128+
wantEq: refZeroNano,
129129
},
130130
{
131131
name: "int64",
132132
args: args{
133133
input: ref.Unix(),
134134
},
135+
wantEq: refZeroNano,
136+
},
137+
{
138+
name: "int64-nano",
139+
args: args{
140+
input: ref.UnixNano(),
141+
},
135142
wantEq: ref,
136143
},
137144
{
138145
name: "float64",
139146
args: args{
140147
input: float64(ref.Unix()),
141148
},
142-
wantEq: ref,
149+
wantEq: refZeroNano,
143150
},
144151
}
145152

146153
for _, tt := range tests {
147154
t.Run(tt.name, func(t *testing.T) {
148-
gt := new(Time)
155+
gt := &Time{}
149156
if err := gt.UnmarshalGraphQL(tt.args.input); err != nil {
150157
t.Errorf("UnmarshalGraphQL() error = %v", err)
151158
return
152159
}
153-
154160
if !gt.Equal(tt.wantEq) {
155161
t.Errorf("UnmarshalGraphQL() got = %v, want = %v", gt, tt.wantEq)
156162
}

0 commit comments

Comments
 (0)