operate_arithmetic: Arithmetic operations for messydates

operate_arithmeticR Documentation

Arithmetic operations for messydates

Description

These operations allow users to add or subtract dates messydate objects. Messydate objects include incomplete or uncertain dates, ranges of dates, negative dates, and date sets.

Usage

## S3 method for class 'mdate'
e1 + e2

## S3 method for class 'mdate'
e1 - e2

Arguments

e1

An mdate or date object.

e2

An mdate, date, or numeric object. Must be a scalar.

Details

When e2 is a number or a string naming a unit ("day", "week", "month", "year", or, for date-times, "hour", "min"/"minute", or "sec"/"second"), e1 is shifted by that amount:

  • A bare number, or a "day"/"week" unit, shifts by that many days.

  • "month" and "year" shift the calendar component itself (preserving the day of month and any time of day), rolling back to the last valid day where necessary, e.g. adding a month to "2012-01-31" gives "2012-02-29" (2012 being a leap year), not an invalid "2012-02-31".

  • Sub-day units, or any unit at all when e1 carries a time of day, shift the instant in seconds via POSIXct, promoting a date-only e1 to a time if the shift is sub-day.

When e2 is itself an mdate, +/- instead treat both sides as sets of dates: + returns their union (as a multiset, in the most succinct mdate notation; see also ?operate_set for ⁠%union%⁠, which returns a plain vector of the member dates instead), and - removes the dates in e2 from e1.

Value

A messydates vector

Examples


d <- as_messydate(c("2008-03-25", "-2012-02-27", "2001-01?", "~2001",
"2001-01-01..2001-02-02", "{2001-01-01,2001-02-02}",
"2008-XX-31", "..2002-02-03", "2001-01-03..", "28 BC"))
data.frame(date = d, add = d + 1, subtract = d - 1)
data.frame(date = d, add = d + "1 year", subtract = d - "1 year")
as_messydate("2001-01-01") + as_messydate("2001-01-02..2001-01-04")
as_messydate("2001-01-01") + as_messydate("2001-01-03")
as_messydate("2001-01-01..2001-01-04") - as_messydate("2001-01-02")
#as_messydate("2001-01-01") - as_messydate("2001-01-03")
# calendar (month/year) arithmetic keeps the day of month and time of day
as_messydate("2012-01-31 09:00") + "1 month"
# sub-day units shift the instant
as_messydate("2012-01-01 14:30:00") + "2 hours"


messydates documentation built on July 17, 2026, 1:07 a.m.