philomena/lib/philomena_query/parse/date_parser.ex

214 lines
5.4 KiB
Elixir
Raw Permalink Normal View History

defmodule PhilomenaQuery.Parse.DateParser do
@moduledoc false
2019-11-02 19:34:25 +01:00
import NimbleParsec
2020-08-08 02:23:36 +02:00
@dialyzer [:no_match, :no_unused]
2019-11-02 19:34:25 +01:00
defp build_datetime(naive, tz_off, tz_hour, tz_minute) do
tz_hour =
tz_hour
|> Integer.to_string()
|> String.pad_leading(2, "0")
tz_minute =
tz_minute
|> Integer.to_string()
|> String.pad_leading(2, "0")
iso8601_string = "#{NaiveDateTime.to_iso8601(naive)}#{tz_off}#{tz_hour}#{tz_minute}"
# Unbelievable that there is no way to build this with integer arguments.
# WTF, Elixir?
case DateTime.from_iso8601(iso8601_string) do
{:ok, datetime, _offset} ->
{:ok, datetime}
2019-11-02 19:34:25 +01:00
_ ->
:error
end
2019-11-02 19:34:25 +01:00
end
defp timezone_bounds([]), do: ["+", 0, 0]
defp timezone_bounds([tz_off, tz_hour]), do: [tz_off, tz_hour, 0]
defp timezone_bounds([tz_off, tz_hour, tz_minute]), do: [tz_off, tz_hour, tz_minute]
2021-12-26 22:48:33 +01:00
defp days_in_year(year) do
case Calendar.ISO.leap_year?(year) do
true -> 366
_ -> 365
end
end
defp days_in_month(year, month) when month in 1..12 do
Calendar.ISO.days_in_month(year, month)
end
2020-01-11 05:20:19 +01:00
defp days_in_month(_year, _month) do
0
end
defp lower_upper(tuple, offset_amount) do
case NaiveDateTime.from_erl(tuple) do
{:ok, lower} ->
upper = NaiveDateTime.add(lower, offset_amount, :second)
{:ok, [lower, upper]}
_ ->
:error
end
end
2019-11-02 19:34:25 +01:00
defp date_bounds([year]) do
2021-12-26 22:48:33 +01:00
days = days_in_year(year)
lower_upper({{year, 1, 1}, {0, 0, 0}}, 86_400 * days)
2019-11-02 19:34:25 +01:00
end
defp date_bounds([year, month]) do
days = days_in_month(year, month)
lower_upper({{year, month, 1}, {0, 0, 0}}, 86_400 * days)
2019-11-02 19:34:25 +01:00
end
defp date_bounds([year, month, day]) do
lower_upper({{year, month, day}, {0, 0, 0}}, 86_400)
2019-11-02 19:34:25 +01:00
end
defp date_bounds([year, month, day, hour]) do
lower_upper({{year, month, day}, {hour, 0, 0}}, 3_600)
2019-11-02 19:34:25 +01:00
end
defp date_bounds([year, month, day, hour, minute]) do
lower_upper({{year, month, day}, {hour, minute, 0}}, 60)
2019-11-02 19:34:25 +01:00
end
defp date_bounds([year, month, day, hour, minute, second]) do
lower_upper({{year, month, day}, {hour, minute, second}}, 1)
2019-11-02 19:34:25 +01:00
end
defp absolute_datetime(_rest, opts, context, _line, _offset) do
2019-11-02 19:34:25 +01:00
date = Keyword.fetch!(opts, :date)
timezone = Keyword.get(opts, :timezone, [])
[tz_off, tz_hour, tz_minute] = timezone_bounds(timezone)
with {:ok, [lower, upper]} <- date_bounds(date),
{:ok, lower} <- build_datetime(lower, tz_off, tz_hour, tz_minute),
2020-01-11 05:20:19 +01:00
{:ok, upper} <- build_datetime(upper, tz_off, tz_hour, tz_minute) do
{[[lower, upper]], context}
else
_ ->
date = Enum.join(date ++ timezone, ", ")
{:error, "invalid date format in input, parsed as #{date}"}
end
2019-11-02 19:34:25 +01:00
end
defp relative_datetime(_rest, [count, scale], context, _line, _offset) do
millennium_seconds = 31_536_000_000
2019-11-02 19:34:25 +01:00
case count * scale <= millennium_seconds do
true ->
now = DateTime.utc_now()
2019-11-02 19:34:25 +01:00
lower = DateTime.add(now, (count + 1) * -scale, :second)
upper = DateTime.add(now, count * -scale, :second)
{[[lower, upper]], context}
_false ->
2020-04-12 02:28:28 +02:00
{:error,
"invalid date format in input; requested time #{count * scale} seconds is over a millennium ago"}
end
2019-11-02 19:34:25 +01:00
end
space =
choice([string(" "), string("\t"), string("\n"), string("\r"), string("\v"), string("\f")])
|> ignore()
year = integer(4)
month = integer(2)
day = integer(2)
2019-11-02 19:34:25 +01:00
hour = integer(2)
minute = integer(2)
second = integer(2)
tz_hour = integer(2)
tz_minute = integer(2)
ymd_sep = ignore(string("-"))
hms_sep = ignore(string(":"))
iso8601_sep = ignore(choice([string("T"), string("t"), space]))
iso8601_tzsep = choice([string("+"), string("-")])
zulu = ignore(choice([string("Z"), string("z")]))
date_part =
year
|> optional(
ymd_sep
|> concat(month)
|> optional(
ymd_sep
|> concat(day)
|> optional(
iso8601_sep
|> optional(
hour
|> optional(
hms_sep
|> concat(minute)
|> optional(concat(hms_sep, second))
)
)
)
)
)
|> tag(:date)
timezone_part =
choice([
iso8601_tzsep
|> concat(tz_hour)
|> optional(
hms_sep
|> concat(tz_minute)
)
|> tag(:timezone),
zulu
])
absolute_date =
date_part
|> optional(timezone_part)
|> eos()
|> post_traverse(:absolute_datetime)
2019-11-02 19:34:25 +01:00
|> unwrap_and_tag(:date)
relative_date =
integer(min: 1)
|> ignore(concat(space, empty()))
|> choice([
string("second") |> optional(string("s")) |> replace(1),
string("minute") |> optional(string("s")) |> replace(60),
string("hour") |> optional(string("s")) |> replace(3_600),
string("day") |> optional(string("s")) |> replace(86_400),
string("week") |> optional(string("s")) |> replace(604_800),
string("month") |> optional(string("s")) |> replace(2_592_000),
string("year") |> optional(string("s")) |> replace(31_536_000)
])
|> ignore(string(" ago"))
|> eos()
|> post_traverse(:relative_datetime)
2019-11-02 19:34:25 +01:00
|> unwrap_and_tag(:date)
date =
choice([
absolute_date,
relative_date
])
2019-11-15 19:27:10 +01:00
|> repeat(space)
2019-11-02 19:34:25 +01:00
|> eos()
2020-01-11 05:20:19 +01:00
|> label(
"a RFC3339 datetime fragment, like `2019-01-01', or relative date, like `3 days ago'"
)
2019-11-02 19:34:25 +01:00
2020-01-11 05:20:19 +01:00
defparsec(:parse, date)
end