Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Created April 10, 2021 13:14
Show Gist options
  • Save preslavrachev/6d01d88dda4f79d3f7fea8fc47f8f4b7 to your computer and use it in GitHub Desktop.
Save preslavrachev/6d01d88dda4f79d3f7fea8fc47f8f4b7 to your computer and use it in GitHub Desktop.
Duration until a given date
def time_until_reset() do
today = :erlang.date()
now = :erlang.time()
now_in_sec = :calendar.datetime_to_gregorian_seconds({today, now})
days_until_next_monday = 8 - :calendar.day_of_the_week(today)
midnight = {0, 0, 0}
next_monday_in_sec =
:calendar.datetime_to_gregorian_seconds(
{(:calendar.date_to_gregorian_days(today) + days_until_next_monday)
|> :calendar.gregorian_days_to_date(), midnight}
)
sec_diff = next_monday_in_sec - now_in_sec
hour_duration = 60 * 60
hours = Integer.floor_div(sec_diff, hour_duration)
minute_duration = 60
minutes = Integer.floor_div(sec_diff - hour_duration * hours, minute_duration)
seconds = sec_diff - (hours * hour_duration + minutes * minute_duration)
{hours, minutes, seconds}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment