class DatePeriodParser::Base
@api private
Attributes
offset[R]
value[R]
Public Class Methods
new(value, options = nil)
click to toggle source
# File lib/date_period_parser.rb, line 108 def initialize(value, options = nil) options ||= {} # in case someone sends Base.new("", nil) @value = value.to_s.freeze @offset = (options[:offset] || options['offset'] || DEFAULT_OFFSET).freeze end
Public Instance Methods
parse()
click to toggle source
# File lib/date_period_parser.rb, line 114 def parse case @value when /\Atoday\Z/i then parse_date(Date.today) when /\Ayesterday\Z/i then parse_date(Date.today - 1) when /\Ayday\Z/i then parse_date(Date.today - 1) when /\Acurrent-month\Z/i then parse_month(Date.today) when /\Aprevious-month\Z/i then parse_month(Date.today << 1) when /\Acurrent-year\Z/i then parse_year(Date.today) when /\Aprevious-year\Z/i then parse_year(Date.today << 12) when /\Amtd\Z/i then mtd when /\Aqtd\Z/i then quarter_of(Date.today.year, Date.today.month) when /\Aytd\Z/i then ytd when /\A\d\d\d\d\-Q\d\Z/i then parse_quarter when /\A\d\d\d\d\Z/i then parse_year when /\A\d\d\d\d\-\d\d\Z/i then parse_month when /\A\d\d\d\d\-\d\d\-\d\d\Z/i then parse_date else raise ArgumentError.new("invalid date period") end end
Protected Instance Methods
end_of_date(dt)
click to toggle source
# File lib/date_period_parser.rb, line 229 def end_of_date(dt) DateTime.new(dt.year, dt.month, dt.day, 23, 59, 59.999, offset) end
last_date_time_of_month(dt)
click to toggle source
# File lib/date_period_parser.rb, line 216 def last_date_time_of_month(dt) first_day_this_month = DateTime.new(dt.year, dt.month, 1, 0, 0, 0, offset) first_day_next_month = first_day_this_month >> 1 last_day_this_month = first_day_next_month - 1 d = last_day_this_month end_of_date(d) end
mtd()
click to toggle source
# File lib/date_period_parser.rb, line 151 def mtd now = now_with_offset [ DateTime.new(now.year, now.month, 1, 0, 0, 0, offset), now ] end
now_with_offset()
click to toggle source
# File lib/date_period_parser.rb, line 146 def now_with_offset d = DateTime.now DateTime.new(d.year, d.month, d.day, d.hour, d.minute, d.second, offset) end
parse_date(date = nil)
click to toggle source
# File lib/date_period_parser.rb, line 167 def parse_date(date = nil) if date.nil? year, month, day = @value.split("-").map(&:to_i) else year, month, day = date.year, date.month, date.day end date = DateTime.new(year, month, day, 0, 0, 0, offset) [ date, end_of_date(date) ] end
parse_month(date = nil)
click to toggle source
# File lib/date_period_parser.rb, line 182 def parse_month(date = nil) if date.nil? year, month = @value.split("-").map(&:to_i) else year, month = date.year, date.month end first = DateTime.new(year, month, 1, 0, 0, 0, offset) [ first, last_date_time_of_month(first) ] end
parse_quarter()
click to toggle source
# File lib/date_period_parser.rb, line 196 def parse_quarter year, quarter = @value.upcase.split("-Q").map(&:to_i) quarter_of(year, (quarter - 1) * 3 + 1) end
parse_year(date = nil)
click to toggle source
# File lib/date_period_parser.rb, line 202 def parse_year(date = nil) if date.nil? year = @value.to_i else year = date.year end first = DateTime.new(year, 1,1,0,0,0,offset) [ first, last_date_time_of_month(first >> 11) ] end
quarter_of(year, month)
click to toggle source
# File lib/date_period_parser.rb, line 135 def quarter_of(year, month) case month when 1..3 then [start_of_date(Date.new(year, 1)), end_of_date(Date.new(year, 3, 31))] when 4..6 then [start_of_date(Date.new(year, 4)), end_of_date(Date.new(year, 6, 30))] when 7..9 then [start_of_date(Date.new(year, 7)), end_of_date(Date.new(year, 9, 30))] when 10..12 then [start_of_date(Date.new(year, 10)), end_of_date(Date.new(year, 12, 31))] else raise ArgumentError.new("invalid date period") end end
start_of_date(dt)
click to toggle source
# File lib/date_period_parser.rb, line 225 def start_of_date(dt) DateTime.new(dt.year, dt.month, dt.day, 0, 0, 0, offset) end
ytd()
click to toggle source
# File lib/date_period_parser.rb, line 159 def ytd now = now_with_offset [ DateTime.new(now.year, 1, 1, 0, 0, 0, offset), now ] end