View source code
Display the source code in std/datetime/interval.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

std.datetime.interval.Interval.shift - multiple declarations

Function Interval.shift

Shifts 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 += duration and end += duration.

void shift(D) (
  D duration
) pure
if (__traits(compiles, begin + duration));

Parameters

NameDescription
duration The duration to shift the interval by.

Throws

DateTimeException this interval is empty or if the resulting interval would be invalid.

Example

auto interval1 = Interval!Date(Date(1996, 1, 2), Date(2012, 4, 5));
auto interval2 = Interval!Date(Date(1996, 1, 2), Date(2012, 4, 5));

interval1.shift(dur!"days"(50));
assert(interval1 == Interval!Date(Date(1996, 2, 21), Date(2012, 5, 25)));

interval2.shift(dur!"days"(-50));
assert(interval2 == Interval!Date(Date(1995, 11, 13), Date(2012, 2, 15)));

Function Interval.shift

Shifts the interval forward or backwards in time by the given number of years and/or months (a positive number of years and months shifts the interval forward; a negative number shifts it backward). It adds the years the given years and months to both begin and end. It effectively calls add!"years"() and then add!"months"() on begin and end with the given number of years and months.

void shift(T) (
  T years,
  T months = 0,
  AllowDayOverflow allowOverflow = AllowDayOverflow.yes
)
if (isIntegral!T);

Parameters

NameDescription
years The number of years to shift the interval by.
months The number of months to shift the interval by.
allowOverflow Whether the days should be allowed to overflow on begin and end, causing their month to increment.

Throws

DateTimeException if this interval is empty or if the resulting interval would be invalid.

Example

auto interval1 = Interval!Date(Date(1996, 1, 2), Date(2012, 3, 1));
auto interval2 = Interval!Date(Date(1996, 1, 2), Date(2012, 3, 1));

interval1.shift(2);
assert(interval1 == Interval!Date(Date(1998, 1, 2), Date(2014, 3, 1)));

interval2.shift(-2);
assert(interval2 == Interval!Date(Date(1994, 1, 2), Date(2010, 3, 1)));

Authors

Jonathan M Davis

License

Boost License 1.0.