class LunarBlessing::Date
Constants
- MAX_YEAR
- MIN_YEAR
Attributes
day[R]
leap[R]
month[R]
year[R]
Public Class Methods
new(year:, month:, day:, leap: false)
click to toggle source
# File lib/lunar_blessing.rb, line 14 def initialize(year:, month:, day:, leap: false) raise OutOfRange.new(year) if year < MIN_YEAR || year > MAX_YEAR @year = year @month = month @day = day @leap = leap @leap_month = DATES_MAPPING[offset][0].to_i reset_month_by_leap init_solar_date if @month > 12 || @solar_month > 12 @solar_year += 1 @solar_month -= 12 end @month -= 1 if @leap_month > 0 && @month > @leap_month end
Public Instance Methods
to_s(with_year: false)
click to toggle source
# File lib/lunar_blessing.rb, line 38 def to_s(with_year: false) mm = MONTH_MAPPING[month.to_i] dd = DAY_MAPPING[day.to_i] "#{with_year ? chinese_year : ''}#{mm}#{dd}" end
to_solar()
click to toggle source
# File lib/lunar_blessing.rb, line 34 def to_solar solar_date.to_s end
Private Instance Methods
chinese_year()
click to toggle source
# File lib/lunar_blessing.rb, line 47 def chinese_year yy = [] year.to_s.each_char do |y| yy << YEAR_MAPPING[y.to_i] end yy << '年' yy = yy.join end
init_solar_date()
click to toggle source
# File lib/lunar_blessing.rb, line 87 def init_solar_date solar_year solar_month solar_day end
offset()
click to toggle source
# File lib/lunar_blessing.rb, line 61 def offset @offset ||= year - MIN_YEAR end
reset_month_by_leap()
click to toggle source
# File lib/lunar_blessing.rb, line 81 def reset_month_by_leap if leap || (@leap_month > 0 && month > @leap_month) @month += 1 end end
solar_date()
click to toggle source
# File lib/lunar_blessing.rb, line 57 def solar_date ::Date.new(solar_year, solar_month, solar_day) + (day - 1) end
solar_day()
click to toggle source
# File lib/lunar_blessing.rb, line 73 def solar_day @solar_day ||= solar_string[-2..-1].to_i end
solar_month()
click to toggle source
# File lib/lunar_blessing.rb, line 69 def solar_month @solar_month ||= solar_string[0..1].to_i end
solar_string()
click to toggle source
# File lib/lunar_blessing.rb, line 65 def solar_string @solar_string ||= DATES_MAPPING[offset][month] end
solar_year()
click to toggle source
# File lib/lunar_blessing.rb, line 77 def solar_year @solar_year ||= year end