class Mallard::Date

Mallard::Date has a fairly simple naming convention: s = start, e = end d = day, w = week, m = month, q = quarter, y = year i = in t = this, l = last, [0-9] = N period ago. stmly and stm1y are the same thing. The only difference is that stmly is explicitly coded while stm1y is handled via method_missing and stmXy generally, methods return a Mallard::Date object. if the time period is spelt out rather than abbreviated (e. g. lyear), then the method returns the value of just that period rather than a date object also, any method with an 'i' in it will return numeric rather than a date

Public Instance Methods

conv(meth) click to toggle source
# File lib/mallard/date.rb, line 261
def conv (meth)
    if meth.class == Fixnum
        return self + meth
    elsif meth == meth.to_i.to_s
        return self + meth.to_i
    else
        return self.send(meth.to_sym)
    end
end
date() click to toggle source

returns self

# File lib/mallard/date.rb, line 31
def date
    self
end
diXm(n) click to toggle source

days in X months ago

# File lib/mallard/date.rb, line 174
def diXm (n)
    sXm(n).ditm
end
dilm() click to toggle source

days in last month

# File lib/mallard/date.rb, line 91
def dilm
    (Mallard::Date.new(year, month) - 1).day
end
ditm() click to toggle source

days in this month

# File lib/mallard/date.rb, line 16
def ditm    # for the uninitiated:  make a new date on the 1st of the month, add 1 month, subtract 1 day, return the day field
    ((Mallard::Date.new(year, month) >> 1) - 1).day
end
ditmXy(n) click to toggle source

days in this month X years ago

# File lib/mallard/date.rb, line 184
def ditmXy (n)
    ((Mallard::Date.new(year - n, month) >> 1) - 1).day
end
ditmly() click to toggle source

days in this month last year

# File lib/mallard/date.rb, line 76
def ditmly
    ((Mallard::Date.new(year - 1, month) >> 1) - 1).day
end
eXm(n) click to toggle source

end X months ago

# File lib/mallard/date.rb, line 159
def eXm (n)
    (stm << (n - 1)) - 1
end
eXw(n) click to toggle source

end X weeks ago

# File lib/mallard/date.rb, line 169
def eXw (n)
    sXw(n) + 6
end
elm() click to toggle source

end last month

# File lib/mallard/date.rb, line 101
def elm
    Mallard::Date.new(year, month) - 1
end
elw() click to toggle source

end last week

# File lib/mallard/date.rb, line 51
def elw
    etw - 7
end
etm() click to toggle source

end of this month

# File lib/mallard/date.rb, line 26
def etm
    (Mallard::Date.new(year, month) >> 1) - 1
end
etmXy(n) click to toggle source

end this month X years ago

# File lib/mallard/date.rb, line 194
def etmXy (n)
    (Mallard::Date.new(year - n, month) >> 1) - 1
end
etmly() click to toggle source

end this month last year

# File lib/mallard/date.rb, line 86
def etmly
    (Mallard::Date.new(year - 1, month) >> 1) - 1
end
etq() click to toggle source

end this quarter

# File lib/mallard/date.rb, line 61
def etq
    (Mallard::Date.new(year, quarter * 3) >> 1) - 1
end
etw() click to toggle source

end this week

# File lib/mallard/date.rb, line 41
def etw
    stw + 6
end
etwly() click to toggle source

end this week last year

# File lib/mallard/date.rb, line 244
def etwly
    stwly + 6
end
jan1ly() click to toggle source

jan 1st last year

# File lib/mallard/date.rb, line 204
def jan1ly
    Mallard::Date.new(lyear, 1, 1)
end
jan1ty() click to toggle source

jan 1st this year

# File lib/mallard/date.rb, line 199
def jan1ty
    Mallard::Date.new(year, 1, 1)
end
lyear() click to toggle source

last year

# File lib/mallard/date.rb, line 71
def lyear
    year - 1
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/mallard/date.rb, line 105
def method_missing(meth, *args, &block)
    if meth.to_s =~ /^s([0-9]+)m$/
        sXm($1.to_i)
    elsif meth.to_s =~ /^e([0-9]+)m$/
        eXm($1.to_i)
    elsif meth.to_s =~ /^s([0-9]+)w$/
        sXw($1.to_i)
    elsif meth.to_s =~ /^e([0-9]+)w$/
        eXw($1.to_i)
    elsif meth.to_s =~ /^di([0-9]+)m$/
        diXm($1.to_i)
    elsif meth.to_s =~ /^year([0-9]+)$/
        yearX($1.to_i)
    elsif meth.to_s =~ /^ditm([0-9]+)y$/
        ditmXy($1.to_i)
    elsif meth.to_s =~ /^stm([0-9]+)y$/
        stmXy($1.to_i)
    elsif meth.to_s =~ /^etm([0-9]+)y$/
        etmXy($1.to_i)
    else
        super
    end
