difftime
Time Intervals

Description

The class difftime represents time intervals.

Usage

.difftime(xx, units) # generally for internal use
difftime(time1, time2, tz, units = c("auto", "secs", "mins", 
   "hours", "days", "weeks"))

## S3 method for class 'difftime': print(x, digits = getOption("digits"), ...) ## S3 method for class 'difftime': [(x, ..., drop = TRUE) ## S3 method for class 'difftime': c(..., recursive = FALSE) ## S3 method for class 'difftime': diff(x, lag = 1L, differences = 1L)

Arguments

xx an S object.
time1, time2 an S object that is inherited from or can be converted to POSIXt, such as POSIXct or POSIXlt objects or date-time strings.
tz a character string representing the time zone, such as "GMT".
units a character string from "auto", "secs", "mins", "hours", "days", and "weeks".
format a character string representing the time format for tim.
x a difftime object.
digits the maximum number of (significant) digits to print.
drop a logical flag passed to operator [.
lag a positive integer, the spacing of the entries to difference
differences a positive integer, the number of iterations of differencing
... additional arguments.

Details

A difftime object is internally represented as structure(i, units = "hours", class = "difftime"), where i is numeric representing the number of units.
Value
.difftime returns a difftime object with specified units.
difftime returns a difftime object representing the time difference between time1 and time2.
print.difftime returns the original object x, with the invisible flag set to prevent reprinting.
[.difftime returns a difftime object formed by extracting the relevant subset.
c.difftime returns a difftime object formed by the concatenation of all of its inputs (except recursive, which is ignored). If the units of all of the difftime inputs are the same, then it is the units of the output. If the units differ or if no inputs are difftime objects, then the output units are "secs".
diff.difftime returns a difftime object with the units of x and values diff(as.numeric(x), lag, differences)
Differences between TIBCO Enterprise Runtime for R and Open-source R
Differences exist between TIBCO Enterprise Runtime for R and open-source R in the results for calculating difftime near DST; the TIBCO Enterprise Runtime for R results appear more reasonable than those of open-source R.
Example
## The start of DST in USA 2013 is 2013-03-10 2:00AM > difftime("2013-03-10 03:00:00", "2013-03-10 02:00:00", tz = "America/New_York") # Time difference of 2 hours (open-source R) # Time difference of 0 secs (TERR) > difftime("2013-03-10 03:00:00", "2013-03-10 02:59:00", tz = "America/New_York") # Time difference of 2 hours (open-source R) # Time difference of -59 mins (TERR) The differences are caused by the differences in as.POSIXct, i.e., > as.POSIXct("2013-03-10 02:00:00", tz = "America/New_York") # [1] "2013-03-10 EST" (open-source R) # [1] "2013-03-10 03:00:00 EDT" (TERR) > as.POSIXct("2011-03-13 02:59:59", tz = "America/New_York") # [1] "2013-03-10 EST" (open-source R) # [1] "2013-03-10 03:59:59 EDT" (TERR)
See Also
Math.difftime, Ops.difftime, Summary.difftime, Date, POSIXt, as.Date, format.Date, units.
Examples
sunrise <- as.POSIXct(c("2019-03-18 07:14:17", "2019-03-19 07:12:12", "2019-03-20 07:10:08", "2019-03-21 07:08:03"))
sunset <- as.POSIXct(c("2019-03-18 19:19:59", "2019-03-19 19:21:28", "2019-03-20 19:22:58", "2019-03-21 19:24:27"))
sunset-sunrise # units chosen automatically: hours
difftime(sunset, sunrise, units="mins") # user control of units
(ds <- diff(sunset)) # units chosen automatically: days
units(ds) <- "hours" # change units to hours
ds
diff(difftime(sunset, sunrise, units="secs")) # change in day lengths, in seconds

(x <- as.difftime(c("0:20", "1:05", "0:35"), format="%H:%M")) x[c(3,1)] c(x[3], x[1])

Package base version 6.0.0-69
Package Index