Skip to content

Commit 0d1b5bc

Browse files
authored
CDRIVER-5759 fix relaxed encoding of date after year 9999 (#1772)
Use format `{"$date": {"$numberLong": "<ms from epoch>"}}` rather than `{"$date": "<ISO-8601 date+time>" }`
1 parent 1c9b0c6 commit 0d1b5bc

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/libbson/src/bson/bson.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2619,7 +2619,10 @@ _bson_as_json_visit_date_time (const bson_iter_t *iter, const char *key, int64_t
26192619
BSON_UNUSED (iter);
26202620
BSON_UNUSED (key);
26212621

2622-
if (state->mode == BSON_JSON_MODE_CANONICAL || (state->mode == BSON_JSON_MODE_RELAXED && msec_since_epoch < 0)) {
2622+
const int64_t msec_since_Y10K = 253402300800000; // Milliseconds since 10000-01-01T00:00:00Z.
2623+
2624+
if (state->mode == BSON_JSON_MODE_CANONICAL ||
2625+
(state->mode == BSON_JSON_MODE_RELAXED && (msec_since_epoch < 0 || msec_since_epoch >= msec_since_Y10K))) {
26232626
mcommon_string_append (state->str, "{ \"$date\" : { \"$numberLong\" : \"");
26242627
mcommon_string_append_printf (state->str, "%" PRId64, msec_since_epoch);
26252628
mcommon_string_append (state->str, "\" } }");

src/libbson/tests/json/bson_corpus/datetime.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
{
2525
"description" : "Y10K",
2626
"canonical_bson" : "1000000009610000DC1FD277E6000000",
27+
"relaxed_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}",
2728
"canonical_extjson" : "{\"a\":{\"$date\":{\"$numberLong\":\"253402300800000\"}}}"
2829
},
2930
{

0 commit comments

Comments
 (0)