Create a duration object.

Description

duration creates a duration object with the specified values. Entries for different units are cumulative. durations display as the number of seconds in a time span. When this number is large, durations also display an estimate in larger units,; however, the underlying object is always recorded as a fixed number of seconds. For display and creation purposes, units are converted to seconds using their most common lengths in seconds. Minutes = 60 seconds, hours = 3600 seconds, days = 86400 seconds, weeks = 604800. Units larger than weeks are not used due to their variability.

Usage

1
2
3
duration(num = NULL, units = "seconds", ...)

is.duration(x)

Arguments

num

the number of time units to include in the duration. From v1.6.0 num can also be a character vector that specifies durations in a convenient shorthand format. All unambiguous name units and abbreviations are supported. See examples.

units

a character string that specifies the type of units that num refers to. When num is character, this argument is ignored.

...

a list of time units to be included in the duration and their amounts. Seconds, minutes, hours, days, and weeks are supported.

x

an R object

Details

Durations record the exact number of seconds in a time span. They measure the exact passage of time but do not always align with measurements made in larger units of time such as hours, months and years. This is because the length of larger time units can be affected by conventions such as leap years and Daylight Savings Time. Base R provides a second class for measuring durations, the difftime class.

Duration objects can be easily created with the helper functions dweeks, ddays, dminutes, dseconds. These objects can be added to and subtracted to date- times to create a user interface similar to object oriented programming.

Value

a duration object

See Also

as.duration

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
duration(day = -1)
# -86400s (~-1 days)
duration(90, "seconds")
duration(1.5, "minutes")
duration(-1, "days")
# -86400s (~-1 days)
duration(second = 90)
duration(minute = 1.5)
duration(mins = 1.5)
duration(second = 3, minute = 1.5, hour = 2, day = 6, week = 1)
duration(hour = 1, minute = -60)
duration("2M 1sec")
duration("2hours 2minutes 1second")
duration("2d 2H 2M 2S")
duration("2days 2hours 2mins 2secs")
# Missing numerals default to 1. Repeated units are added up.
duration("day day")
# Comparison with characters is supported from v1.6.0.
duration("day 2 sec") > "day 1sec"
is.duration(as.Date("2009-08-03")) # FALSE
is.duration(duration(days = 12.4)) # TRUE

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.