std.datetime.interval.everyDuration
- multiple declarations
Function everyDuration
Range-generating function.
TP delegate(scope const TP) everyDuration(TP, Direction dir = Direction .fwd, D)
(
D duration
) nothrow
if (isTimePoint!TP && __traits(compiles, TP .init + duration) && (dir == Direction .fwd || dir == Direction .bwd));
Returns a delegate which returns the next time point which is the given duration later.
Using this delegate allows iteration over successive time points which
are apart by the given duration e.g. passing dur!"days"(3)
to
everyDuration
would result in a delegate which could be used to iterate
over a range of days which are each 3 days apart.
Parameters
Name | Description |
---|---|
dir | The direction to iterate in. If passing the return value to
fwdRange , use Direction . If passing it to
bwdRange , use Direction . |
duration | The duration which separates each successive time point in the range. |
Example
import core .time : dur;
import std .datetime .date : Date;
auto interval = Interval!Date(Date(2010, 9, 2), Date(2010, 9, 27));
auto func = everyDuration!Date(dur!"days"(8));
auto range = interval .fwdRange(func);
// Using PopFirst.yes would have made this Date(2010, 9, 10).
writeln(range .front); // Date(2010, 9, 2)
range .popFront();
writeln(range .front); // Date(2010, 9, 10)
range .popFront();
writeln(range .front); // Date(2010, 9, 18)
range .popFront();
writeln(range .front); // Date(2010, 9, 26)
range .popFront();
assert(range .empty);
Function everyDuration
Range-generating function.
TP delegate(scope const TP) everyDuration(TP, Direction dir = Direction .fwd, D)
(
int years,
int months = 0,
AllowDayOverflow allowOverflow = AllowDayOverflow .yes,
D duration = dur!"days"(0)
) nothrow
if (isTimePoint!TP && __traits(compiles, TP .init + duration) && __traits(compiles, TP .init .add!"years"(years)) && __traits(compiles, TP .init .add!"months"(months)) && (dir == Direction .fwd || dir == Direction .bwd));
Returns a delegate which returns the next time point which is the given number of years, month, and duration later.
The difference between this version of everyDuration
and the version
which just takes a Duration
is that this one also takes
the number of years and months (along with an AllowDayOverflow
to
indicate whether adding years and months should allow the days to overflow).
Note that if iterating forward, add!"years"()
is called on the given
time point, then add!"months"()
, and finally the duration is added
to it. However, if iterating backwards, the duration is added first, then
add!"months"()
is called, and finally add!"years"()
is called.
That way, going backwards generates close to the same time points that
iterating forward does, but since adding years and months is not entirely
reversible (due to possible day overflow, regardless of whether
AllowDayOverflow
or AllowDayOverflow
is used), it can't be
guaranteed that iterating backwards will give the same time points as
iterating forward would have (even assuming that the end of the range is a
time point which would be returned by the delegate when iterating forward
from begin
).
Parameters
Name | Description |
---|---|
dir | The direction to iterate in. If passing the return
value to fwdRange , use Direction . If
passing it to bwdRange , use Direction . |
years | The number of years to add to the time point passed to the delegate. |
months | The number of months to add to the time point passed to the delegate. |
allowOverflow | Whether the days should be allowed to overflow on
begin and end , causing their month to
increment. |
duration | The duration to add to the time point passed to the delegate. |
Example
import core .time : dur;
import std .datetime .date : AllowDayOverflow, Date;
auto interval = Interval!Date(Date(2010, 9, 2), Date(2025, 9, 27));
auto func = everyDuration!Date(4, 1, AllowDayOverflow .yes, dur!"days"(2));
auto range = interval .fwdRange(func);
// Using PopFirst.yes would have made this Date(2014, 10, 12).
writeln(range .front); // Date(2010, 9, 2)
range .popFront();
writeln(range .front); // Date(2014, 10, 4)
range .popFront();
writeln(range .front); // Date(2018, 11, 6)
range .popFront();
writeln(range .front); // Date(2022, 12, 8)
range .popFront();
assert(range .empty);