knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Time classes for R, w/ time mocking capability via timefuzz

library(pendulum)

clock

clock(2009)$time
clock(2009, 3, 13)$time
clock(2009, 3, 13, 1, 4, 53)$time
x <- clock(2009, 3, 13)
x$utc
x$date

now

now()$now()
now("UTC")$now()

sys_date

Mockable [Sys.Date()] drop in replacement. Uses now() internally so can be mocked as shown below.

Sys.Date()
sys_date()

sys_time

Mockable [Sys.time()] drop in replacement. Uses now() internally so can be mocked as shown below.

Sys.time()
sys_time()

use in a function

todays_date <- function() sys_time()
todays_date()

now let's mock time

library(timefuzz)
x <- time_fuzz$new()
## set to today + 5 days
x$freeze(Sys.Date() + 5)
todays_date()
# if you run it again, you get the same EXACT time
todays_date()

"unfreeze" and you're back to real time

x$unfreeze()
todays_date()
Sys.sleep(1)
todays_date()
Sys.sleep(1)
todays_date()


ropensci/clock documentation built on Sept. 11, 2022, 11:07 a.m.