tmlee / time_difference

The missing Ruby method to print out time difference (duration) in year, month, week, day, hour, minute, and second.
https://rubygems.org/gems/time_difference
MIT License
305 stars 92 forks source link

time diff wrong calculation? #32

Closed mtricolici closed 6 years ago

mtricolici commented 7 years ago
# cat test.rb 
#!/usr/bin/env ruby
require 'date'
require 'time_difference'

now = Time.now.utc
puts "now is #{now}"

backup_date = DateTime.strptime('2017-06-28-000001', '%Y-%m-%d-%H%M%S').utc
puts "backup_date is #{backup_date}"

h = TimeDifference.between(backup_date, now).in_hours
puts "...time diff in hours=#{h}"
# ./test.rb 
now is 2017-06-28 07:54:20 UTC
backup_date is 2017-06-28 00:00:01 UTC
...time diff in hours=102454424.73
# ruby -v
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux-musl]
time_difference (0.5.0)
mtricolici commented 7 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
mtricolici commented 7 years ago

works fine in ruby 2.2. and 2.4. (issue in 2.3.1 & 2.3.4)

bodsch commented 7 years ago

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}
BLauris commented 7 years ago

I got this issue on all of these ruby versions 2.3.1, 2.3.3, 2.3.4 and 2.4.1.

aviflax commented 7 years ago

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!

aviflax commented 7 years ago

Oh and also I noticed that #31 might address this. Not entirely sure.

fadynaffa3 commented 7 years ago

@aviflax your 100% right the issue happened when I updated rails to 5.1.2 as 5.1.1 had no problems.

Jacotb commented 7 years ago

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 
fadynaffa3 commented 7 years ago

You can find the change here https://github.com/rails/rails/blob/v5.1.2/activesupport/CHANGELOG.md

conroywhitney commented 7 years ago

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.

tmlee commented 6 years ago

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