I have this data
%{
day: "sun"
since: ~T[13:30:00],
till: ~T[14:30:00]
}
I am trying to make a DateTime from this, using
Enum.reduce_while(1..7, DateTime.utc_now(), fn _day, acc ->
current_day =
acc
|> Timex.weekday()
|> Timex.day_shortname()
|> String.downcase()
if current_day != hour.day,
do: {:cont, Timex.shift(acc, days: 1)},
else: {:halt, acc}
end)
|> DateTime.to_date()
|> NaiveDateTime.new(hour.since)
|> elem(1)
|> DateTime.from_naive!("Etc/UTC")
This works fine. but I am asking for a suggestion is there can be simpler approach to this?