std.datetime.interval

Types 8

Indicates a direction in time. One example of its use is Interval's

expand function which uses it to indicate whether the interval

should be expanded backwards (into the past), forwards (into the future), or both.

bwdBackward.
fwdForward.
bothBoth backward and forward.
aliasPopFirst = Flag!"popFirst"

Used to indicate whether popFront should be called immediately upon creating a range. The idea is that for some functions used to generate a range for an interval, front is not necessarily a time point which would ever be generated by the range (e.g. if the range were every Sunday within an interval, but the interval started on a Monday), so there needs to be a way to deal with that. To get the first time point in the range to match what the function generates, then use PopFirst.yes to indicate that the range should have popFront called on it before the range is returned so that front is a time point which the function would generate. To let the first time point not match the generator function, use PopFront.no.

For instance, if the function used to generate a range of time points generated successive Easters (i.e. you're iterating over all of the Easters within the interval), the initial date probably isn't an Easter. Using PopFirst.yes would tell the function which returned the range that popFront was to be called so that front would then be an Easter - the next one generated by the function (which when iterating forward would be the Easter following the original front, while when iterating backward, it would be the Easter prior to the original front). If PopFirst.no were used, then front would remain the original time point and it would not necessarily be a time point which would be generated by the range-generating function (which in many cases is exactly what is desired - e.g. if iterating over every day starting at the beginning of the interval).

If set to PopFirst.no, then popFront is not called before returning the range.

Otherwise, if set to PopFirst.yes, then popFront is called before returning the range.

structInterval(TP)

Represents an interval of time.

An Interval has a starting point and an end point. The interval of time is therefore the time starting at the starting point up to, but not including, the end point. e.g.

A range can be obtained from an Interval, allowing iteration over that interval, with the exact time points which are iterated over depending on the function which generates the range.

Fields
TP _begin
TP _end
Methods
Interval opAssign(const ref Interval rhs) ref pure nothrowParams: rhs = The Interval to assign to this one.
Interval opAssign(Interval rhs) ref pure nothrowParams: rhs = The Interval to assign to this one.
TP begin() @property const pure nothrowThe starting point of the interval. It is included in the interval.
void begin(TP timePoint) @property pureThe starting point of the interval. It is included in the interval.
TP end() @property const pure nothrowThe end point of the interval. It is excluded from the interval.
void end(TP timePoint) @property pureThe end point of the interval. It is excluded from the interval.
@property auto length() const pure nothrowReturns the duration between `begin` and `end`.
bool empty() @property const pure nothrowWhether the interval's length is 0, that is, whether begin == end.
bool contains(scope const TP timePoint) const pureWhether the given time point is within this interval.
bool contains(scope const Interval interval) const pureWhether the given interval is completely within this interval.
bool contains(scope const PosInfInterval!TP interval) const pureWhether the given interval is completely within this interval.
bool contains(scope const NegInfInterval!TP interval) const pureWhether the given interval is completely within this interval.
bool isBefore(scope const TP timePoint) const pureWhether this interval is before the given time point.
bool isBefore(scope const Interval interval) const pureWhether this interval is before the given interval and does not intersect with it.
bool isBefore(scope const PosInfInterval!TP interval) const pureWhether this interval is before the given interval and does not intersect with it.
bool isBefore(scope const NegInfInterval!TP interval) const pureWhether this interval is before the given interval and does not intersect with it.
bool isAfter(scope const TP timePoint) const pureWhether this interval is after the given time point.
bool isAfter(scope const Interval interval) const pureWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const PosInfInterval!TP interval) const pureWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const NegInfInterval!TP interval) const pureWhether this interval is after the given interval and does not intersect it.
bool intersects(scope const Interval interval) const pureWhether the given interval overlaps this interval.
bool intersects(scope const PosInfInterval!TP interval) const pureWhether the given interval overlaps this interval.
bool intersects(scope const NegInfInterval!TP interval) const pureWhether the given interval overlaps this interval.
Interval intersection(scope const Interval interval) constReturns the intersection of two intervals
Interval intersection(scope const PosInfInterval!TP interval) constReturns the intersection of two intervals
Interval intersection(scope const NegInfInterval!TP interval) constReturns the intersection of two intervals
bool isAdjacent(scope const Interval interval) const pureWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const PosInfInterval!TP interval) const pureWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const NegInfInterval!TP interval) const pureWhether the given interval is adjacent to this interval.
Interval merge(scope const Interval interval) constReturns the union of two intervals
PosInfInterval!TP merge(scope const PosInfInterval!TP interval) constReturns the union of two intervals
NegInfInterval!TP merge(scope const NegInfInterval!TP interval) constReturns the union of two intervals
Interval span(scope const Interval interval) const pureReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
PosInfInterval!TP span(scope const PosInfInterval!TP interval) const pureReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
NegInfInterval!TP span(scope const NegInfInterval!TP interval) const pureReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
void shift(D)(D duration) if (__traits(compiles, begin + duration)) pureShifts the interval forward or backwards in time by the given duration (a positive duration shifts the interval forward; a negative duration shifts it backward). Effectively, it does begin += durat...
void expand(D)(D duration, Direction dir = Direction.both) if (__traits(compiles, begin + duration)) pureExpands the interval forwards and/or backwards in time. Effectively, it does begin -= duration and/or end += duration. Whether it expands forwards and/or backwards in time is determined by dir.
IntervalRange!(TP, Direction.fwd) fwdRange(TP delegate(scope const TP) func, PopFirst popFirst = PopFirst.no) constReturns a range which iterates forward over the interval, starting at `begin`, using func to generate each successive time point.
IntervalRange!(TP, Direction.bwd) bwdRange(TP delegate(scope const TP) func, PopFirst popFirst = PopFirst.no) constReturns a range which iterates backwards over the interval, starting at `end`, using func to generate each successive time point.
string toString() const @safe nothrowConverts this interval to a string. Params: w = A `char` accepting output range Returns: A `string` when not using an output range; `void` otherwise.
void toString(Writer)(ref Writer w) if (isOutputRange!(Writer, char)) constditto
void _enforceNotEmpty(size_t line = __LINE__) const pure
bool _valid(scope const TP begin, scope const TP end) pure nothrow @trusted
Constructors
this(scope const TP begin, scope const U end)Params: begin = The time point which begins the interval. end = The time point which ends (but is not included in) the interval.
this(scope const TP begin, scope const D duration)Params: begin = The time point which begins the interval. duration = The duration from the starting point to the end point.
structPosInfInterval(TP)

