diff --git a/Gemfile b/Gemfile index 3e233f7..97d5a83 100644 --- a/Gemfile +++ b/Gemfile @@ -4,5 +4,6 @@ source 'https://rubygems.org' gemspec group :test do + gem 'byebug' gem 'pry' end \ No newline at end of file diff --git a/lib/nepali_calendar/ad_calendar.rb b/lib/nepali_calendar/ad_calendar.rb index 4d85cea..ac52209 100644 --- a/lib/nepali_calendar/ad_calendar.rb +++ b/lib/nepali_calendar/ad_calendar.rb @@ -11,7 +11,7 @@ def bs_to_ad(year, month, day) ref_day_nep = get_ref_day_nep date_bs = "#{year}/#{month}/#{day}" return unless date_in_range?(date_bs, ref_day_nep) - days = total_days_for_bs(date_bs, ref_day_nep) + days = total_days_for_bs(date_bs, ref_day_nep) # ref = '2000/01/01' get_ad_date(days, ref_date['bs_to_ad']['ad']) end diff --git a/lib/nepali_calendar/bs_calendar.rb b/lib/nepali_calendar/bs_calendar.rb index e32a3d3..ddcdc72 100644 --- a/lib/nepali_calendar/bs_calendar.rb +++ b/lib/nepali_calendar/bs_calendar.rb @@ -9,12 +9,12 @@ def ad_to_bs(year, month, day) raise NepaliCalendar::Calendar::NilDateFieldsException unless valid_date_input?(year, month, day) raise NepaliCalendar::Calendar::InvalidADDateException unless valid_ad_date?(year, month, day) - ref_day_eng = get_ref_day_eng + ref_day_eng = get_ref_day_eng # 1994/1/1 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']) + get_bs_date(days, ref_date['ad_to_bs']['bs']) # days = 10372 when '2022-05-26', ref_date = '2009/9/17' end def get_bs_date(days, ref_day_nep) diff --git a/lib/nepali_calendar/calendar.rb b/lib/nepali_calendar/calendar.rb index 020e5d4..a3314a9 100644 --- a/lib/nepali_calendar/calendar.rb +++ b/lib/nepali_calendar/calendar.rb @@ -57,12 +57,12 @@ def self.total_days(date_eng, reference_date) days.to_i end - def self.total_days_for_bs(date_nep, reference_date) + def self.total_days_for_bs(date_nep, reference_date) # ref = '2000/1/1' ref_year, ref_month, ref_day = reference_date.split('/').map(&:to_i) nep_year, nep_month, nep_day = date_nep.split('/').map(&:to_i) temp_day = nep_day days = 0 - while nep_year >= ref_year && nep_month >= ref_month && ref_day!=nep_day + while nep_year >= ref_year && nep_month >= ref_month days += temp_day nep_month -= 1 if nep_month < 1 @@ -72,7 +72,16 @@ def self.total_days_for_bs(date_nep, reference_date) end nep_month = 12 end - temp_day = nep_year == ref_year && nep_month == ref_month ? NepaliCalendar::BS[nep_year][nep_month].to_i - 1 : NepaliCalendar::BS[nep_year][nep_month].to_i + + temp_day = begin + if nep_year == ref_year && nep_month == ref_month + # we need to accomodate for that fact that ref date '2000/1/1' so day begins at '1' not '0' + # thats why we need to subtract 1 when we successfully time travel to that date + NepaliCalendar::BS[nep_year][nep_month].to_i - 1 + else + NepaliCalendar::BS[nep_year][nep_month].to_i + end + end end days diff --git a/spec/nepali_calendar_spec.rb b/spec/nepali_calendar_spec.rb index 631dd8e..348224b 100644 --- a/spec/nepali_calendar_spec.rb +++ b/spec/nepali_calendar_spec.rb @@ -115,13 +115,14 @@ end + let(:ad_date) { NepaliCalendar::AdCalendar.bs_to_ad('2072', '04', '01') } it 'converts date from bs_to_ad' do expect(ad_date.year).to eq(2015) - expect(ad_date.month).to eq(9) - expect(ad_date.day).to eq(9) - expect(ad_date.wday).to eq(3) - expect(ad_date.month_name).to eq('September') - expect(ad_date.wday_name).to eq('Wednesday') + expect(ad_date.month).to eq(7) + expect(ad_date.day).to eq(17) + expect(ad_date.wday).to eq(5) + expect(ad_date.month_name).to eq('July') + expect(ad_date.wday_name).to eq('Friday') end