Closed mtricolici closed 6 years ago
another test
ruby 2.3.3p222
time_difference (0.5.0)
#!/usr/bin/env ruby
require 'date'
require 'time_difference'
now = DateTime.now.utc
puts "now is #{now}. class=#{now.class}"
backup_date = DateTime.strptime('2017-06-28-000001', '%Y-%m-%d-%H%M%S').utc
puts "backup_date is #{backup_date}. class=#{backup_date.class}"
h = TimeDifference.between(backup_date, now).in_hours
puts "...time diff in hours=#{h}"
now is 2017-06-28 08:26:35 UTC. class=Time
backup_date is 2017-06-28 00:00:01 UTC. class=Time
...time diff in hours=109421732.92
works fine in ruby 2.2. and 2.4. (issue in 2.3.1 & 2.3.4)
the same here, but with ruby 2.4.1
here works ruby 2.3.0
/ # ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux-musl]
/ # gem list time_difference
*** LOCAL GEMS ***
time_difference (0.5.0)
irb(main):024:0* now = DateTime.now.utc
=> 2017-06-29 13:56:31 UTC
irb(main):025:0> last = Time.at( 1506470400000 / 1000 )
=> 2017-09-27 00:00:00 +0000
irb(main):026:0> puts "now is #{now}. class=#{now.class}"
now is 2017-06-29 13:56:31 UTC. class=Time
=> nil
irb(main):027:0> puts "last is #{last}. class=#{last.class}"
last is 2017-09-27 00:00:00 +0000. class=Time
=> nil
irb(main):028:0> h = TimeDifference.between(now,last).in_each_component
=> {:years=>243797379089759.7, :months=>21942621712.53, :weeks=>4672461867467.01, :days=>667494552495.29, :hours=>27812273020.64, :minutes=>463537883.68, :seconds=>7725631.394621372}
I got this issue on all of these ruby versions 2.3.1
, 2.3.3
, 2.3.4
and 2.4.1
.
I just encountered this problem as well, but in my case it doesn’t appear to have anything to do with the runtime version. Rather, I encountered the problem while upgrading dependencies. I managed to narrow it down to ActiveSupport. For whatever reason, when I use ActiveSupport 5.1.1 it works fine, but when I use 5.1.2 it breaks. I checked the release notes for 5.1.2 and they only describe two changes. One of them, however, is “Fix implicit coercion calculations with scalars and durations” — so that might be the culprit.
I have not dug further than that, because personally I’d rather just stick with 5.1.1 for now rather than take more time to debug this.
HTH!
Oh and also I noticed that #31 might address this. Not entirely sure.
@aviflax your 100% right the issue happened when I updated rails to 5.1.2 as 5.1.1 had no problems.
ActiveSupport 5.1.1
(((Time.now + 2.hours) - Time.now) / 1.hours).round
= 2
ActiveSupport 5.1.2
(((Time.now + 2.hours) - Time.now) / 1.hours).round
= 25920000
Can confirm. Rails 5.1.2 changed the behaviour of x / 1.hour
. It now returns x.hours
rather than (x/3600).hours
.
2.4.0 :033 > TimeDifference.between(DateTime.now, DateTime.now - 1.week).in_seconds
=> 604799.9996852875
2.4.0 :034 > TimeDifference.between(DateTime.now, DateTime.now - 1.week).in_hours
=> 2177279998.97
You can find the change here https://github.com/rails/rails/blob/v5.1.2/activesupport/CHANGELOG.md
Fix implicit coercion calculations with scalars and durations
Previously calculations where the scalar is first would be converted to a duration of seconds but this causes issues with dates being converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the ActiveSupport::Duration::Scalar
calculation methods will try to maintain
the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
Andrew White
Thank you! This helped me troubleshoot the issue much quicker than I could have on my own.
BTW, activesupport v5.1.3 is out now and fixes this issue. bundle update rails
should do the trick.
Thanks guys. Verifying that the library continues to work as expected with the latest version of ActiveSupport. Will re-open if anyone is still facing issues for this one