Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4023

Dates before epoch get serialized differently from dates after epoch

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.2.0, 4.2.1
    • Component/s: BSON
    • None
    • Environment:
      Windows, Linux (Docker)

      When calling toJson() on a Document containing dates before and after epoch (1970), the result differs. Dates after epoch are represented as a string with an ISO 8601 formatted date, while dates before epoch are represented as the number of milliseconds to epoch.

      Expectation is that all dates get represented the same way.

      Minimal example:

      final String jsonInput = "{" +
          "_id: '1234'," +
          "foo: ISODate('1970-01-01T00:00:00.001Z')," +
          "bar: ISODate('1969-12-31T23:59:59.999Z')," +
          "baz: ISODate('1900-01-01T00:00:00.000Z')" +
          "}";
      final String expectedOutput = "{" +
          "\"_id\": \"1234\", " +
          "\"foo\": {\"$date\": \"1970-01-01T00:00:00.001Z\"}, " +
          "\"bar\": {\"$date\": \"1969-12-31T23:59:59.999Z\"}, " +
          "\"baz\": {\"$date\": \"1900-01-01T00:00:00Z\"}, " +
          "}";
      Document doc = Document.parse(jsonInput);
      String jsonOutput = doc.toJson();
      
      if (!expectedOutput.equalsIgnoreCase(jsonOutput)) {
          System.out.println(jsonOutput);
      };
      
      // prints {"_id": "1234", "foo": {"$date": "1970-01-01T00:00:00.001Z"}, "bar": {"$date": {"$numberLong": "-1"}}, "baz": {"$date": {"$numberLong": "-2208988800000"}}}
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            atorsius@gmail.com Aalbert Torsius
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: