timecop~TobiasGSmollett
timecop.cr
A timecop inspired library to allow easy manipulation of time in tests. Originally authored by TobiasGSmollett.
Installation
Add this to your application's shard.yml
:
dependencies:
+ timecop:
+ github: crystal-community/timecop.cr
Usage
require "timecop"
Timecop.freeze
time = Time.local(2008, 10, 10, 10, 10, 10)
Timecop.freeze(time) do |frozen_time|
frozen_time == Time.local # => true
end
Timecop.travel
Timecop.travel(Time.local(2014, 1, 1, 0, 0, 0)) do
Time.local # => "2014-01-01 00:00:00 +0900"
sleep(5.seconds)
Time.local # => "2014-01-01 00:00:05 +0900"
end
Timecop.scale
# seconds will now seem like hours
Timecop.scale(3600)
Time.local # => "2017-08-28 23:50:06 +0900"
sleep(2.seconds)
# 2 seconds later, hours have passed and it's gone from
# 23pm at night to 1am in the morning
Time.local # => "2017-08-29 01:50:21 +0900"
Timecop.safe_mode
Timecop.safe_mode? # => false
Timecop.safe_mode = true
# using method without block
Timecop.freeze Time.local(2008, 10, 10, 10, 10, 10)
# => raises Timecop::SafeModeException
Development
Pull Requests Welcome!
Contributing
- Fork it (https://github.com/TobiasGSmollett/timecop.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- TobiasGSmollett - creator
- Robacarp - maintainer
- Sija - maintainer
Thanks
Thanks to Travis Jeffery for his awesome work on timecop.