Add support for decoding timestamp from existing ULID - #38
Open
cocoahero wants to merge 2 commits into
Open
Conversation
dmke
reviewed
Jul 25, 2023
dmke
left a comment
There was a problem hiding this comment.
I am in need of this being a part of the gem :)
| end | ||
| end | ||
|
|
||
| describe 'decoding a timestamp' do |
There was a problem hiding this comment.
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)
endFor 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_rinstead ofto_fwhen comparing the two timestamp values. I used.to_rfor the same reasons as already noted here: https://github.com/rafaelsales/ulid/blob/master/lib/ulid/generator.rb#L59-L69