Time travel to test time dependent code - a port of Ruby’s timecop

BEWARE: VERY ALPHA

Package API:

  • time_fuzz
  • TimeStackItem
  • Time

Features supported:

  • freeze: freeze time to a specific point

Hope to support soon:

  • travel: travel back to a specific point in time, but allow time to continue moving forward from there
  • scale: scale time by a given scaling factor that will cause time to move at an accelerated pace

Installation

remotes::install_github("ropenscilabs/timefuzz")
library("timefuzz")

freeze

freeze with a code block

library(timefuzz)
library(clock)
library(testthat)

book_due() is a toy function that tells us if a book is due

Given the due date of 2020-04-01 the book is not due

expect_false(book_due()) # FALSE

Create a time_fuzz object

Call freez(), passing the date you want to freeze time to, and then a code block to run in the context of that frozen time. Here we’ll freeze time to today + 450 days

x$freeze(Sys.Date() + 450, {
  expect_true(book_due())
})

book_due() results in TRUE now, whereas it was FALSE above in real time

freeze without a code block

We’re in the freezed date. So any time based actions using the [clock][] package are now using your frozen time context.

call $return() to unfreeze

now we’re back in current time

Meta

rofooter