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
# 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
returns self
# File lib/mallard/date.rb, line 31 def date self end
days in X months ago
# File lib/mallard/date.rb, line 174 def diXm (n) sXm(n).ditm end
days in last month
# File lib/mallard/date.rb, line 91 def dilm (Mallard::Date.new(year, month) - 1).day end
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
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
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
end X months ago
# File lib/mallard/date.rb, line 159 def eXm (n) (stm << (n - 1)) - 1 end
end X weeks ago
# File lib/mallard/date.rb, line 169 def eXw (n) sXw(n) + 6 end
end last month
# File lib/mallard/date.rb, line 101 def elm Mallard::Date.new(year, month) - 1 end
end last week
# File lib/mallard/date.rb, line 51 def elw etw - 7 end
end of this month
# File lib/mallard/date.rb, line 26 def etm (Mallard::Date.new(year, month) >> 1) - 1 end
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
end this month last year
# File lib/mallard/date.rb, line 86 def etmly (Mallard::Date.new(year - 1, month) >> 1) - 1 end
end this quarter
# File lib/mallard/date.rb, line 61 def etq (Mallard::Date.new(year, quarter * 3) >> 1) - 1 end
end this week
# File lib/mallard/date.rb, line 41 def etw stw + 6 end
end this week last year
# File lib/mallard/date.rb, line 244 def etwly stwly + 6 end
jan 1st last year
# File lib/mallard/date.rb, line 204 def jan1ly Mallard::Date.new(lyear, 1, 1) end
jan 1st this year
# File lib/mallard/date.rb, line 199 def jan1ty Mallard::Date.new(year, 1, 1) end
last year
# File lib/mallard/date.rb, line 71 def lyear year - 1 end
# 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
current quarter
# File lib/mallard/date.rb, line 66 def quarter (month - 1) / 3 + 1 end
# 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
start X months ago
# File lib/mallard/date.rb, line 154 def sXm (n) slm << (n - 1) end
start X weeks ago
# File lib/mallard/date.rb, line 164 def sXw (n) stw - n * 7 end
start last month
# File lib/mallard/date.rb, line 96 def slm Mallard::Date.new(year, month) << 1 end
start last week
# File lib/mallard/date.rb, line 46 def slw stw - 7 end
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
start of this month
# File lib/mallard/date.rb, line 21 def stm Mallard::Date.new(year, month) end
start this month X years ago
# File lib/mallard/date.rb, line 189 def stmXy (n) Mallard::Date.new(year - n, month) end
start this month last year
# File lib/mallard/date.rb, line 81 def stmly Mallard::Date.new(year - 1, month) end
start this quarter
# File lib/mallard/date.rb, line 56 def stq Mallard::Date.new(year, (quarter - 1) * 3 + 1) end
start this week
# File lib/mallard/date.rb, line 36 def stw self - wday end
start this week last year
# File lib/mallard/date.rb, line 239 def stwly sly + (twly - 1) * 7 end
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
this day last year
# File lib/mallard/date.rb, line 249 def tdly stwly + wday end
# File lib/mallard/date.rb, line 253 def to_str strftime('%F') end
# File lib/mallard/date.rb, line 257 def today Mallard::Date.today end
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
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
the week year for last year. See wyear
# File lib/mallard/date.rb, line 214 def wlyear stw.year - 1 end
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
X years ago
# File lib/mallard/date.rb, line 179 def yearX (n) year - n end