struct KSUID

Included Modules

Defined in:

ksuid.cr
ksuid/base62.cr
ksuid/error.cr
ksuid/utils.cr

Constant Summary

EPOCH = 14e8.to_u32

KSUID's epoch starts more recently so that the 32-bit number space gives a significantly higher useful lifetime of around 136 years from May 2014. This number (14e8) was picked to be easy to remember.

MAX = from(StaticArray(UInt8, TOTAL_SIZE).new(255_u8))

KSUID with maximum valid value (aWgEPTl1tmebfsQzFP4bxwgy80V).

MIN = from(StaticArray(UInt8, TOTAL_SIZE).new(0_u8))

KSUID with minimum valid value (000000000000000000000000000).

PAYLOAD_SIZE = 16

Payload is 16 bytes.

STRING_ENCODED_SIZE = 27

The length of a KSUID when string (base62) encoded.

TIMESTAMP_SIZE = 4

Timestamp is an UInt32.

TOTAL_SIZE = TIMESTAMP_SIZE + PAYLOAD_SIZE

KSUIDs are 20 bytes:

VERSION = "0.2.0"

Constructors

Instance Method Summary

Constructor Detail

def self.from(string : String) : KSUID #

Converts a base62-encoded String into a KSUID.


[View source]
def self.from(bytes : Bytes) : KSUID #

Converts Bytes into a KSUID.


[View source]
def self.from(array : StaticArray(UInt8, TOTAL_SIZE)) : KSUID #

Converts StaticArray into a KSUID.


[View source]
def self.new(time : Time, payload : Bytes) #

Generates a new KSUID with given time and payload.


[View source]
def self.new(timestamp : Int, payload : Bytes) #

Generates a new KSUID with given timestamp and payload.


[View source]
def self.new(time : Time = Time.now) #

Generates a new KSUID with given time and random #payload.


[View source]

Instance Method Detail

def <=>(other : KSUID) #

Compares the KSUID against other.


[View source]
def ==(other : self) #

def hash(hasher) #
Description copied from struct Struct

See Object#hash(hasher)


def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def payload : StaticArray(UInt8, PAYLOAD_SIZE) #

The 16-byte random payload without the timestamp.


[View source]
def raw : String #

Returns the KSUID as a hex-encoded String.


[View source]
def timestamp : UInt32 #

The timestamp portion of the KSUID as an UInt32 which is uncorrected for KSUID's special EPOCH.


[View source]
def to_s(io : IO) : Nil #

Writes the KSUID as a base62-encoded String to io.


[View source]
def to_slice : Bytes #

Returns the KSUID as Bytes.


[View source]
def to_time : Time #

Returns the timestamp portion of the KSUID as a Time object.


[View source]