View source code Display the source code in std/datetime.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.datetime.every_duration - multiple declarations

Function everyDuration

Range-generating function.

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.

Prototype

TP delegate(in TP) everyDuration(TP, std.datetime.Direction dir, D)(
  D duration
) nothrow
if (isTimePoint!TP && __traits(compiles, TP.init + duration) && (dir == Direction.fwd || dir == Direction.bwd));

Parameters

NameDescription
dir The direction to iterate in. If passing the return value to fwdRange, use Direction.fwd. If passing it to bwdRange, use Direction.bwd.
duration The duration which separates each successive time point in the range.

Example

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).
assert(range.front == Date(2010, 9, 2));

range.popFront();
assert(range.front == Date(2010, 9, 10));

range.popFront();
assert(range.front == Date(2010, 9, 18));

range.popFront();
assert(range.front == Date(2010, 9, 26));

range.popFront();
assert(range.empty);

Function everyDuration

Range-generating function.

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 core.time.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.yes or AllowDayOverflow.no 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).

Prototype

TP delegate(in TP) everyDuration(TP, std.datetime.Direction dir, 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));

Parameters

NameDescription
dir The direction to iterate in. If passing the return value to fwdRange, use Direction.fwd. If passing it to bwdRange, use Direction.bwd.
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

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).
assert(range.front == Date(2010, 9, 2));

range.popFront();
assert(range.front == Date(2014, 10, 4));

range.popFront();
assert(range.front == Date(2018, 11, 6));

range.popFront();
assert(range.front == Date(2022, 12, 8));

range.popFront();
assert(range.empty);

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.

Comments