Class: Cassandra::TimeUuid

Inherits:
Uuid
  • Object
show all
Includes:
Comparable
Defined in:
lib/cassandra/time_uuid.rb

Overview

A variant of UUID which can extract its time component.

You can use Uuid::Generator to generate TimeUuids

Instance Method Summary collapse

Methods inherited from Uuid

#hash, #initialize, #to_s, #value

Constructor Details

This class inherits a constructor from Cassandra::Uuid

Instance Method Details

#<=>(other) ⇒ nil, Integer

Compares this timeuuid with another timeuuid

Parameters:

Returns:

  • (nil)

    when other is not a Uuid

  • (Integer)

    -1 when less than other, 0 when equal to other and 1 when greater than other

See Also:

  • Comparable


54
55
56
57
58
59
# File 'lib/cassandra/time_uuid.rb', line 54

def <=>(other)
  return nil unless other.kind_of?(Cassandra::Uuid)
  c = self.value <=> other.value
  return c if c == 0 || !other.is_a?(Cassandra::TimeUuid)
  self.time_bits <=> other.time_bits
end

#to_dateDate

Returns the date component from this UUID as Date.

This just sugar around #to_time

Returns:

  • (Date)


42
43
44
# File 'lib/cassandra/time_uuid.rb', line 42

def to_date
  to_time.to_date
end

#to_timeTime

Returns the time component from this UUID as a Time.

Returns:

  • (Time)


29
30
31
32
33
34
35
# File 'lib/cassandra/time_uuid.rb', line 29

def to_time
  t = time_bits - GREGORIAN_OFFSET
  seconds = t/10_000_000
  microseconds = (t - seconds * 10_000_000)/10.0

  Time.at(seconds, microseconds).utc
end