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.36k stars 223 forks source link

DateTime parse without date not working properly #413

Closed ilyakuzmin closed 3 months ago

ilyakuzmin commented 10 months ago
> DateTime.current
Mon, 16 May 2022 16:00:00 +0300

> DateTime.now_without_mock_time
Thu, 02 Nov 2023 03:04:10 +0300

> DateTime.parse('15:00:00')
Tue, 17 May 2022 15:00:00 +0000

I assume it's about round for TimeStackItem#travel_offset_days

Or need one more case for parse_with_mock_date

> DateTime._parse('15:00:00')
{:hour=>15, :min=>0, :sec=>0}
joshuacronemeyer commented 10 months ago

Seems like you have a good handle on this. Can you create a PR?

joshuacronemeyer commented 10 months ago

Was thinking about this and wanted to see more test cases. Seems like the correct behavior is to keep the Date part and only change the time

3.0.1 :002 > Time.now
 => 2023-11-02 07:30:29.518102 -0700
3.0.1 :003 > DateTime.parse('06:00:00')
 => Thu, 02 Nov 2023 06:00:00 +0000
3.0.1 :004 > DateTime.parse('01:00:00')
 => Thu, 02 Nov 2023 01:00:00 +0000
3.0.1 :005 > DateTime.parse('15:00:00')
 => Thu, 02 Nov 2023 15:00:00 +0000
ilyakuzmin commented 10 months ago

It depends on frozen time you use. Not sure what we can do with all this case in parse_with_mock_date, too much variations

case 1 correct

> Time.now
2022-08-02 09:50:00 +0300

> DateTime.parse("15:00:00")
Tue, 02 Aug 2022 15:00:00 +0000

> Timecop.top_stack_item
#<Timecop::TimeStackItem:... @travel_offset=-39521870.819318704

> 39521870.0 / 60 / 60 / 24
457.429050925926

> Timecop.top_stack_item.travel_offset_days
-457

case 2 incorrect

> Time.now
2022-08-02 05:50:00 +0300

> DateTime.parse("15:00:00")
Mon, 01 Aug 2022 15:00:00 +0000

> Timecop.top_stack_item
#<Timecop::TimeStackItem:... @travel_offset=-39536896.712171

> 39536896.0 / 60 / 60 / 24
457.602962962963

> Timecop.top_stack_item.travel_offset_days
-458
joshuacronemeyer commented 8 months ago

@ilyakuzmin does this fix it?

https://github.com/travisjeffery/timecop/pull/415

joshuacronemeyer commented 3 months ago

I am going to merge that fix. Please reopen @ilyakuzmin if this issue is not fixed on main