Skip to content

Commit

Permalink
RUBY-1702 Skip creating Time instance in Date serialization (mongodb#118
Browse files Browse the repository at this point in the history
)

* Skip creating Time instance in Date serialization

* Fix spelling and mark constants private

* Move constants to BSON namespace
  • Loading branch information
fanfilmu authored and p-mongo committed Feb 28, 2019
1 parent e9b03c5 commit 297af12
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/bson/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

module BSON

# Julian day of Date 1970-01-01 - UNIX timestamp reference.
#
# @api private
DATE_REFERENCE = ::Date.new(1970, 1, 1).jd

# Number of miliseconds in a day.
#
# @api private
MILLISECONDS_IN_DAY = 60 * 60 * 24 * 1_000

# Injects behaviour for encoding date values to raw bytes as specified by
# the BSON spec for time.
#
Expand All @@ -35,7 +45,7 @@ module Date
#
# @since 2.1.0
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
::Time.utc(year, month, day).to_bson(buffer)
buffer.put_int64((jd - DATE_REFERENCE) * MILLISECONDS_IN_DAY)
end

# Get the BSON type for the date.
Expand Down

0 comments on commit 297af12

Please sign in to comment.