Skip to content

Add support for decoding timestamp from existing ULID - #38

Open
cocoahero wants to merge 2 commits into
rafaelsales:masterfrom
cocoahero:add-decode
Open

Add support for decoding timestamp from existing ULID#38
cocoahero wants to merge 2 commits into
rafaelsales:masterfrom
cocoahero:add-decode

Conversation

@cocoahero

Copy link
Copy Markdown

This is another attempt at @joshbeckman's original PR (#31) which adds support for decoding the timestamp portion of an existing ULID value.

The main difference is this version fixes the flakey spec by using to_r instead of to_f when comparing the two timestamp values. I used .to_r for the same reasons as already noted here: https://github.com/rafaelsales/ulid/blob/master/lib/ulid/generator.rb#L59-L69

@dmke dmke left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in need of this being a part of the gem :)

Comment thread spec/lib/ulid_spec.rb
end
end

describe 'decoding a timestamp' do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ULIDs are case-insensitive. This currently fails:

it 'decodes in a case-insensitve manner' do
  ulid = ULID.generate.downcase
  assert ULID.decode_time(ulid).is_a?(Time)
end

For completeness, I'd like to see a few known input/output pairs (but please don't feel obliged to include them):

it 'correctly parses known test vectors' do
  {
    "0000000000Y2GBSG3FEFT635J1" => Time.at(0),
    "013XRZP292318JWRM98F0YAPV9" => Time.at(1234567891234/1000r),
    "ZZZZZZZZZZ-this-is-ignored" => Time.at(1125899906842623/1000r),
  }.each do |vector, expected_time|
    assert_equal expected_time, ULID.decode_time(vector)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants