Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Time breaking change from crystal 0.27.0 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ulid
version: 0.1.0
version: 0.1.1

authors:
- SuperPaintman <[email protected]>
Expand Down
5 changes: 4 additions & 1 deletion spec/ulid_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "./spec_helper"


puts ULID.generate

describe ULID do
describe ".generate : String" do
it "should not raise an error" do
Expand Down Expand Up @@ -51,7 +54,7 @@ describe ULID do
1000.times do
ulid_1 = ULID.generate
sleep 1.millisecond
ulid_2 = ULID.generate(Time.now - 1.second)
ulid_2 = ULID.generate(Time.utc - 1.second)

(ulid_2 < ulid_1).should be_true
end
Expand Down
8 changes: 4 additions & 4 deletions src/ulid.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ULID

# Crockford's Base32
private ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
private ENCODING_LEN = ENCODING.size
private ENCODING_LEN = ENCODING.size.as(Int32)
private TIME_LEN = 10
private RANDOM_LEN = 16

Expand All @@ -15,17 +15,17 @@ module ULID
# ULID.generate
# # => "01B3EAF48P97R8MP9WS6MHDTZ3"
# ```
def generate(seed_time : Time = Time.now) : String
def generate(seed_time : Time = Time.utc) : String
encode_time(seed_time, TIME_LEN) + encode_random(RANDOM_LEN)
end

private def encode_time(now : Time, len : Int32) : String
ms = now.epoch_ms
ms = now.to_unix_ms

String.build do |io|
len.times do |i|
mod = ms % ENCODING_LEN
io << ENCODING[mod]
io << ENCODING[mod.to_i]
ms = (ms - mod) / ENCODING_LEN
end
end.reverse
Expand Down