summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-11 01:04:58 +0000
committerJordan Bracco <href@random.sh>2022-12-11 02:03:36 +0000
commit98be237aba84c9e64391b2f6cd5bc4e686709905 (patch)
tree707d92d7b52d65baf801994e1a44927703fe63b3
parentchore: fix git ignore for config/releases.exs (diff)
fix: alcoolog: time fixes
-rw-r--r--lib/lsg_irc/alcolog_plugin.ex7
-rw-r--r--lib/util.ex25
2 files changed, 29 insertions, 3 deletions
diff --git a/lib/lsg_irc/alcolog_plugin.ex b/lib/lsg_irc/alcolog_plugin.ex
index 46c43f4..f61b237 100644
--- a/lib/lsg_irc/alcolog_plugin.ex
+++ b/lib/lsg_irc/alcolog_plugin.ex
@@ -492,9 +492,9 @@ defmodule LSG.IRC.AlcoologPlugin do
@spec since() :: %{IRC.Account.id() => DateTime.t()}
@doc "Returns the last time the user was at 0 g/l"
def since() do
- :ets.foldr(fn({{acct, timestamp}, _vol, current, _cl, _deg, _name, _comment, _m}, acc) ->
+ :ets.foldr(fn({{acct, timestamp_or_date}, _vol, current, _cl, _deg, _name, _comment, _m}, acc) ->
if !Map.get(acc, acct) && current == 0 do
- date = DateTime.from_unix!(timestamp, :millisecond)
+ date = Util.to_date_time(timestamp_or_date)
Map.put(acc, acct, date)
else
acc
@@ -1198,11 +1198,12 @@ defmodule LSG.IRC.AlcoologPlugin do
date = DateTime.from_unix!(date, :millisecond)
|> Timex.Timezone.convert("Europe/Paris")
else
- date
+ Util.to_naive_date_time(date)
end
now = DateTime.utc_now()
|> Timex.Timezone.convert("Europe/Paris")
{:ok, detail} = Timex.Format.DateTime.Formatters.Default.lformat(date, "({h24}:{m})", "fr")
+
mins_since = round(DateTime.diff(now, date)/60.0)
if ago = format_minute_duration(mins_since) do
word = if mins_since > 0 do
diff --git a/lib/util.ex b/lib/util.ex
index d35157b..ce46174 100644
--- a/lib/util.ex
+++ b/lib/util.ex
@@ -1,5 +1,30 @@
defmodule Util do
+ def to_naive_date_time(naive = %NaiveDateTime{}), do: naive
+ def to_naive_date_time(datetime = %DateTime{}), do: DateTime.to_naive(datetime)
+ def to_naive_date_time(timestamp) when is_integer(timestamp) do
+ timestamp
+ |> to_date_time()
+ |> to_naive_date_time()
+ end
+
+ def to_date_time(naive_or_timestamp, timezone \\ "Europe/Paris")
+
+ def to_date_time(date = %DateTime{}, timezone) do
+ DateTime.shift_zone!(data, timezone, Tzdata.TimeZoneDatabase)
+ end
+
+ def to_date_time(naive = %NaiveDateTime{}, timezone) do
+ DateTime.from_naive!(naive, timezone, Tzdata.TimeZoneDatabase)
+ end
+
+ # todo: this is wrong.
+ def to_date_time(timestamp, timezone) when is_integer(timestamp) do
+ timestamp
+ |> DateTime.from_unix!(:millisecond)
+ |> DateTime.shift_zone!(timezone, Tzdata.TimeZoneDatabase)
+ end
+
def plusminus(number) when number > 0, do: "+#{number}"
def plusminus(0), do: "0"
def plusminus(number) when number < 0, do: "#{number}"