interval() creates an Interval object with the specified start and end dates. If the start date occurs before the end date, the interval will be positive. Otherwise, it will be negative. %--% Creates an interval that covers the range spanned by two dates. It replaces the original behavior of lubridate, which created an interval by default whenever two date-times were subtracted. int_start() and int_start<-() are accessors the start date of an interval. Note that changing the start date of an interval will change the length of the interval, since the end date will remain the same. int_end() and int_end<-() are accessors the end date of an interval. Note that changing the end date of an interval will change the length of the interval, since the start date will remain the same. int_flip() reverses the order of the start date and end date in an interval. The new interval takes place during the same timespan as the original interval, but has the opposite direction. int_shift() shifts the start and end dates of an interval up or down the timeline by a specified amount. Note that this may change the exact length of the interval if the interval is shifted by a Period object. Intervals shifted by a Duration or difftime object will retain their exact length in seconds. int_overlaps() tests if two intervals overlap. int_standardize() ensures all intervals in an interval object are positive. If an interval is not positive, flip it so that it retains its endpoints but becomes positive. int_aligns() tests if two intervals share an endpoint. The direction of each interval is ignored. int_align tests whether the earliest or latest moments of each interval occur at the same time. int_diff() returns the intervals that occur between the elements of a vector of date-times. int_diff() is similar to the POSIXt and Date methods of diff(), but returns an Interval object instead of a difftime object.

interval(start, end, tzone = attr(start, "tzone"))

start %--% end

is.interval(x)

int_start(int)

int_start(int) <- value

int_end(int)

int_end(int) <- value

int_length(int)

int_flip(int)

int_shift(int, by)

int_overlaps(int1, int2)

int_standardize(int)

int_aligns(int1, int2)

int_diff(times)

Arguments

start

a POSIXt or Date date-time object

end

a POSIXt or Date date-time object

tzone

a recognized timezone to display the interval in

x

an R object

int

an interval object

value

interval's start/end to be assigned to int

by

A period or duration object to shift by (for int_shift)

int1

an Interval object (for int_overlaps(), int_aligns())

int2

an Interval object (for int_overlaps(), int_aligns())

times

A vector of POSIXct, POSIXlt or Date class date-times (for int_diff())

Value

interval() -- Interval object. int_start() and int_end() return a POSIXct date object when used as an accessor. Nothing when used as a setter. int_length() -- numeric length of the interval in seconds. A negative number connotes a negative interval. int_flip() -- flipped interval object int_shift() -- an Interval object int_overlaps() -- logical, TRUE if int1 and int2 overlap by at least one second. FALSE otherwise int_aligns() -- logical, TRUE if int1 and int2 begin or end on the same moment. FALSE otherwise int_diff() -- interval object that contains the n-1 intervals between the n date-time in times

Details

Intervals are time spans bound by two real date-times. Intervals can be accurately converted to either period or duration objects using as.period(), as.duration(). Since an interval is anchored to a fixed history of time, both the exact number of seconds that passed and the number of variable length time units that occurred during the interval can be calculated.

See also

Interval, as.interval(), %within%

Examples

interval(ymd(20090201), ymd(20090101))
#> [1] 2009-02-01 UTC--2009-01-01 UTC
date1 <- as.POSIXct("2009-03-08 01:59:59") date2 <- as.POSIXct("2000-02-29 12:00:00") interval(date2, date1)
#> [1] 2000-02-29 12:00:00 PST--2009-03-08 01:59:59 PST
interval(date1, date2)
#> [1] 2009-03-08 01:59:59 PST--2000-02-29 12:00:00 PST
span <- interval(ymd(20090101), ymd(20090201)) is.interval(period(months= 1, days = 15)) # FALSE
#> [1] FALSE
is.interval(interval(ymd(20090801), ymd(20090809))) # TRUE
#> [1] TRUE
int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int_start(int)
#> [1] "2001-01-01 UTC"
int_start(int) <- ymd("2001-06-01") int
#> [1] 2001-06-01 UTC--2002-01-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int_end(int)
#> [1] "2002-01-01 UTC"
int_end(int) <- ymd("2002-06-01") int
#> [1] 2001-01-01 UTC--2002-06-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int_length(int)
#> [1] 31536000
int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int_flip(int)
#> [1] 2002-01-01 UTC--2001-01-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int_shift(int, duration(days = 11))
#> [1] 2001-01-12 UTC--2002-01-12 UTC
int_shift(int, duration(hours = -1))
#> [1] 2000-12-31 23:00:00 UTC--2001-12-31 23:00:00 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int2 <- interval(ymd("2001-06-01"), ymd("2002-06-01")) int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01")) int_overlaps(int1, int2) # TRUE
#> [1] TRUE
int_overlaps(int1, int3) # FALSE
#> [1] FALSE
int <- interval(ymd("2002-01-01"), ymd("2001-01-01")) int_standardize(int)
#> [1] 2001-01-01 UTC--2002-01-01 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01")) int2 <- interval(ymd("2001-06-01"), ymd("2002-01-01")) int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01")) int_aligns(int1, int2) # TRUE
#> [1] TRUE
int_aligns(int1, int3) # FALSE
#> [1] FALSE
dates <- now() + days(1:10) int_diff(dates)
#> [1] 2017-05-27 10:13:04 PDT--2017-05-28 10:13:04 PDT #> [2] 2017-05-28 10:13:04 PDT--2017-05-29 10:13:04 PDT #> [3] 2017-05-29 10:13:04 PDT--2017-05-30 10:13:04 PDT #> [4] 2017-05-30 10:13:04 PDT--2017-05-31 10:13:04 PDT #> [5] 2017-05-31 10:13:04 PDT--2017-06-01 10:13:04 PDT #> [6] 2017-06-01 10:13:04 PDT--2017-06-02 10:13:04 PDT #> [7] 2017-06-02 10:13:04 PDT--2017-06-03 10:13:04 PDT #> [8] 2017-06-03 10:13:04 PDT--2017-06-04 10:13:04 PDT #> [9] 2017-06-04 10:13:04 PDT--2017-06-05 10:13:04 PDT