Learn R Programming

clock (version 0.7.1)

duration-arithmetic: Arithmetic: duration

Description

These are duration methods for the arithmetic generics.

  • add_years()

  • add_quarters()

  • add_months()

  • add_weeks()

  • add_days()

  • add_hours()

  • add_minutes()

  • add_seconds()

  • add_milliseconds()

  • add_microseconds()

  • add_nanoseconds()

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

# S3 method for clock_duration
add_years(x, n, ...)

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

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

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

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

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

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

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

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

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

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

Value

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

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 using tidyverse recycling rules.

Examples

Run this code
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)

Run the code above in your browser using DataLab