Represents an interval of time which has positive infinity as its end point.

Any ranges which iterate over a PosInfInterval are infinite. So, the main purpose of using PosInfInterval is to create an infinite range which starts at a fixed point in time and goes to positive infinity.

Fields
bool emptyWhether the interval's length is 0. Always returns false.
TP _begin
Methods
PosInfInterval opAssign(const ref PosInfInterval rhs) ref pure nothrowParams: rhs = The `PosInfInterval` to assign to this one.
PosInfInterval opAssign(PosInfInterval rhs) ref pure nothrowParams: rhs = The `PosInfInterval` to assign to this one.
TP begin() @property const pure nothrowThe starting point of the interval. It is included in the interval.
void begin(TP timePoint) @property pure nothrowThe starting point of the interval. It is included in the interval.
bool contains(TP timePoint) const pure nothrowWhether the given time point is within this interval.
bool contains(scope const Interval!TP interval) const pureWhether the given interval is completely within this interval.
bool contains(scope const PosInfInterval interval) const pure nothrowWhether the given interval is completely within this interval.
bool contains(scope const NegInfInterval!TP interval) const pure nothrowWhether the given interval is completely within this interval.
bool isBefore(scope const TP timePoint) const pure nothrowWhether this interval is before the given time point.
bool isBefore(scope const Interval!TP interval) const pureWhether this interval is before the given interval and does not intersect it.
bool isBefore(scope const PosInfInterval interval) const pure nothrowWhether this interval is before the given interval and does not intersect it.
bool isBefore(scope const NegInfInterval!TP interval) const pure nothrowWhether this interval is before the given interval and does not intersect it.
bool isAfter(scope const TP timePoint) const pure nothrowWhether this interval is after the given time point.
bool isAfter(scope const Interval!TP interval) const pureWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const PosInfInterval interval) const pure nothrowWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const NegInfInterval!TP interval) const pure nothrowWhether this interval is after the given interval and does not intersect it.
bool intersects(scope const Interval!TP interval) const pureWhether the given interval overlaps this interval.
bool intersects(scope const PosInfInterval interval) const pure nothrowWhether the given interval overlaps this interval.
bool intersects(scope const NegInfInterval!TP interval) const pure nothrowWhether the given interval overlaps this interval.
Interval!TP intersection(scope const Interval!TP interval) constReturns the intersection of two intervals
PosInfInterval intersection(scope const PosInfInterval interval) const pure nothrowReturns the intersection of two intervals
Interval!TP intersection(scope const NegInfInterval!TP interval) constReturns the intersection of two intervals
bool isAdjacent(scope const Interval!TP interval) const pureWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const PosInfInterval interval) const pure nothrowWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const NegInfInterval!TP interval) const pure nothrowWhether the given interval is adjacent to this interval.
PosInfInterval merge(scope const Interval!TP interval) constReturns the union of two intervals
PosInfInterval merge(scope const PosInfInterval interval) const pure nothrowReturns the union of two intervals
PosInfInterval span(scope const Interval!TP interval) const pureReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
PosInfInterval span(scope const PosInfInterval interval) const pure nothrowReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
void shift(D)(D duration) if (__traits(compiles, begin + duration)) pure nothrowShifts the `begin` of this interval forward or backwards in time by the given duration (a positive duration shifts the interval forward; a negative duration shifts it backward). Effectively, it doe...
void expand(D)(D duration) if (__traits(compiles, begin + duration)) pure nothrowExpands the interval backwards in time. Effectively, it does begin -= duration.
PosInfIntervalRange!(TP) fwdRange(TP delegate(scope const TP) func, PopFirst popFirst = PopFirst.no) constReturns a range which iterates forward over the interval, starting at `begin`, using func to generate each successive time point.
string toString()
string toString() const nothrowConverts this interval to a string.
string _toStringImpl() const nothrow
Constructors
this(scope const TP begin)Params: begin = The time point which begins the interval.
structNegInfInterval(TP)

