duration-arithmetic: Arithmetic: duration

Description Usage Arguments Details Value Examples

Description

These are duration methods for the arithmetic generics.

When adding to a duration using one of these functions, a second duration is created based on the function name and n. The two durations are then added together, and the precision of the result is determined as the more precise precision of the two durations.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
## S3 method for class 'clock_duration'
add_years(x, n, ...)

## S3 method for class 'clock_duration'
add_quarters(x, n, ...)

## S3 method for class 'clock_duration'
add_months(x, n, ...)

## S3 method for class 'clock_duration'
add_weeks(x, n, ...)

## S3 method for class 'clock_duration'
add_days(x, n, ...)

## S3 method for class 'clock_duration'
add_hours(x, n, ...)

## S3 method for class 'clock_duration'
add_minutes(x, n, ...)

## S3 method for class 'clock_duration'
add_seconds(x, n, ...)

## S3 method for class 'clock_duration'
add_milliseconds(x, n, ...)

## S3 method for class 'clock_duration'
add_microseconds(x, n, ...)

## S3 method for class 'clock_duration'
add_nanoseconds(x, n, ...)

Arguments

x

[clock_duration]

A duration vector.

n

[integer / clock_duration]

An integer vector to be converted to a duration, or a duration corresponding to the arithmetic function being used. This corresponds to the number of duration units to add. n may be negative to subtract units of duration.

...

These dots are for future extensions and must be empty.

Details

You can add calendrical durations to other calendrical durations, and chronological durations to other chronological durations, but you can't add a chronological duration to a calendrical duration (such as adding days and months). For more information, see the documentation on the duration helper page.

x and n are recycled against each other.

Value

x after performing the arithmetic, possibly with a more precise precision.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
x <- duration_seconds(5)

# Addition in the same precision
add_seconds(x, 1:10)

# Addition with days, defined as 86400 seconds
add_days(x, 1)

# Similarly, if you start with days and add seconds, you get the common
# precision of the two back, which is seconds
y <- duration_days(1)
add_seconds(y, 5)

# But you can't add a chronological duration (days) and
# a calendrical duration (months)
try(add_months(y, 1))

# You can add years to a duration of months, which adds
# an additional 12 months / year
z <- duration_months(5)
add_years(z, 1)

clock documentation built on April 23, 2021, 1:06 a.m.