Skip to content

Commit f543655

Browse files
authored
Enhancement: Better display of timestamp in callback traces (#872)
* add timezone to traces timepstamp
1 parent 06aef41 commit f543655

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lib/live_debugger/app/debugger/callback_tracing/web/components/trace.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.Components.Trace do
184184
assigns = assign(assigns, :timestamp, timestamp)
185185

186186
~H"""
187-
<.tooltip id={@id <> "-tooltip"} content="timestamp" class="min-w-24">
187+
<.tooltip id={@id <> "-tooltip"} content="Timestamp" class="min-w-12">
188188
<%= Parsers.parse_timestamp(@timestamp) %>
189189
</.tooltip>
190190
"""

lib/live_debugger/app/utils/parsers.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ defmodule LiveDebugger.App.Utils.Parsers do
4242
def parse_timestamp(timestamp) when is_integer(timestamp) and timestamp > 0 do
4343
timestamp
4444
|> DateTime.from_unix(:microsecond)
45+
|> apply_timezone_diff()
4546
|> case do
4647
{:ok, %DateTime{hour: hour, minute: minute, second: second, microsecond: {micro, _}}} ->
4748
"~2..0B:~2..0B:~2..0B.~6..0B"
@@ -142,4 +143,18 @@ defmodule LiveDebugger.App.Utils.Parsers do
142143
|> Module.split()
143144
|> Enum.join(".")
144145
end
146+
147+
defp apply_timezone_diff({:ok, timestamp}) do
148+
{:ok, DateTime.add(timestamp, get_utc_difference_in_minutes(), :minute)}
149+
end
150+
151+
defp apply_timezone_diff(error), do: error
152+
153+
def get_utc_difference_in_minutes do
154+
NaiveDateTime.diff(
155+
NaiveDateTime.local_now(),
156+
DateTime.utc_now() |> DateTime.to_naive(),
157+
:minute
158+
)
159+
end
145160
end

test/app/utils/parsers_test.exs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ defmodule LiveDebugger.App.Utils.ParsersTest do
66
describe "parse_timestamp/1" do
77
test "parses a valid timestamp" do
88
timestamp1 = 1_000_000
9-
assert Parsers.parse_timestamp(timestamp1) == "00:00:01.000000"
9+
10+
timezone_offset_minutes =
11+
NaiveDateTime.diff(
12+
NaiveDateTime.local_now(),
13+
DateTime.utc_now()
14+
|> DateTime.to_naive(),
15+
:minute
16+
)
17+
18+
assert Parsers.parse_timestamp(timestamp1) ==
19+
"00:00:01.000000"
20+
|> apply_timezone_diff(timezone_offset_minutes)
1021

1122
timestamp2 = 60_000_000
12-
assert Parsers.parse_timestamp(timestamp2) == "00:01:00.000000"
23+
24+
assert Parsers.parse_timestamp(timestamp2) ==
25+
"00:01:00.000000" |> apply_timezone_diff(timezone_offset_minutes)
1326
end
1427

1528
test "returns \"Invalid timestamp\" when timestamp is invalid" do
@@ -82,4 +95,11 @@ defmodule LiveDebugger.App.Utils.ParsersTest do
8295
~s(%{key: "value"}, %{foo: :bar})
8396
end
8497
end
98+
99+
defp apply_timezone_diff(time, timezone_offset_minutes) do
100+
time
101+
|> Time.from_iso8601!()
102+
|> Time.add(timezone_offset_minutes, :minute)
103+
|> Calendar.strftime("%H:%M:%S.%6f")
104+
end
85105
end

0 commit comments

Comments
 (0)