-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #905. Co-authored-by: Alexey Palazhchenko <alexey.palazhchenko@ferretdb.io>
- Loading branch information
Showing
6 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2021 FerretDB Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tjson | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/FerretDB/FerretDB/internal/util/lazyerrors" | ||
) | ||
|
||
// dateTimeType represents BSON UTC datetime type. | ||
type dateTimeType time.Time | ||
|
||
// tjsontype implements tjsontype interface. | ||
func (dt *dateTimeType) tjsontype() {} | ||
|
||
// String returns formatted time for debugging. | ||
func (dt *dateTimeType) String() string { | ||
return time.Time(*dt).Format(time.RFC3339Nano) | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface. | ||
func (dt *dateTimeType) UnmarshalJSON(data []byte) error { | ||
if bytes.Equal(data, []byte("null")) { | ||
panic("null data") | ||
} | ||
|
||
r := bytes.NewReader(data) | ||
dec := json.NewDecoder(r) | ||
|
||
var o time.Time | ||
if err := dec.Decode(&o); err != nil { | ||
return lazyerrors.Error(err) | ||
} | ||
|
||
if err := checkConsumed(dec, r); err != nil { | ||
return lazyerrors.Error(err) | ||
} | ||
|
||
*dt = dateTimeType(time.UnixMilli(o.UnixMilli()).UTC()) | ||
|
||
return nil | ||
} | ||
|
||
// MarshalJSON implements tjsontype interface. | ||
func (dt *dateTimeType) MarshalJSON() ([]byte, error) { | ||
res, err := json.Marshal(time.UnixMilli(time.Time(*dt).UnixMilli()).UTC()) | ||
if err != nil { | ||
return nil, lazyerrors.Error(err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
// check interfaces | ||
var ( | ||
_ tjsontype = (*dateTimeType)(nil) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2021 FerretDB Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tjson | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/AlekSi/pointer" | ||
) | ||
|
||
var dateTimeTestCases = []testCase{{ | ||
name: "2021", | ||
v: pointer.To(dateTimeType(time.Date(2021, 11, 1, 10, 18, 42, 123000000, time.UTC))), | ||
schema: dateTimeSchema, | ||
j: `"2021-11-01T10:18:42.123Z"`, | ||
}, { | ||
name: "unix_zero", | ||
v: pointer.To(dateTimeType(time.Unix(0, 0).UTC())), | ||
schema: dateTimeSchema, | ||
j: `"1970-01-01T00:00:00Z"`, | ||
}, { | ||
name: "0", | ||
v: pointer.To(dateTimeType(time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC))), | ||
schema: dateTimeSchema, | ||
j: `"0000-01-01T00:00:00Z"`, | ||
}, { | ||
name: "9999", | ||
v: pointer.To(dateTimeType(time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC))), | ||
schema: dateTimeSchema, | ||
j: `"9999-12-31T23:59:59.999Z"`, | ||
}, { | ||
name: "EOF", | ||
schema: stringSchema, | ||
j: `{`, | ||
jErr: `unexpected EOF`, | ||
}} | ||
|
||
func TestDateTime(t *testing.T) { | ||
t.Parallel() | ||
testJSON(t, dateTimeTestCases, func() tjsontype { return new(dateTimeType) }) | ||
} | ||
|
||
func FuzzDateTime(f *testing.F) { | ||
fuzzJSON(f, dateTimeTestCases) | ||
} | ||
|
||
func BenchmarkDateTime(b *testing.B) { | ||
benchmark(b, dateTimeTestCases) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters