module BSON::Time
def _bson_to_i
def _bson_to_i # Workaround for JRuby's #to_i rounding negative timestamps up # rather than down (https://github.com/jruby/jruby/issues/6104) if BSON::Environment.jruby? (self - usec.to_r/1000000).to_i else to_i end end
def as_extended_json(**options)
-
(Hash)
- The extended json representation.
Options Hash:
(**opts)
-
:mode
(nil | :relaxed | :legacy
) -- Serialization mode
Other tags:
- Note: - The time is floored to the nearest millisecond.
def as_extended_json(**options) utc_time = utc if options[:mode] == :relaxed && (1970..9999).include?(utc_time.year) if utc_time.usec != 0 if utc_time.respond_to?(:floor) # Ruby 2.7+ utc_time = utc_time.floor(3) else utc_time -= utc_time.usec.divmod(1000).last.to_r / 1000000 end {'$date' => utc_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')} else {'$date' => utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')} end else sec = utc_time._bson_to_i msec = utc_time.usec.divmod(1000).first {'$date' => {'$numberLong' => (sec * 1000 + msec).to_s}} end end
def to_bson(buffer = ByteBuffer.new)
- Since: - 2.0.0
Other tags:
- See: http://bsonspec.org/#/specification -
Returns:
-
(BSON::ByteBuffer)
- The buffer with the encoded object.
Other tags:
- Example: Get the time as encoded BSON. -
Other tags:
- Note: - The time is floored to the nearest millisecond.
def to_bson(buffer = ByteBuffer.new) value = _bson_to_i * 1000 + usec.divmod(1000).first buffer.put_int64(value) end