-
Notifications
You must be signed in to change notification settings - Fork 3.4k
support Duration
in Date.range/3
#14172
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
Changes from 7 commits
563fd96
c0b0dd4
c480baf
a31cead
99c0fab
8e0c543
ab237e9
228f1df
104d7b9
c9279d1
66de751
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -69,6 +69,9 @@ defmodule Date do | |||||||
calendar: Calendar.calendar() | ||||||||
} | ||||||||
|
||||||||
@type duration_unit_pair :: | ||||||||
{:year, integer} | {:month, integer} | {:week, integer} | {:day, integer} | ||||||||
|
||||||||
@doc """ | ||||||||
Returns a range of dates. | ||||||||
|
||||||||
|
@@ -84,6 +87,13 @@ defmodule Date do | |||||||
iex> Date.range(~D[1999-01-01], ~D[2000-01-01]) | ||||||||
Date.range(~D[1999-01-01], ~D[2000-01-01]) | ||||||||
|
||||||||
A range may also be built from a `Date` and a `Duration`: | ||||||||
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.
Suggested change
? |
||||||||
|
||||||||
iex> Date.range(~D[1999-01-01], Duration.new!(year: 1)) | ||||||||
Date.range(~D[1999-01-01], ~D[2000-01-01]) | ||||||||
iex> Date.range(~D[1999-01-01], year: 1) | ||||||||
Date.range(~D[1999-01-01], ~D[2000-01-01]) | ||||||||
|
||||||||
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
A range of dates implements the `Enumerable` protocol, which means | ||||||||
functions in the `Enum` module can be used to work with | ||||||||
ranges: | ||||||||
|
@@ -100,7 +110,8 @@ defmodule Date do | |||||||
|
||||||||
""" | ||||||||
@doc since: "1.5.0" | ||||||||
@spec range(Calendar.date(), Calendar.date()) :: Date.Range.t() | ||||||||
@spec range(Calendar.date(), Calendar.date() | Duration.t() | [duration_unit_pair]) :: | ||||||||
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. I think this would improve docs (now they would still show
Suggested change
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. while I like
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. 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.
|
||||||||
Date.Range.t() | ||||||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do | ||||||||
{first_days, _} = to_iso_days(first) | ||||||||
{last_days, _} = to_iso_days(last) | ||||||||
|
@@ -123,6 +134,20 @@ defmodule Date do | |||||||
raise ArgumentError, "both dates must have matching calendars" | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: calendar} = first, %Duration{} = duration) do | ||||||||
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. shall we allow the keyword list version as well? :) 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. i found that |
||||||||
last = shift(first, duration) | ||||||||
range(first, last) | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: calendar} = first, duration) when is_list(duration) do | ||||||||
last = shift(first, duration) | ||||||||
range(first, last) | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: _, year: _, month: _, day: _}, _duration) do | ||||||||
raise ArgumentError, "expected a date or duration as second argument" | ||||||||
end | ||||||||
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. @josevalim do we usually do this, or just let this fall through and raise a |
||||||||
|
||||||||
@doc """ | ||||||||
Returns a range of dates with a step. | ||||||||
|
||||||||
|
@@ -140,8 +165,11 @@ defmodule Date do | |||||||
|
||||||||
""" | ||||||||
@doc since: "1.12.0" | ||||||||
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) :: | ||||||||
Date.Range.t() | ||||||||
@spec range( | ||||||||
Calendar.date(), | ||||||||
Calendar.date() | Duration.t() | [duration_unit_pair], | ||||||||
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. Same naming issue here |
||||||||
step :: pos_integer | neg_integer | ||||||||
) :: Date.Range.t() | ||||||||
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step) | ||||||||
when is_integer(step) and step != 0 do | ||||||||
{first_days, _} = to_iso_days(first) | ||||||||
|
@@ -159,6 +187,24 @@ defmodule Date do | |||||||
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}" | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: calendar} = first, %Duration{} = duration, step) | ||||||||
when is_integer(step) and step != 0 do | ||||||||
last = shift(first, duration) | ||||||||
range(first, last, step) | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: calendar} = first, duration, step) | ||||||||
when is_list(duration) and is_integer(step) and step != 0 do | ||||||||
last = shift(first, duration) | ||||||||
range(first, last, step) | ||||||||
end | ||||||||
|
||||||||
def range(%{calendar: _, year: _, month: _, day: _} = first, duration, step) do | ||||||||
raise ArgumentError, | ||||||||
"expected a date or duration as second argument and the step must be a " <> | ||||||||
"non-zero integer, got: #{inspect(first)}, #{inspect(duration)}, #{step}" | ||||||||
end | ||||||||
|
||||||||
defp range(first, first_days, last, last_days, calendar, step) do | ||||||||
%Date.Range{ | ||||||||
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day}, | ||||||||
|
@@ -795,8 +841,7 @@ defmodule Date do | |||||||
|
||||||||
""" | ||||||||
@doc since: "1.17.0" | ||||||||
@spec shift(Calendar.date(), Duration.t() | [unit_pair]) :: t | ||||||||
when unit_pair: {:year, integer} | {:month, integer} | {:week, integer} | {:day, integer} | ||||||||
@spec shift(Calendar.date(), Duration.t() | [duration_unit_pair]) :: t | ||||||||
def shift(%{calendar: calendar} = date, duration) do | ||||||||
%{year: year, month: month, day: day} = date | ||||||||
{year, month, day} = calendar.shift_date(year, month, day, __duration__!(duration)) | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.