Fix DateTime.to_iso8601 error by handling NaiveDateTime
- Handle both DateTime and NaiveDateTime structs from database - Convert NaiveDateTime to UTC DateTime before formatting - Add fallback for string timestamps - Fixes FunctionClauseError when processing packets
This commit is contained in:
parent
d389db4c7d
commit
887c35a1c8
2 changed files with 22 additions and 8 deletions
|
|
@ -195,14 +195,28 @@ defmodule AprsmeWeb.Live.Shared.PacketUtils do
|
||||||
"""
|
"""
|
||||||
@spec get_timestamp(map()) :: String.t()
|
@spec get_timestamp(map()) :: String.t()
|
||||||
def get_timestamp(packet) do
|
def get_timestamp(packet) do
|
||||||
cond do
|
received_at = Map.get(packet, :received_at) || Map.get(packet, "received_at")
|
||||||
Map.has_key?(packet, :received_at) && packet.received_at ->
|
|
||||||
DateTime.to_iso8601(packet.received_at)
|
|
||||||
|
|
||||||
Map.has_key?(packet, "received_at") && packet["received_at"] ->
|
case received_at do
|
||||||
DateTime.to_iso8601(packet["received_at"])
|
nil ->
|
||||||
|
""
|
||||||
|
|
||||||
true ->
|
%DateTime{} = dt ->
|
||||||
|
DateTime.to_iso8601(dt)
|
||||||
|
|
||||||
|
%NaiveDateTime{} = ndt ->
|
||||||
|
ndt
|
||||||
|
|> DateTime.from_naive!("Etc/UTC")
|
||||||
|
|> DateTime.to_iso8601()
|
||||||
|
|
||||||
|
timestamp when is_binary(timestamp) ->
|
||||||
|
# If it's already a string, try to parse and reformat it
|
||||||
|
case DateTime.from_iso8601(timestamp) do
|
||||||
|
{:ok, dt, _} -> DateTime.to_iso8601(dt)
|
||||||
|
_ -> timestamp
|
||||||
|
end
|
||||||
|
|
||||||
|
_ ->
|
||||||
""
|
""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ defmodule Aprsme.Repo.Migrations.FixPacketNotifyTrigger do
|
||||||
'rain_24h', NEW.rain_24h,
|
'rain_24h', NEW.rain_24h,
|
||||||
'rain_midnight', NEW.rain_midnight,
|
'rain_midnight', NEW.rain_midnight,
|
||||||
'luminosity', NEW.luminosity,
|
'luminosity', NEW.luminosity,
|
||||||
'snow_24h', NEW.snow_24h,
|
'snow', NEW.snow,
|
||||||
'has_weather', NEW.has_weather,
|
'has_weather', NEW.has_weather,
|
||||||
'inserted_at', NEW.inserted_at
|
'inserted_at', NEW.inserted_at
|
||||||
)::text;
|
)::text;
|
||||||
|
|
@ -113,7 +113,7 @@ defmodule Aprsme.Repo.Migrations.FixPacketNotifyTrigger do
|
||||||
'rain_24h', NEW.rain_24h,
|
'rain_24h', NEW.rain_24h,
|
||||||
'rain_midnight', NEW.rain_midnight,
|
'rain_midnight', NEW.rain_midnight,
|
||||||
'luminosity', NEW.luminosity,
|
'luminosity', NEW.luminosity,
|
||||||
'snow_24h', NEW.snow_24h,
|
'snow', NEW.snow,
|
||||||
'has_weather', NEW.has_weather,
|
'has_weather', NEW.has_weather,
|
||||||
'inserted_at', NEW.inserted_at
|
'inserted_at', NEW.inserted_at
|
||||||
)::text;
|
)::text;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue