Length of lubridate interval

Ken Williams picture Ken Williams · Jan 7, 2012 · Viewed 10.1k times · Source

What's the best way to get the length of time represented by an interval in lubridate, in specified units? All I can figure out is something like the following messy thing:

> ival
[1] 2011-01-01 03:00:46 -- 2011-10-21 18:33:44

> difftime(attr(ival, "start") + as.numeric(ival), attr(ival, "start"), 'days')
Time difference of 293.6479 days

(I also added this as a feature request at https://github.com/hadley/lubridate/issues/105, under the assumption that there's no better way available - but maybe someone here knows of one.)

Update - apparently the difftime function doesn't handle this either. Here's an example.

> (d1 <- as.POSIXct("2011-03-12 12:00:00", 'America/Chicago'))
[1] "2011-03-12 12:00:00 CST"
> (d2 <- d1 + days(1))  # Gives desired result
[1] "2011-03-13 12:00:00 CDT"
> (i2 <- d2 - d1)
[1] 2011-03-12 12:00:00 -- 2011-03-13 12:00:00 
> difftime(attr(i2, "start") + as.numeric(i2), attr(i2, "start"), 'days')
Time difference of 23 hours

As I mention below, I think one nice way to handle this would be to implement a /.interval function that doesn't first cast its input to a period.

Answer

IRTFM picture IRTFM · Jan 7, 2012

The as.duration function is what lubridate provides. The interval class is represented internally as the number of seconds from the start, so if you wanted the number of hours you could simply divide as.numeric(ival) by 3600, or by (3600*24) for days.

If you want worked examples of functions applied to your object, you should provide the output of dput(ival). I did my testing on the objects created on the help(duration) page which is where ?interval sent me.

 date <- as.POSIXct("2009-03-08 01:59:59") # DST boundary
 date2 <- as.POSIXct("2000-02-29 12:00:00")
 span <- date2 - date  #creates interval 
 span
#[1] 2000-02-29 12:00:00 -- 2009-03-08 01:59:59 
 str(span)
#Classes 'interval', 'numeric'  atomic [1:1] 2.85e+08
#  ..- attr(*, "start")= POSIXct[1:1], format: "2000-02-29 12:00:00"
 as.duration(span)
#[1] 284651999s (9.02y) 
 as.numeric(span)/(3600*24)
#[1] 3294.583
# A check against the messy method:
difftime(attr(span, "start") + as.numeric(span), attr(span, "start"), 'days')
# Time difference of 3294.583 days