-
Notifications
You must be signed in to change notification settings - Fork 3.4k
WIP: Implement big year parsing #13720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc9f6df
Implement big year parsing
voughtdq a9f2fef
Fix up naming, use `Enum.reduce/3` instead of `List.foldl/3`, format
voughtdq f27be1f
Add tests
voughtdq d6f79ae
Allow big years in Inspect protocol
voughtdq 44b6d0b
Reorder case clauses, fix up naming
voughtdq f484be6
Format
voughtdq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,22 @@ defmodule Calendar.ISO do | |
] | ||
end | ||
|
||
[match_big_year, guard_big_year, match_big_year_rest, guard_big_year_rest, read_big_year_rest] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about this: every time we see a + or - sign, we use Integer.parse to extract the year and then we parse the rest (month/day) using the patterns below? Would that simplify things a bit? But the general direction is good to me. |
||
quote do | ||
[ | ||
<<y1, y2, y3, y4, y5>>, | ||
y1 >= ?0 and y1 <= ?9 and (y2 >= ?0 and y2 <= ?9) and | ||
(y3 >= ?0 and y3 <= ?9) and (y4 >= ?0 and y4 <= ?9) and (y5 >= ?0 and y5 <= ?9), | ||
<<@ext_date_sep, m1, m2, @ext_date_sep, d1, d2>>, | ||
m1 >= ?0 and m1 <= ?9 and (m2 >= ?0 and m1 <= ?9) and (d1 >= ?0 and d1 <= ?9) and | ||
(d2 >= ?0 and d2 <= ?9), | ||
{ | ||
(m1 - ?0) * 10 + (m2 - ?0), | ||
(d1 - ?0) * 10 + (d2 - ?0) | ||
} | ||
] | ||
end | ||
|
||
[match_basic_time, match_ext_time, guard_time, read_time] = | ||
quote do | ||
[ | ||
|
@@ -421,6 +437,23 @@ defmodule Calendar.ISO do | |
parse_formatted_date(year, month, day, multiplier) | ||
end | ||
|
||
defp do_parse_date(unquote(match_big_year) <> rest, multiplier, :extended) | ||
when unquote(guard_big_year) do | ||
<<y1, y2, y3, y4, y5>> = unquote(match_big_year) | ||
{year, rest} = parse_big_year(rest, [y5, y4, y3, y2, y1]) | ||
|
||
case parse_big_year_rest(rest) do | ||
:error -> | ||
{:error, :invalid_format} | ||
|
||
{{month, day}, ""} -> | ||
parse_formatted_date(year, month, day, multiplier) | ||
|
||
{{_month, _day}, _rest} -> | ||
{:error, :invalid_format} | ||
end | ||
end | ||
|
||
defp do_parse_date(_, _, _) do | ||
{:error, :invalid_format} | ||
end | ||
|
@@ -435,6 +468,38 @@ defmodule Calendar.ISO do | |
end | ||
end | ||
|
||
defp parse_big_year(<<y, rest::binary>>, digits) when y >= ?0 and y <= ?9 do | ||
parse_big_year(rest, [y | digits]) | ||
end | ||
|
||
defp parse_big_year("", _digits) do | ||
{:error, :invalid_format} | ||
end | ||
|
||
defp parse_big_year(rest, digits) do | ||
{year, _} = | ||
Enum.reduce(digits, {0, 1}, fn digit, {year, n} -> | ||
{(digit - ?0) * n + year, 10 * n} | ||
end) | ||
|
||
{year, rest} | ||
end | ||
|
||
defp parse_big_year_rest(unquote(match_big_year_rest)) when unquote(guard_big_year_rest) do | ||
{month, day} = unquote(read_big_year_rest) | ||
{{month, day}, ""} | ||
end | ||
|
||
defp parse_big_year_rest(unquote(match_big_year_rest) <> rest) | ||
when unquote(guard_big_year_rest) do | ||
{month, day} = unquote(read_big_year_rest) | ||
{{month, day}, rest} | ||
end | ||
|
||
defp parse_big_year_rest(_rest) do | ||
:error | ||
end | ||
|
||
@doc """ | ||
Parses a naive datetime `string` in the `:extended` format. | ||
|
||
|
@@ -518,6 +583,25 @@ defmodule Calendar.ISO do | |
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) | ||
end | ||
|
||
defp do_parse_naive_datetime(unquote(match_big_year) <> rest, multiplier, :extended) | ||
when unquote(guard_big_year) do | ||
<<y1, y2, y3, y4, y5>> = unquote(match_big_year) | ||
{year, rest} = parse_big_year(rest, [y5, y4, y3, y2, y1]) | ||
|
||
case parse_big_year_rest(rest) do | ||
:error -> | ||
{:error, :invalid_format} | ||
|
||
{{month, day}, <<datetime_sep, unquote(match_ext_time), rest::binary>>} | ||
when datetime_sep in @datetime_seps and unquote(guard_time) -> | ||
{hour, minute, second} = unquote(read_time) | ||
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) | ||
|
||
{{_month, _day}, _rest} -> | ||
{:error, :invalid_format} | ||
end | ||
end | ||
|
||
defp do_parse_naive_datetime(_, _, _) do | ||
{:error, :invalid_format} | ||
end | ||
|
@@ -622,6 +706,25 @@ defmodule Calendar.ISO do | |
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) | ||
end | ||
|
||
defp do_parse_utc_datetime(unquote(match_big_year) <> rest, multiplier, :extended) | ||
when unquote(guard_big_year) do | ||
<<y1, y2, y3, y4, y5>> = unquote(match_big_year) | ||
{year, rest} = parse_big_year(rest, [y5, y4, y3, y2, y1]) | ||
|
||
case parse_big_year_rest(rest) do | ||
:error -> | ||
{:error, :invalid_format} | ||
|
||
{{month, day}, <<datetime_sep, unquote(match_ext_time), rest::binary>>} | ||
when datetime_sep in @datetime_seps and unquote(guard_time) -> | ||
{hour, minute, second} = unquote(read_time) | ||
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) | ||
|
||
{{_month, _day}, _rest} -> | ||
{:error, :invalid_format} | ||
end | ||
end | ||
|
||
defp do_parse_utc_datetime(_, _, _) do | ||
{:error, :invalid_format} | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove this clause and the guards. Just always invoke
calendar.date_to_string
. :)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that, but wanted to avoid a situation where a particular calendar implementation is inadvertently relying on year being in
-9999..9999
. I know there is a Jalaali calendar implementation I can test, not sure if there are others,