class NepaliCalendar::Calendar
Constants
- DAYS
- MONTHS
Public Class Methods
new(y = Date.today.year, m = Date.today.month, d = Date.today.day)
click to toggle source
# File lib/nepali_calendar.rb, line 11 def initialize(y = Date.today.year, m = Date.today.month, d = Date.today.day) @year = y @month = m @day = d end
today()
click to toggle source
# File lib/nepali_calendar.rb, line 18 def today today = Date.today date = Calendar.new.ad_to_bs(today.year, today.month, today.day) end
Public Instance Methods
ad_to_bs(year, month, day)
click to toggle source
# File lib/nepali_calendar.rb, line 24 def ad_to_bs(year, month, day) fail 'Invalid date!' unless valid_date?(year, month, day) ref_day_eng = Date.parse(ref_date['ad_to_bs']['ad']) date_ad = Date.parse("#{year}/#{month}/#{day}") return unless date_in_range?(date_ad, ref_day_eng) days = total_days(date_ad, ref_day_eng) get_bs_date(days, ref_date['ad_to_bs']['bs']) end
bs_to_ad(year, month, day)
click to toggle source
# File lib/nepali_calendar.rb, line 65 def bs_to_ad(year, month, day) fail 'Invalid date!' unless valid_date?(year, month, day) ref_day_nep = ref_date['bs_to_ad']['bs'] date_bs = Date.parse("#{year}/#{month}/#{day}") return unless date_in_range?(date_bs, Date.parse(ref_day_nep)) get_ad_date(year, month, day, ref_day_nep) end
get_ad_date(year, month, day, ref_day_nep)
click to toggle source
# File lib/nepali_calendar.rb, line 76 def get_ad_date(year, month, day, ref_day_nep) ref_year, ref_month, ref_day = ref_day_nep.split('/').map(&:to_i) k = ref_year # No. of Days from year i = 0 days = 0 j = 0 while i < (year.to_i - ref_year) i += 1 while j < 12 days += NepaliCalendar::BS[k][j] j += 1 end j = 0 k += 1 end # No. of Days from month j = 0 while j < (month.to_i - ref_month) days += NepaliCalendar::BS[k][j] j += 1 end days += (day.to_i - ref_day) Date.parse(ref_date['bs_to_ad']['ad']) + days end
get_bs_date(days, ref_day_nep)
click to toggle source
# File lib/nepali_calendar.rb, line 35 def get_bs_date(days, ref_day_nep) year, month, day = ref_day_nep.split('/').map(&:to_i) i = year j = month while days != 0 bs_month_days = NepaliCalendar::BS[i][j - 1] day += 1 if day > bs_month_days month += 1 day = 1 j += 1 end if month > 12 year += 1 month = 1 end if j > 12 j = 1 i += 1 end days -= 1 end Date.parse("#{year}/#{month}/#{day}") end
Private Instance Methods
date_in_range?(date, reference_date)
click to toggle source
# File lib/nepali_calendar.rb, line 112 def date_in_range?(date, reference_date) date > reference_date end
ref_date()
click to toggle source
# File lib/nepali_calendar.rb, line 120 def ref_date { 'bs_to_ad' => { 'bs' => '2000/01/01', 'ad' => '1943/04/14' }, 'ad_to_bs' => { 'bs' => '2000/09/17', 'ad' => '1944/01/01' } } end
total_days(date_eng, reference_date)
click to toggle source
# File lib/nepali_calendar.rb, line 107 def total_days(date_eng, reference_date) days = date_eng - reference_date days.to_i end
valid_date?(year, month, day)
click to toggle source
# File lib/nepali_calendar.rb, line 116 def valid_date?(year, month, day) Date.valid_date?(year.to_i, month.to_i, day.to_i) end