Class: BSON::Timestamp

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/timestamp.rb

Overview

Represents a timestamp type, which is predominately used for sharding.

See Also:

Since:

  • 2.0.0

Constant Summary collapse

BSON_TYPE =

A timestamp is type 0x11 in the BSON spec.

Since:

  • 2.0.0

17.chr.force_encoding(BINARY).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(seconds, increment) ⇒ Timestamp

Instantiate the new timestamp.

Examples:

Instantiate the timestamp.

BSON::Timestamp.new(5, 30)

Parameters:

  • seconds (Integer)

    The number of seconds.

  • increment (Integer)

    The increment value.

Since:

  • 2.0.0



76
77
78
# File 'lib/bson/timestamp.rb', line 76

def initialize(seconds, increment)
  @seconds, @increment = seconds, increment
end

Instance Attribute Details

#incrementObject

Since:

  • 2.0.0



38
# File 'lib/bson/timestamp.rb', line 38

attr_reader :seconds, :increment

#secondsInteger

Returns The number of seconds.

Returns:

  • (Integer)

    The number of seconds.

Since:

  • 2.0.0



38
39
40
# File 'lib/bson/timestamp.rb', line 38

def seconds
  @seconds
end

Class Method Details

.from_bson(bson) ⇒ Timestamp

Deserialize timestamp from BSON.

Parameters:

  • bson (BSON)

    The bson representing a timestamp.

Returns:

See Also:

Since:

  • 2.0.0



104
105
106
# File 'lib/bson/timestamp.rb', line 104

def self.from_bson(bson)
  new(*bson.read(8).unpack(Int32::PACK * 2).reverse)
end

Instance Method Details

#==(other) ⇒ true, false

Determine if this timestamp is equal to another object.

Examples:

Check the timestamp equality.

timestamp == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



50
51
52
53
# File 'lib/bson/timestamp.rb', line 50

def ==(other)
  return false unless other.is_a?(Timestamp)
  seconds == other.seconds && increment == other.increment
end

#as_json(*args) ⇒ Hash

Get the timestamp as JSON hash data.

Examples:

Get the timestamp as a JSON hash.

timestamp.as_json

Returns:

  • (Hash)

    The timestamp as a JSON hash.

Since:

  • 2.0.0



63
64
65
# File 'lib/bson/timestamp.rb', line 63

def as_json(*args)
  { "t" => seconds, "i" => increment }
end

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

Get the timestamp as its encoded raw BSON bytes.

Examples:

Get the timestamp as BSON.

timestamp.to_bson

Returns:

  • (String)

    The raw BSON bytes.

See Also:

Since:

  • 2.0.0



90
91
92
93
# File 'lib/bson/timestamp.rb', line 90

def to_bson(encoded = ''.force_encoding(BINARY))
  increment.to_bson_int32(encoded)
  seconds.to_bson_int32(encoded)
end