travisjeffery / timecop

A gem providing "time travel", "time freezing", and "time acceleration" capabilities, making it simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
MIT License
3.37k stars 226 forks source link

Add support for 'in:' kwarg in Time.new and Time.now #432

Open mcfadden opened 1 month ago

mcfadden commented 1 month ago

This adds support for Ruby 3.1's in: keyword argument.

Example usage:

Timecop.freeze(Time.new(2024, 9, 20, 1, 2, 30)) do
  puts Time.new # 2024-09-20 01:02:30 -0400
  puts Time.new(in: "+04:00") # 2024-09-20 09:02:30 +0400
  puts Time.now(in: "+03:00") # 2024-09-20 08:02:30 +0300
  puts Time.new(in: "+02:00") # 2024-09-20 07:02:30 +0200
end

Fixes #431