class Yage::Diff

Public Class Methods

new(date = Date.today) click to toggle source
# File lib/yage/diff.rb, line 3
def initialize(date = Date.today)
  @base = date
end

Public Instance Methods

diff(d2 = Date.today) click to toggle source
# File lib/yage/diff.rb, line 7
def diff(d2 = Date.today)
  return if d2 < @base

  Data.new(@base,
           d2,
           diff_year(d2),
           diff_month(d2),
           diff_mday(d2),
           diff_yday(d2))
end

Private Instance Methods

diff_mday(d2) click to toggle source
# File lib/yage/diff.rb, line 34
def diff_mday(d2)
  d2.mday -
    @base.mday +
    (full_month?(d2) ? 0 : end_of_last_month(d2).mday)
end
diff_month(d2) click to toggle source
# File lib/yage/diff.rb, line 24
def diff_month(d2)
  if @base.month < d2.month
    (full_month?(d2) ? 0 : -1) + d2.month - @base.month
  elsif @base.month == d2.month
    full_year?(d2) ? 0 : 11
  else
    (full_year?(d2) ? 0 : 12) + d2.month - @base.month
  end
end
diff_yday(d2) click to toggle source
# File lib/yage/diff.rb, line 40
def diff_yday(d2)
  d2.yday -
    @base.yday +
    (full_year?(d2) ? 0 : end_of_year(d2).yday) -
    (d2.leap? ? 1 : 0)
end
diff_year(d2) click to toggle source
# File lib/yage/diff.rb, line 20
def diff_year(d2)
  d2.year - @base.year - (full_year?(d2) ? 0 : 1)
end
end_of_last_month(d2) click to toggle source
# File lib/yage/diff.rb, line 60
def end_of_last_month(d2)
  last_month = ([12] + (1..11).to_a)[d2.month - 1]
  Date.new(d2.year, last_month, -1)
end
end_of_year(d2) click to toggle source
# File lib/yage/diff.rb, line 52
def end_of_year(d2)
  Date.new(d2.year, 12, 31)
end
full_month?(d2) click to toggle source
# File lib/yage/diff.rb, line 56
def full_month?(d2)
  @base.mday <= d2.mday
end
full_year?(d2) click to toggle source
# File lib/yage/diff.rb, line 47
def full_year?(d2)
  @base.month < d2.month ||
    @base.month == d2.month && @base.mday <= d2.mday
end