Represents an interval of time which has negative infinity as its starting point.

Any ranges which iterate over a NegInfInterval are infinite. So, the main purpose of using NegInfInterval is to create an infinite range which starts at negative infinity and goes to a fixed end point. Iterate over it in reverse.

Fields
bool emptyWhether the interval's length is 0. Always returns false.
TP _end
Methods
NegInfInterval opAssign(const ref NegInfInterval rhs) ref pure nothrowParams: rhs = The `NegInfInterval` to assign to this one.
NegInfInterval opAssign(NegInfInterval rhs) ref pure nothrowParams: rhs = The `NegInfInterval` to assign to this one.
TP end() @property const pure nothrowThe end point of the interval. It is excluded from the interval.
void end(TP timePoint) @property pure nothrowThe end point of the interval. It is excluded from the interval.
bool contains(TP timePoint) const pure nothrowWhether the given time point is within this interval.
bool contains(scope const Interval!TP interval) const pureWhether the given interval is completely within this interval.
bool contains(scope const PosInfInterval!TP interval) const pure nothrowWhether the given interval is completely within this interval.
bool contains(scope const NegInfInterval interval) const pure nothrowWhether the given interval is completely within this interval.
bool isBefore(scope const TP timePoint) const pure nothrowWhether this interval is before the given time point.
bool isBefore(scope const Interval!TP interval) const pureWhether this interval is before the given interval and does not intersect it.
bool isBefore(scope const PosInfInterval!TP interval) const pure nothrowWhether this interval is before the given interval and does not intersect it.
bool isBefore(scope const NegInfInterval interval) const pure nothrowWhether this interval is before the given interval and does not intersect it.
bool isAfter(scope const TP timePoint) const pure nothrowWhether this interval is after the given time point.
bool isAfter(scope const Interval!TP interval) const pureWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const PosInfInterval!TP interval) const pure nothrowWhether this interval is after the given interval and does not intersect it.
bool isAfter(scope const NegInfInterval interval) const pure nothrowWhether this interval is after the given interval and does not intersect it.
bool intersects(scope const Interval!TP interval) const pureWhether the given interval overlaps this interval.
bool intersects(scope const PosInfInterval!TP interval) const pure nothrowWhether the given interval overlaps this interval.
bool intersects(scope const NegInfInterval!TP interval) const pure nothrowWhether the given interval overlaps this interval.
Interval!TP intersection(scope const Interval!TP interval) constReturns the intersection of two intervals
Interval!TP intersection(scope const PosInfInterval!TP interval) constReturns the intersection of two intervals
NegInfInterval intersection(scope const NegInfInterval interval) const nothrowReturns the intersection of two intervals
bool isAdjacent(scope const Interval!TP interval) const pureWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const PosInfInterval!TP interval) const pure nothrowWhether the given interval is adjacent to this interval.
bool isAdjacent(scope const NegInfInterval interval) const pure nothrowWhether the given interval is adjacent to this interval.
NegInfInterval merge(scope const Interval!TP interval) constReturns the union of two intervals
NegInfInterval merge(scope const NegInfInterval interval) const pure nothrowReturns the union of two intervals
NegInfInterval span(scope const Interval!TP interval) const pureReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
NegInfInterval span(scope const NegInfInterval interval) const pure nothrowReturns an interval that covers from the earliest time point of two intervals up to (but not including) the latest time point of two intervals.
void shift(D)(D duration) if (__traits(compiles, end + duration)) pure nothrowShifts the `end` of this interval forward or backwards in time by the given duration (a positive duration shifts the interval forward; a negative duration shifts it backward). Effectively, it does ...
void expand(D)(D duration) if (__traits(compiles, end + duration)) pure nothrowExpands the interval forwards in time. Effectively, it does end += duration.
NegInfIntervalRange!(TP) bwdRange(TP delegate(scope const TP) func, PopFirst popFirst = PopFirst.no) constReturns a range which iterates backwards over the interval, starting at `end`, using func to generate each successive time point.
string toString()
string toString() const nothrowConverts this interval to a string.
string _toStringImpl() const nothrow
Constructors
this(scope const TP end)Params: end = The time point which ends the interval.
structIntervalRange(TP, Direction dir) if (isTimePoint!TP && dir != Direction.both)

