On this page:
gen:  date-arithmetic-provider
date-arithmetic-provider?
+  years
-years
+  months
-months
+  weeks
-weeks
+  days
-days
+  date-period
-date-period
6.3.90.900

11 Date Arithmetic

value

gen:date-arithmetic-provider : any/c

An interface that defines date arithmetic operations. It is implemented by all objects that satisfy either date-provider? or period?.

procedure

(date-arithmetic-provider? x)  boolean?

  x : any/c
Returns #t if x implements gen:date-arithmetic-provider; #f otherwise.

procedure

(+years d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain

procedure

(-years d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain
Adds or subtracts n years to/from d, returning a fresh date arithmetic provider the same type as d. Where possible, the result will differ from the input only in its year component. However, when changing only the year would result in an invalid date, the date is adjusted backward. Additionally, if the result would contain invalid time components, the provided (or default) offset-resolver is used to adjust the result.

Examples:
> (+years (date 1970) 5)

#<date 1975-01-01>

> (-years (date 1970) 1)

#<date 1969-01-01>

> (-years (datetime 1980 2 29) 1)

#<datetime 1979-02-28T00:00:00>

> (+years (moment 2014 3 8 2 #:tz "America/New_York") 1)

#<moment 2015-03-08T03:00:00-04:00[America/New_York]>

> (+years (moment 2014 3 8 2 #:tz "America/New_York") 1 #:resolve-offset resolve-offset/pre)

#<moment 2015-03-08T01:59:59.999999999-05:00[America/New_York]>

> (-years (days 2) 5)

#<period of -5 years, 2 days>

procedure

(+months d n [#:resolve-offset resolve])

  date-arithmetic-provider?
  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain

procedure

(-months d n [#:resolve-offset resolve])

  date-arithmetic-provider?
  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain
Adds or subtracts n months to/from d, returning a fresh date arithmetic provider the same type as d. Where possible, the result will differ from the input only in its month (and, possibly, year) components. However, when changing only these components would result in an invalid date, the date is adjusted backward. Additionally, if the result would contain invalid time components, the provided (or default) offset-resolver is used to adjust the result.

Examples:
> (+months (date 1970) 30)

#<date 1972-07-01>

> (+months (datetime 2015 12 29) 2)

#<datetime 2016-02-29T00:00:00>

> (-months (datetime 2015 3 29) 1)

#<datetime 2015-02-28T00:00:00>

> (+months (moment 2015 2 8 2) 1)

#<moment 2015-03-08T03:00:00-06:00[America/Denver]>

> (+months (moment 2015 2 8 2) 1 #:resolve-offset resolve-offset/raise)

Illegal moment: local time 2015-03-08T02:00:00 does not

exist in time zone America/Denver

> (-months (months 8) 3)

#<period of 5 months>

procedure

(+weeks d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain

procedure

(-weeks d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain
Adds or subtracts n weeks to/from d, returning a fresh date arithmetic provider the same type as d. If that would result in an invalid date, the date is adjusted backward. Additionally, if the result would contain invalid time components, the provided (or default) offset-resolver is used to adjust the result.

Examples:
> (+weeks (date 1970) 3)

#<date 1970-01-22>

> (-weeks (date 1970) 3)

#<date 1969-12-11>

> (+weeks (datetime 2016 2 22) 1)

#<datetime 2016-02-29T00:00:00>

> (-weeks (moment 2015 3 29 2) 3 #:resolve-offset resolve-offset/raise)

Illegal moment: local time 2015-03-08T02:00:00 does not

exist in time zone America/Denver

> (+weeks (minutes 7) 1)

#<period of 1 week, 7 minutes>

procedure

(+days d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain

procedure

(-days d n [#:resolve-offset resolve])  date-arithmetic-provider?

  d : date-arithmetic-provider?
  n : exact-integer?
  resolve : offset-resolver/c = resolve-offset/retain
Adds or subtracts n days to/from d, returning a fresh date arithmetic provider the same type as d. If that would result in an invalid date, the date is adjusted backward. Additionally, if the result would contain invalid time components, the provided (or default) offset-resolver is used to adjust the result.

Examples:
> (+days (date 1970) 3)

#<date 1970-01-04>

> (-days (date 1970) 3)

#<date 1969-12-29>

> (+days (datetime 2016 2 28) 1)

#<datetime 2016-02-29T00:00:00>

> (-days (moment 2015 3 11 2) 3 #:resolve-offset resolve-offset/raise)

Illegal moment: local time 2015-03-08T02:00:00 does not

exist in time zone America/Denver

> (-days (period [years 5] [hours 2]) 20)

#<period of 5 years, -20 days, 2 hours>

procedure

(+date-period d p [#:resolve-offset resolve])

  date-arithmetic-provider?
  d : date-arithmetic-provider?
  p : date-period?
  resolve : offset-resolver/c = resolve-offset/retain

procedure

(-date-period d p [#:resolve-offset resolve])

  date-arithmetic-provider?
  d : date-arithmetic-provider?
  p : date-period?
  resolve : offset-resolver/c = resolve-offset/retain
Adds or subtracts p to/from d, returning a fresh date arithmetic provider the same type as d. If that would result in an invalid date, the date is adjusted backward. Additionally, if the result would contain invalid time components, the provided (or default) offset-resolver is used to adjust the result.

Examples:
> (+date-period (date 1970) (years 10))

#<date 1980-01-01>

> (-date-period (date 1970) (weeks 1))

#<date 1969-12-25>

> (+date-period (minutes 4) (days 2))

#<period of 2 days, 4 minutes>