defmodule TimeUtil do @moduledoc false def cur_year() do {{year, _, _}, _} = :calendar.local_time year end def cur_ymd() do {ymd, _} = :calendar.local_time ymd end @day_30 [2, 4, 6, 9, 11] @hms {0, 0, 0} def ym_first_last_day() do {{y, m, d}, _} = :calendar.local_time t = cond do m == 2 -> case rem(y, 4) == 0 && rem(y, 100) != 0 do true -> {y, m, 29} _ -> {y, m, 28} end Enum.member?(@day_30, m) -> {y, m, 30} true -> {y, m, 31} end {NaiveDateTime.from_erl!({{y, m, 1}, @hms}), NaiveDateTime.from_erl!({t, @hms})} end def naive_local_now, do: :calendar.local_time |> NaiveDateTime.from_erl! def naive_local_now_string, do: :calendar.local_time |> NaiveDateTime.from_erl! |> NaiveDateTime.to_string def naive_date_today, do: :calendar.local_time |> NaiveDateTime.from_erl! |> NaiveDateTime.to_date def naive_date_today_string, do: :calendar.local_time |> NaiveDateTime.from_erl! |> NaiveDateTime.to_date |> Date.to_string def naive_string_to_date(date_str) do case NaiveDateTime.from_iso8601(date_str) do {:ok, naive_date} -> naive_date _reason -> TimeUtil.naive_local_now() end end end