-
Notifications
You must be signed in to change notification settings - Fork 21
Description
Inconsistent data are returned in the hash
require "rubygems"
require "time_diff"
d1 = Date.new(2014, 1, 20)
d2 = Date.new(2013, 1, 20)
td2 = Time.diff(d1.to_time, d2.to_time)
d3 = Date.new(2013, 1, 19)
td3 = Time.diff(d1.to_time, d3.to_time)
d4 = Date.new(2013, 1, 21)
td4 = Time.diff(d1.to_time, d4.to_time)
p td2[:year] == 1
p td3[:year] == 1
p td4[:year] == 0
p td2
p td3
p td4
Output
false
true
true
{:year=>0, :month=>12, :week=>0, :day=>5, :hour=>0, :minute=>0, :second=>0, :diff=>"12 months, 5 days and 00:00:00"}
{:year=>1, :month=>0, :week=>0, :day=>0, :hour=>18, :minute=>0, :second=>0, :diff=>"1 year and 18:00:00"}
{:year=>0, :month=>12, :week=>0, :day=>4, :hour=>0, :minute=>0, :second=>0, :diff=>"12 months, 4 days and 00:00:00"}
The return value of today - (the same day of last year) might be a matter of discussion but everybody believed we were 1 year old and not 0 at our first birthday, so I think there is general consensus that t2[:year] is wrong.
Furthermore 0 years is inconsistent with the other data in the hash. 12 months and 5 days clearly sum to 1 year, not 0 (see my note on the length of months at the end).
The value of d3[:hour] is wrong, it should be 24 hours, or 1 day and 0 hours.
The value of d4[:month] is wrong, it should be 11 months plus 30 days.
I didn't look into the code but the cause seems to be that months are assumed to be 30 days long, which is not true. They have variable length and it should be taken into account.
March 1st 2014 00:01 - January 31st 2014 23:59 should return 1 month, 0 days even if there are only 28 days in between.
March 1st 2012 00:01 - January 31st 2012 23:59 should still return 1 month 0 days with 29 days in between.