Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 27.3.4.6
elixir 1.19.5-otp-27
erlang 28.4.3
elixir 1.20.0-otp-28
44 changes: 37 additions & 7 deletions lib/hlclock/timestamp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,43 @@ defmodule HLClock.Timestamp do
Recover a Timestamp from the string representation.
"""
def from_string(tstr) do
with {:ok, dt, _} <- tstr |> String.slice(0, 24) |> DateTime.from_iso8601(),
time when is_integer(time) <- DateTime.to_unix(dt, :millisecond),
{counter, _} <- tstr |> String.slice(25, 4) |> Integer.parse(16),
{node, _} <- tstr |> String.slice(30, 16) |> Integer.parse(16),
{:ok, timestamp} <- new(time, counter, node) do
timestamp
end
time =
tstr
|> String.slice(0, 24)
|> DateTime.from_iso8601()
|> case do
{:ok, dt, _} ->
DateTime.to_unix(dt, :millisecond)

_ ->
raise ArgumentError, "invalid timestamp string #{tstr}"
end

counter =
tstr
|> String.slice(25, 4)
|> Integer.parse(16)
|> case do
{counter, _} ->
counter

:error ->
raise ArgumentError, "invalid timestamp string #{tstr}"
end

node =
tstr
|> String.slice(30, 16)
|> Integer.parse(16)
|> case do
{node, _} ->
node

:error ->
raise ArgumentError, "invalid timestamp string #{tstr}"
end

new(time, counter, node)
end

defp compare_node_ids(local_id, remote_id) when local_id == remote_id,
Expand Down
2 changes: 1 addition & 1 deletion test/support/generators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule HLClock.Generators do
def ntp_millis, do: integer(0..@max_time_size)

def int_of_size(size) do
bind(bitstring(length: size), fn <<n::integer-size(size)>> ->
bind(bitstring(length: size), fn <<n::integer-size(^size)>> ->
constant(n)
end)
end
Expand Down