Module: BSON::TimeWithZone

Defined in:
lib/bson/time_with_zone.rb

Overview

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

See Also:

Since:

  • 4.4.0

Instance Method Summary collapse

Instance Method Details

#_bson_to_iObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 4.4.0



52
53
54
55
56
57
58
59
60
# File 'lib/bson/time_with_zone.rb', line 52

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

#bson_typeObject

Get the BSON type for the ActiveSupport::TimeWithZone.

As the ActiveSupport::TimeWithZone is converted to a time, this returns the BSON type for time.

Since:

  • 4.4.0



47
48
49
# File 'lib/bson/time_with_zone.rb', line 47

def bson_type
  ::Time::BSON_TYPE
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Get the ActiveSupport::TimeWithZone as encoded BSON.

Examples:

Get the ActiveSupport::TimeWithZone as encoded BSON.

Time.utc(2012, 12, 12, 0, 0, 0).in_time_zone("Pacific Time (US & Canada)").to_bson

Returns:

See Also:

Since:

  • 4.4.0



39
40
41
# File 'lib/bson/time_with_zone.rb', line 39

def to_bson(buffer = ByteBuffer.new)
  buffer.put_int64((to_i * 1000) + (usec / 1000))
end