mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-30 14:57:59 +01:00
add sane maximum value to relative date parser, fixes #43
This commit is contained in:
parent
2b99afb746
commit
f6f29840ca
1 changed files with 16 additions and 6 deletions
|
@ -90,13 +90,23 @@ defmodule Search.DateParser do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp relative_datetime([count, scale]) do
|
defp relative_datetime(_rest, [count, scale], context, _line, _offset) do
|
||||||
now = DateTime.utc_now()
|
millenium_seconds = 31_536_000_000
|
||||||
|
|
||||||
lower = DateTime.add(now, (count + 1) * -scale, :second)
|
case count * scale <= millenium_seconds do
|
||||||
upper = DateTime.add(now, count * -scale, :second)
|
true ->
|
||||||
|
now = DateTime.utc_now()
|
||||||
|
|
||||||
[lower, upper]
|
lower = DateTime.add(now, (count + 1) * -scale, :second)
|
||||||
|
upper = DateTime.add(now, count * -scale, :second)
|
||||||
|
|
||||||
|
{[[lower, upper]], context}
|
||||||
|
|
||||||
|
_false ->
|
||||||
|
{:error, "invalid date format in input; requested time #{
|
||||||
|
count*scale
|
||||||
|
} seconds is over a millenium ago"}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
space =
|
space =
|
||||||
|
@ -175,7 +185,7 @@ defmodule Search.DateParser do
|
||||||
])
|
])
|
||||||
|> ignore(string(" ago"))
|
|> ignore(string(" ago"))
|
||||||
|> eos()
|
|> eos()
|
||||||
|> reduce(:relative_datetime)
|
|> post_traverse(:relative_datetime)
|
||||||
|> unwrap_and_tag(:date)
|
|> unwrap_and_tag(:date)
|
||||||
|
|
||||||
date =
|
date =
|
||||||
|
|
Loading…
Reference in a new issue