end
quarter() click to toggle source

current quarter

# File lib/mallard/date.rb, line 66
def quarter
    (month - 1) / 3 + 1
end
respond_to?(meth) click to toggle source
Calls superclass method
# File lib/mallard/date.rb, line 129
def respond_to?(meth)
    if meth.to_s =~ /^s([0-9]+)m$/
        true
    elsif meth.to_s =~ /^e([0-9]+)m$/
        true
    elsif meth.to_s =~ /^s([0-9]+)w$/
        true
    elsif meth.to_s =~ /^e([0-9]+)w$/
        true
    elsif meth.to_s =~ /^di([0-9]+)m$/
        true
    elsif meth.to_s =~ /^year([0-9]+)$/
        true
    elsif meth.to_s =~ /^ditm([0-9]+)y$/
        true
    elsif meth.to_s =~ /^stm([0-9]+)y$/
        true
    elsif meth.to_s =~ /^etm([0-9]+)y$/
        true
    else
        super
    end
end
sXm(n) click to toggle source

start X months ago

# File lib/mallard/date.rb, line 154
def sXm (n)
    slm << (n - 1)
end
sXw(n) click to toggle source

start X weeks ago

# File lib/mallard/date.rb, line 164
def sXw (n)
    stw - n * 7
end
slm() click to toggle source

start last month

# File lib/mallard/date.rb, line 96
def slm
    Mallard::Date.new(year, month) << 1
end
slw() click to toggle source

start last week

# File lib/mallard/date.rb, line 46
def slw
    stw - 7
end
sly() click to toggle source

start last year

# File lib/mallard/date.rb, line 234
def sly
    Mallard::Date.new(wlyear, 1, 1) + (7 - Mallard::Date.new(wlyear, 1, 1).wday) % 7
end
stm() click to toggle source

start of this month

# File lib/mallard/date.rb, line 21
def stm
    Mallard::Date.new(year, month)
end
stmXy(n) click to toggle source

start this month X years ago

# File lib/mallard/date.rb, line 189
def stmXy (n)
    Mallard::Date.new(year - n, month)
end
stmly() click to toggle source

start this month last year

# File lib/mallard/date.rb, line 81
def stmly
    Mallard::Date.new(year - 1, month)
end
stq() click to toggle source

start this quarter

# File lib/mallard/date.rb, line 56
def stq
    Mallard::Date.new(year, (quarter - 1) * 3 + 1)
end
stw() click to toggle source

start this week

# File lib/mallard/date.rb, line 36
def stw
    self - wday
end
stwly() click to toggle source

start this week last year

# File lib/mallard/date.rb, line 239
def stwly
    sly + (twly - 1) * 7
end
sty() click to toggle source

start this year, from the week perspective, not simply Jan 1st

# File lib/mallard/date.rb, line 229
def sty
    Mallard::Date.new(wyear) + (7 - Mallard::Date.new(wyear).wday) % 7
end
tdly() click to toggle source

this day last year

# File lib/mallard/date.rb, line 249
def tdly
    stwly + wday
end
to_str() click to toggle source
# File lib/mallard/date.rb, line 253
def to_str
    strftime('%F')
end
today() click to toggle source
# File lib/mallard/date.rb, line 257
def today
    Mallard::Date.today
end
twly() click to toggle source

this week last year. Same as week unless we're in the last week of a 53 week year, in which case it's 52

# File lib/mallard/date.rb, line 224
def twly
    week == 53 ? 52 : week
end
week() click to toggle source

current week number

# File lib/mallard/date.rb, line 219
def week
    ((stw - sty) / 7).to_i + 1  # added the to_i to eliminate some weirdness when converting to string
end
wlyear() click to toggle source

the week year for last year. See wyear

# File lib/mallard/date.rb, line 214
def wlyear
    stw.year - 1
end
wyear() click to toggle source

the 'week' year, or in another words what year said week is week number X in. Will only be different from year in early January

# File lib/mallard/date.rb, line 209
def wyear
    stw.year
end
yearX(n) click to toggle source

X years ago

# File lib/mallard/date.rb, line 179
def yearX (n)
    year - n
end