A range over an Interval.

IntervalRange is only ever constructed by Interval. However, when it is constructed, it is given a function, func, which is used to generate the time points which are iterated over. func takes a time point and returns a time point of the same type. For instance, to iterate over all of the days in the interval Interval!Date, pass a function to Interval's fwdRange where that function took a Date and returned a Date which was one day later. That function would then be used by IntervalRange's popFront to iterate over the Dates in the interval.

If dir == Direction.fwd, then a range iterates forward in time, whereas if dir == Direction.bwd, then it iterates backwards in time. So, if

dir == Direction.fwd then front == interval.begin, whereas if dir == Direction.bwd then front == interval.end. func must

generate a time point going in the proper direction of iteration, or a

DateTimeException will be thrown. So, to iterate

forward in time, the time point that func generates must be later in time than the one passed to it. If it's either identical or earlier in time, then a DateTimeException will be thrown. To iterate backwards, then the generated time point must be before the time point which was passed in.

If the generated time point is ever passed the edge of the range in the proper direction, then the edge of that range will be used instead. So, if iterating forward, and the generated time point is past the interval's end, then front becomes end. If iterating backwards, and the generated time point is before begin, then front becomes begin. In either case, the range would then be empty.

Also note that while normally the begin of an interval is included in it and its end is excluded from it, if dir == Direction.bwd, then begin is treated as excluded and end is treated as included. This allows for the same behavior in both directions. This works because none of

Interval's functions which care about whether begin or end

is included or excluded are ever called by IntervalRange. interval returns a normal interval, regardless of whether dir == Direction.fwd or if dir == Direction.bwd, so any Interval functions which are called on it which care about whether begin or end are included or excluded will treat begin as included and end as excluded.

Fields
Interval!TP _interval
TP delegate(scope const TP) _func
Methods
IntervalRange opAssign(ref IntervalRange rhs) ref pure nothrowParams: rhs = The `IntervalRange` to assign to this one.
IntervalRange opAssign(IntervalRange rhs) ref pure nothrowDitto
bool empty() @property const pure nothrowWhether this `IntervalRange` is empty.
TP front() @property const pureThe first time point in the range.
void popFront()Pops `front` from the range, using `func` to generate the next time point in the range. If the generated time point is beyond the edge of the range, then `front` is set to that edge, and the range ...
IntervalRange save() @property pure nothrowReturns a copy of `this`.
Interval!TP interval() @property const pure nothrowThe interval that this `IntervalRange` currently covers.
TP delegate(scope const TP) func() pure nothrow @propertyThe function used to generate the next time point in the range.
Direction direction() @property const pure nothrowThe `Direction` that this range iterates in.
void _enforceNotEmpty(size_t line = __LINE__) const pure
void _enforceCorrectDirection(scope const TP newTP, size_t line = __LINE__) const
Constructors
this(const Interval!TP interval, TP delegate(scope const TP) func)
structPosInfIntervalRange(TP) if (isTimePoint!TP)

A range over a PosInfInterval. It is an infinite range.

PosInfIntervalRange is only ever constructed by PosInfInterval. However, when it is constructed, it is given a function, func, which is used to generate the time points which are iterated over. func takes a time point and returns a time point of the same type. For instance, to iterate over all of the days in the interval PosInfInterval!Date, pass a function to PosInfInterval's fwdRange where that function took a

Date and returned a Date

which was one day later. That function would then be used by PosInfIntervalRange's popFront to iterate over the

Dates in the interval - though obviously, since the

range is infinite, use a function such as std.range.take with it rather than iterating over all of the dates.

