Skip to content
A testing library which allows "time travel," "freezing time," and "time acceleration". Inspired by the ruby-timecop library.
Crystal
Branch: master
Clone or download
Pull request Compare This branch is 10 commits ahead of TobiasGSmollett:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
spec
src
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
shard.yml

README.md

timecop.cr

A timecop inspired library to allow easy manipulation of time in tests. Originally authored by TobiasGSmollett.

Status

As of 2019-06-14

Crystal Version Travis.com

Installation

Add this to your application's shard.yml:

dependencies:
+  timecop:
+    github: crystal-community/timecop.cr

Usage

require "timecop"

Timecop.freeze

date = Time.new(2008, 10, 10, 10, 10, 10)
Timecop.freeze(date) do |frozen_time|
  puts "#{frozen_time == Time.local}" # => true
end

Timecop.travel

Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0)) do
  puts Time.local # => 2014-01-01 00:00:00 +0900
  sleep(5)
  puts Time.local # => 2014-01-01 00:00:05 +0900
end

Timecop.scale

# seconds will now seem like hours
Timecop.scale(Time.local, 3600)
puts Time.local # => 2017-08-28 23:50:06 +0900
sleep(2.0)
# seconds later, hours have passed and it's gone from 23pm at night to 1am in the morning
puts Time.local # => 2017-08-29 01:50:21 +0900

Timecop.safe_mode

# turn on safe mode
Timecop.safe_mode = true

Timecop.safe_mode? # => true

# using method without block
Timecop.freeze Time.new(2008, 10, 10, 10, 10, 10)
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.

Development

Pull Request Welcome

Contributing

  1. Fork it ( https://github.com/TobiasGSmollett/timecop.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Thanks

Thanks to Travis Jeffery for his awesome work on timecop.

You can’t perform that action at this time.