Module: BSON::DateTime

Defined in:
lib/bson/date_time.rb

Overview

Injects behaviour for encoding date time values to raw bytes as specified by the BSON spec for time.

See Also:

Since:

  • 2.1.0

Constant Summary collapse

FACTOR =

Constant to multiple the seconds fraction my for millis in REE.

Since:

  • 2.1.0

86400000000

Instance Method Summary collapse

Instance Method Details

#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String

Get the date time as encoded BSON.

Examples:

Get the date time as encoded BSON.

DateTime.new(2012, 1, 1, 0, 0, 0).to_bson

Returns:

  • (String)

    The encoded string.

See Also:

Since:

  • 2.1.0



37
38
39
# File 'lib/bson/date_time.rb', line 37

def to_bson(encoded = ''.force_encoding(BINARY))
  to_time.to_bson(encoded)
end

#to_timeTime

REE does not define a to_time on DateTime, so if we are using REE we define it ourselves.

Examples:

Conver the DateTime to a time.

date_time.to_time

Returns:

  • (Time)

    The converted time.

Since:

  • 2.1.0



57
58
59
# File 'lib/bson/date_time.rb', line 57

def to_time
  ::Time.utc(year, mon, mday, hour, min, sec, (sec_fraction * FACTOR).to_i).getlocal
end