From 4ba41e712ac041785be8029cabc49787ac1c112c Mon Sep 17 00:00:00 2001 From: sangam Date: Thu, 9 Mar 2023 10:16:25 +0545 Subject: [PATCH 1/9] add to_ad method in bs calendar --- lib/nepali_calendar/bs_calendar.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/nepali_calendar/bs_calendar.rb b/lib/nepali_calendar/bs_calendar.rb index 54cd07d..3363d65 100644 --- a/lib/nepali_calendar/bs_calendar.rb +++ b/lib/nepali_calendar/bs_calendar.rb @@ -146,5 +146,29 @@ def date_range start_date.end_of_month.end_of_week ] end + + def to_ad + if !year || !month || !day + raise NilDateFieldsException + end + + if !NepaliCalendar::Calendar.valid_date_input?(year, month, day) + raise InvalidBSDateException + end + + ad_date = NepaliCalendar::Calendar.ref_date['bs_to_ad']['ad'] + bs_date = NepaliCalendar::Calendar.ref_date['bs_to_ad']['bs'] + + total_days = NepaliCalendar::Calendar.total_days_for_bs("#{year}/#{month}/#{day}", bs_date) + ad_date = Date.parse(ad_date) + total_days + { + year: ad_date.year, + month: ad_date.month, + day: ad_date.day, + wday: ad_date.wday, + month_name: I18n.t("date.month_names")[ad_date.month], + wday_name: I18n.t("date.day_names")[ad_date.wday], + } + end end end From e9e816fd03ab00673fc9bdb666c70e661b0d161e Mon Sep 17 00:00:00 2001 From: sangam Date: Thu, 9 Mar 2023 14:27:06 +0545 Subject: [PATCH 2/9] to_date method for AdCalendar --- lib/nepali_calendar/ad_calendar.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/nepali_calendar/ad_calendar.rb b/lib/nepali_calendar/ad_calendar.rb index 640449d..d5ff5d3 100644 --- a/lib/nepali_calendar/ad_calendar.rb +++ b/lib/nepali_calendar/ad_calendar.rb @@ -111,5 +111,9 @@ def date_range start_date.end_of_month.end_of_week ] end + + def to_date + Date.new(year, month, day) + end end end From 47d3a099e26ffacecc74b08272afe6ce812604dd Mon Sep 17 00:00:00 2001 From: sangam Date: Thu, 9 Mar 2023 14:34:41 +0545 Subject: [PATCH 3/9] specs for to_date method --- spec/nepali_calendar_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/nepali_calendar_spec.rb b/spec/nepali_calendar_spec.rb index 47de788..6c875c6 100644 --- a/spec/nepali_calendar_spec.rb +++ b/spec/nepali_calendar_spec.rb @@ -158,6 +158,23 @@ d1 = NepaliCalendar::AdCalendar.bs_to_ad(2076, 7, 15).end_of_month expect(d1).to eq(Date.parse('Sat, 30 Nov, 2019')) end + + describe '.to_date' do + it 'returns a Date object for a valid date string' do + date_string = '2022-03-09' + date = date_string.to_date + expect(date).to be_a(Date) + expect(date.year).to eq(2022) + expect(date.month).to eq(3) + expect(date.day).to eq(9) + end + + it 'raises an error for an invalid date string' do + expect { ad_date.to_date('2022-02-31') }.to raise_error(ArgumentError) + expect { ad_date.to_date('2022-13-01') }.to raise_error(ArgumentError) + expect { ad_date.to_date('2022-01-') }.to raise_error(ArgumentError) + end + end end context '#FiscalYear' do From 981fd68beae866a613efba2e0c1114436c99e001 Mon Sep 17 00:00:00 2001 From: sangam Date: Mon, 13 Mar 2023 11:06:36 +0545 Subject: [PATCH 4/9] bug fixes in ad_calendar --- lib/nepali_calendar/ad_calendar.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/nepali_calendar/ad_calendar.rb b/lib/nepali_calendar/ad_calendar.rb index 640449d..af86edf 100644 --- a/lib/nepali_calendar/ad_calendar.rb +++ b/lib/nepali_calendar/ad_calendar.rb @@ -28,29 +28,30 @@ def today end def beginning_of_week - date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month], - wday_name: Date::DAYNAMES[wday] } + debugger + date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month.to_i], + wday_name: Date::DAYNAMES[wday.to_i] } formatted_date = Date.parse(NepaliCalendar::Calendar.new('', date).to_s) formatted_date.beginning_of_week end def end_of_week - date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month], - wday_name: Date::DAYNAMES[wday] } + date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month.to_i], + wday_name: Date::DAYNAMES[wday.to_i] } formatted_date = Date.parse(NepaliCalendar::Calendar.new('', date).to_s) formatted_date.end_of_week end def beginning_of_month - date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month], - wday_name: Date::DAYNAMES[wday] } + date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month.to_i], + wday_name: Date::DAYNAMES[wday.to_i] } formatted_date = Date.parse(NepaliCalendar::Calendar.new('', date).to_s) formatted_date.beginning_of_month end def end_of_month - date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month], - wday_name: Date::DAYNAMES[wday] } + date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month.to_i], + wday_name: Date::DAYNAMES[wday.to_i] } formatted_date = Date.parse(NepaliCalendar::Calendar.new('', date).to_s) formatted_date.end_of_month end @@ -70,7 +71,7 @@ def self.travel_forward(days) ad = Date.parse(ref_date['bs_to_ad']['ad']) + days option = { year: ad.year, month: ad.month, day: ad.day, wday: ad.wday, - month_name: Date::MONTHNAMES[ad.month], wday_name: ad.strftime('%A') } + month_name: Date::MONTHNAMES[ad.month.to_i], wday_name: ad.strftime('%A') } new('', option) end @@ -111,5 +112,9 @@ def date_range start_date.end_of_month.end_of_week ] end + + def to_date + Date.new(year, month, day) + end end end From 2fdf4ad1aae7fe4ed779b108292f1e53bb7caa95 Mon Sep 17 00:00:00 2001 From: sangam Date: Tue, 14 Mar 2023 13:53:05 +0545 Subject: [PATCH 5/9] date method in bs calendar to display full date with month name and week day name and minor fixes --- lib/nepali_calendar/ad_calendar.rb | 1 - lib/nepali_calendar/bs_calendar.rb | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/nepali_calendar/ad_calendar.rb b/lib/nepali_calendar/ad_calendar.rb index af86edf..6180088 100644 --- a/lib/nepali_calendar/ad_calendar.rb +++ b/lib/nepali_calendar/ad_calendar.rb @@ -28,7 +28,6 @@ def today end def beginning_of_week - debugger date = { year: year, month: month, day: day, wday: wday, month_name: Date::MONTHNAMES[month.to_i], wday_name: Date::DAYNAMES[wday.to_i] } formatted_date = Date.parse(NepaliCalendar::Calendar.new('', date).to_s) diff --git a/lib/nepali_calendar/bs_calendar.rb b/lib/nepali_calendar/bs_calendar.rb index 3363d65..c6b5365 100644 --- a/lib/nepali_calendar/bs_calendar.rb +++ b/lib/nepali_calendar/bs_calendar.rb @@ -4,6 +4,7 @@ module NepaliCalendar class BsCalendar < NepaliCalendar::Calendar MONTHNAMES = %w[nil Baisakh Jestha Ashad Shrawn Bhadra Ashwin Kartik Mangshir Poush Magh Falgun Chaitra].freeze DAYNAMES = %w[nil Aitabar Sombar Mangalbar Budhbar Bihibar Sukrabar Sanibar].freeze + class << self def ad_to_bs(year, month, day) raise NepaliCalendar::Calendar::NilDateFieldsException unless valid_date_input?(year, month, day) @@ -29,6 +30,20 @@ def today end end + def date + # Convert nepali date to Ad date + date = NepaliCalendar::BsCalendar.new(nil, {year: @year, month: @month, day: @day}).to_ad + + # Create new date object with the Ad date + date_object = Date.new(date[:year], date[:month], date[:day]) + + # Get the week_day name, month name + weekday_name = DAYNAMES[date_object.wday + 1] + month_name = MONTHNAMES[@month] + + { weekday_name: weekday_name, month_name: month_name, day: @day, month: @month, year: @year } + end + def beginning_of_week date = { year: year, month: month, day: day, wday: wday } days = wday > 1 ? -(wday - 1) : 0 From d314ea9bb16432f6d80f31e42d6b4c3f3ae674d0 Mon Sep 17 00:00:00 2001 From: sangam Date: Wed, 15 Mar 2023 10:19:26 +0545 Subject: [PATCH 6/9] refactor --- lib/nepali_calendar/bs_calendar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nepali_calendar/bs_calendar.rb b/lib/nepali_calendar/bs_calendar.rb index c6b5365..0621b11 100644 --- a/lib/nepali_calendar/bs_calendar.rb +++ b/lib/nepali_calendar/bs_calendar.rb @@ -32,7 +32,7 @@ def today def date # Convert nepali date to Ad date - date = NepaliCalendar::BsCalendar.new(nil, {year: @year, month: @month, day: @day}).to_ad + date = self.to_ad # Create new date object with the Ad date date_object = Date.new(date[:year], date[:month], date[:day]) From 2d1306c3559fc3982c3a90f8f5dd7873bbe8f5c2 Mon Sep 17 00:00:00 2001 From: sangam Date: Wed, 15 Mar 2023 10:48:58 +0545 Subject: [PATCH 7/9] changes from review --- spec/nepali_calendar_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/nepali_calendar_spec.rb b/spec/nepali_calendar_spec.rb index 6c875c6..145d21a 100644 --- a/spec/nepali_calendar_spec.rb +++ b/spec/nepali_calendar_spec.rb @@ -160,13 +160,13 @@ end describe '.to_date' do - it 'returns a Date object for a valid date string' do - date_string = '2022-03-09' - date = date_string.to_date + it 'returns a Date object for a valid Ad date' do + ad_date = NepaliCalendar::AdCalendar.new(nil, { year: 2033, month: 12, day:12 }) + date = ad_date.to_date expect(date).to be_a(Date) - expect(date.year).to eq(2022) - expect(date.month).to eq(3) - expect(date.day).to eq(9) + expect(date.year).to eq(2033) + expect(date.month).to eq(12) + expect(date.day).to eq(12) end it 'raises an error for an invalid date string' do From c917dcda5037f09d6d71bb859bd8f42d56c01364 Mon Sep 17 00:00:00 2001 From: sangam Date: Wed, 15 Mar 2023 10:50:17 +0545 Subject: [PATCH 8/9] rename ad_date to ad_calendar --- spec/nepali_calendar_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/nepali_calendar_spec.rb b/spec/nepali_calendar_spec.rb index 145d21a..d02a294 100644 --- a/spec/nepali_calendar_spec.rb +++ b/spec/nepali_calendar_spec.rb @@ -161,8 +161,8 @@ describe '.to_date' do it 'returns a Date object for a valid Ad date' do - ad_date = NepaliCalendar::AdCalendar.new(nil, { year: 2033, month: 12, day:12 }) - date = ad_date.to_date + ad_calendar = NepaliCalendar::AdCalendar.new(nil, { year: 2033, month: 12, day:12 }) + date = ad_calendar.to_date expect(date).to be_a(Date) expect(date.year).to eq(2033) expect(date.month).to eq(12) From b794e9af93e0265d60984947f2feefe82bc1fbc5 Mon Sep 17 00:00:00 2001 From: sangam Date: Wed, 15 Mar 2023 11:18:57 +0545 Subject: [PATCH 9/9] spec for to_ad method in bs_calendar --- spec/nepali_calendar_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/nepali_calendar_spec.rb b/spec/nepali_calendar_spec.rb index d02a294..bfca960 100644 --- a/spec/nepali_calendar_spec.rb +++ b/spec/nepali_calendar_spec.rb @@ -102,6 +102,15 @@ d1 = NepaliCalendar::BsCalendar.ad_to_bs(2015, 10, 20).end_of_month expect(d1.class).to eq(NepaliCalendar::BsCalendar) end + + describe '#to_ad' do + let(:expected_date) { { year: 2023, month: 3, day: 7, wday: 2, month_name: "March", wday_name: "Tuesday" } } + let(:bs_calendar) { NepaliCalendar::BsCalendar.new(nil, { year: 2079, month: 11, day: 23 }) } + + it 'converts the date from the BS to the AD date' do + expect(bs_calendar.to_ad).to eq(expected_date) + end + end end context '#AdCalendar' do @@ -159,7 +168,7 @@ expect(d1).to eq(Date.parse('Sat, 30 Nov, 2019')) end - describe '.to_date' do + describe '#to_date' do it 'returns a Date object for a valid Ad date' do ad_calendar = NepaliCalendar::AdCalendar.new(nil, { year: 2033, month: 12, day:12 }) date = ad_calendar.to_date