@@ -84,6 +84,11 @@ defmodule Date do
84
84
iex> Date.range(~D[1999-01-01], ~D[2000-01-01])
85
85
Date.range(~D[1999-01-01], ~D[2000-01-01])
86
86
87
+ Alternatively, a range may be built from a first `Date` and a `Duration`:
88
+
89
+ iex> Date.range(~D[1999-01-01], Duration.new!(year: 1))
90
+ Date.range(~D[1999-01-01], ~D[2000-01-01])
91
+
87
92
A range of dates implements the `Enumerable` protocol, which means
88
93
functions in the `Enum` module can be used to work with
89
94
ranges:
@@ -100,7 +105,7 @@ defmodule Date do
100
105
101
106
"""
102
107
@ doc since: "1.5.0"
103
- @ spec range ( Calendar . date ( ) , Calendar . date ( ) ) :: Date.Range . t ( )
108
+ @ spec range ( Calendar . date ( ) , Calendar . date ( ) | Duration . t ( ) ) :: Date.Range . t ( )
104
109
def range ( % { calendar: calendar } = first , % { calendar: calendar } = last ) do
105
110
{ first_days , _ } = to_iso_days ( first )
106
111
{ last_days , _ } = to_iso_days ( last )
@@ -119,6 +124,11 @@ defmodule Date do
119
124
range ( first , first_days , last , last_days , calendar , step )
120
125
end
121
126
127
+ def range ( % { calendar: calendar } = first , % Duration { } = duration ) do
128
+ last = shift ( first , duration )
129
+ range ( first , last )
130
+ end
131
+
122
132
def range ( % { calendar: _ , year: _ , month: _ , day: _ } , % { calendar: _ , year: _ , month: _ , day: _ } ) do
123
133
raise ArgumentError , "both dates must have matching calendars"
124
134
end
@@ -140,7 +150,7 @@ defmodule Date do
140
150
141
151
"""
142
152
@ doc since: "1.12.0"
143
- @ spec range ( Calendar . date ( ) , Calendar . date ( ) , step :: pos_integer | neg_integer ) ::
153
+ @ spec range ( Calendar . date ( ) , Calendar . date ( ) | Duration . t ( ) , step :: pos_integer | neg_integer ) ::
144
154
Date.Range . t ( )
145
155
def range ( % { calendar: calendar } = first , % { calendar: calendar } = last , step )
146
156
when is_integer ( step ) and step != 0 do
@@ -149,6 +159,11 @@ defmodule Date do
149
159
range ( first , first_days , last , last_days , calendar , step )
150
160
end
151
161
162
+ def range ( % { calendar: calendar } = first , % Duration { } = duration , step ) do
163
+ last = shift ( first , duration )
164
+ range ( first , last , step )
165
+ end
166
+
152
167
def range (
153
168
% { calendar: _ , year: _ , month: _ , day: _ } = first ,
154
169
% { calendar: _ , year: _ , month: _ , day: _ } = last ,
0 commit comments