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

Extra fraction in the result of in_each_component method #13

Closed wbotelhos closed 9 years ago

wbotelhos commented 9 years ago

Given:

time = Time.now
TimeDifference.between(time, time + 6.months).in_each_component

Then:

{
  :years   => 0.5,
  :months  => 6.02,
  :weeks   => 26.14,
  :days    => 183.0,
  :hours   => 4392.0,
  :minutes => 263520.0,
  :seconds => 15811200.0
}

The months has 0.02 more, why this difference, not just 6.0?

tmlee commented 9 years ago

@wbotelhos computer calculates at a higher precision http://www.wolframalpha.com/input/?i=Time+from+now+and+6+months+later

wbotelhos commented 9 years ago

Hi, Thanks for the answer.

I had to calculate manually using next_month and counting the index, because we have months with 28, 29, 30 and 31 days. In a big range of months we get a big difference of amount/month. 6 months * 1 dollar is different of 6.02 months vs 1 dollar. But it works fine for weeks, days and so...

Thank you.