Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-2187

Convert DateTime instances to Gregorian calendar before serializing to BSON

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Trivial - P5 Trivial - P5
    • bson-4.10.0
    • Affects Version/s: None
    • Component/s: BSON
    • When DateTime instances in non-Gregorian calendars are stored in MongoDB, the times are converted to Gregorian calendar prior to persistence.
    • Minor Change

      The Ruby standard library documentation in https://ruby-doc.org/stdlib-2.6.1/libdoc/date/rdoc/DateTime.html#class-DateTime-label-When+should+you+use+DateTime+and+when+should+you+use+Time-3F explains the difference between Time objects and DateTime objects. The important difference is that DateTime objects support different calendars.

      When we serialize DateTime to BSON we appear to ignore the calendar, such that both dates given in the above example serialize to the same value:

      irb(main):011:0> shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND)
      irb(main):012:0> shakespeare
      => #<DateTime: 1616-04-23T00:00:00+00:00 ((2311415j,0s,0n),+0s,2361222j)>
      irb(main):013:0> shakespeare.to_bson
      => #<BSON::ByteBuffer:0x000055fe5fb4e1b0>
      irb(main):014:0> Hash.from_bson({a:shakespeare}.to_bson)
      => {"a"=>1616-04-23 00:00:00 UTC}
      irb(main):015:0> other = DateTime.iso8601('1616-04-23', Date::ITALY)
      irb(main):016:0> Hash.from_bson({a:other}.to_bson)
      => {"a"=>1616-04-23 00:00:00 UTC}

      Since we convert dates to the beginning of respective day in UTC prior to serialization, it seems consistent to convert DateTime instances to Gregorian calendar before serializing them.

            neil.shweky@mongodb.com Neil Shweky (Inactive)
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            0 Vote for this issue
            3 Start watching this issue