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

#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


59
60
61
62
63
64
# File 'lib/cassandra/time_uuid.rb', line 59

def <=>(other)
  return nil unless other.is_a?(Cassandra::Uuid)
  c = value <=> other.value
  return c if c == 0 || !other.is_a?(Cassandra::TimeUuid)
  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)


47
48
49
# File 'lib/cassandra/time_uuid.rb', line 47

def to_date
  to_time.to_date
end

#to_timeTime

Returns the time component from this UUID as a Time.

Returns:



34
35
36
37
38
39
40
# File 'lib/cassandra/time_uuid.rb', line 34

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