Skip to content
  • Michael J Gruber's avatar
    date: avoid "X years, 12 months" in relative dates · f1e9c548
    Michael J Gruber authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    When relative dates are more than about a year ago, we start
    writing them as "Y years, M months".  At the point where we
    calculate Y and M, we have the time delta specified as a
    number of days. We calculate these integers as:
    
      Y = days / 365
      M = (days % 365 + 15) / 30
    
    This rounds days in the latter half of a month up to the
    nearest month, so that day 16 is "1 month" (or day 381 is "1
    year, 1 month").
    
    We don't round the year at all, though, meaning we can end
    up with "1 year, 12 months", which is silly; it should just
    be "2 years".
    
    Implement this differently with months of size
    
      onemonth = 365/12
    
    so that
    
      totalmonths = (long)( (days + onemonth/2)/onemonth )
      years = totalmonths / 12
      months = totalmonths % 12
    
    In order to do this without floats, we write the first formula as
    
      totalmonths = (days*12*2 + 365) / (365*2)
    
    Tests and inspiration by Jeff King.
    
    Helped-by: default avatarJeff King <peff@peff.net>
    Signed-off-by: default avatarMichael J Gruber <git@drmicha.warpmail.net>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    f1e9c548