Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Danphe/debug conversion issue #3

Merged
merged 5 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'byebug'
gem 'pry'
end
2 changes: 1 addition & 1 deletion lib/nepali_calendar/ad_calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/nepali_calendar/bs_calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions lib/nepali_calendar/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions spec/nepali_calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down