As the interval goes to positive infinity, the range is always iterated over forwards, never backwards. func must generate a time point going in the proper direction of iteration, or a

DateTimeException will be thrown. So, the time

points that func generates must be later in time than the one passed to it. If it's either identical or earlier in time, then a

DateTimeException will be thrown.
Fields
bool emptyThis is an infinite range, so it is never empty.
PosInfInterval!TP _interval
TP delegate(scope const TP) _func
Methods
PosInfIntervalRange opAssign(ref PosInfIntervalRange rhs) ref pure nothrowParams: rhs = The `PosInfIntervalRange` to assign to this one.
TP front() @property const pure nothrowThe first time point in the range.
void popFront()Pops `front` from the range, using `func` to generate the next time point in the range.
PosInfIntervalRange save() @property pure nothrowReturns a copy of `this`.
PosInfInterval!TP interval() @property const pure nothrowThe interval that this range currently covers.
TP delegate(scope const TP) func() pure nothrow @propertyThe function used to generate the next time point in the range.
void _enforceCorrectDirection(scope const TP newTP, size_t line = __LINE__) const
Constructors
this(const PosInfInterval!TP interval, TP delegate(scope const TP) func)
structNegInfIntervalRange(TP) if (isTimePoint!TP)

A range over a NegInfInterval. It is an infinite range.

NegInfIntervalRange is only ever constructed by NegInfInterval. However, when it is constructed, it is given a function, func, which is used to generate the time points which are iterated over. func takes a time point and returns a time point of the same type. For instance, to iterate over all of the days in the interval NegInfInterval!Date, pass a function to NegInfInterval's bwdRange where that function took a Date and returned a Date which was one day earlier. That function would then be used by NegInfIntervalRange's popFront to iterate over the Dates in the interval - though obviously, since the range is infinite, use a function such as std.range.take with it rather than iterating over all of the dates.

As the interval goes to negative infinity, the range is always iterated over backwards, never forwards. func must generate a time point going in the proper direction of iteration, or a

DateTimeException will be thrown. So, the time

points that func generates must be earlier in time than the one passed to it. If it's either identical or later in time, then a

DateTimeException will be thrown.

Also note that while normally the end of an interval is excluded from it, NegInfIntervalRange treats it as if it were included. This allows for the same behavior as with PosInfIntervalRange. This works because none of NegInfInterval's functions which care about whether end is included or excluded are ever called by NegInfIntervalRange. interval returns a normal interval, so any NegInfInterval functions which are called on it which care about whether end is included or excluded will treat end as excluded.

Fields
bool emptyThis is an infinite range, so it is never empty.
NegInfInterval!TP _interval
TP delegate(scope const TP) _func
Methods
NegInfIntervalRange opAssign(ref NegInfIntervalRange rhs) ref pure nothrowParams: rhs = The `NegInfIntervalRange` to assign to this one.
TP front() @property const pure nothrowThe first time point in the range.
void popFront()Pops `front` from the range, using `func` to generate the next time point in the range.
NegInfIntervalRange save() @property pure nothrowReturns a copy of `this`.
NegInfInterval!TP interval() @property const pure nothrowThe interval that this range currently covers.
TP delegate(scope const TP) func() pure nothrow @propertyThe function used to generate the next time point in the range.
void _enforceCorrectDirection(scope const TP newTP, size_t line = __LINE__) const
Constructors
this(const NegInfInterval!TP interval, TP delegate(scope const TP) func)

Functions 4

fnTP delegate(scope const TP) everyDayOfWeek(TP, Direction dir = Direction.fwd)(DayOfWeek dayOfWeek) if (isTimePoint!TP && (dir == Direction.fwd || dir == Direction.bwd) && __traits(hasMember, TP, "dayOfWeek") && !__traits(isStaticFunction, TP.dayOfWeek) && is(typeof(TP.dayOfWeek) == DayOfWeek)) nothrowRange-generating function.
fnTP delegate(scope const TP) everyMonth(TP, Direction dir = Direction.fwd)(int month) if (isTimePoint!TP && (dir == Direction.fwd || dir == Direction.bwd) && __traits(hasMember, TP, "month") && !__traits(isStaticFunction, TP.month) && is(typeof(TP.month) == Month))Range-generating function.
fnTP delegate(return scope const TP) everyDuration(TP, Direction dir = Direction.fwd, D)(D duration) if (isTimePoint!TP && __traits(compiles, TP.init + duration) && (dir == Direction.fwd || dir == Direction.bwd)) nothrowRange-generating function.
fnTP 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)) 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)) nothrowRange-generating function.