From 567338f662af0a1e776b2b1a5a37c41db7210817 Mon Sep 17 00:00:00 2001 From: khumnath <50103558+khumnath@users.noreply.github.com> Date: Sat, 7 Dec 2024 12:13:03 +0900 Subject: [PATCH] fix undefined tithi for some days. fix bsDay in combobox (#28) --- bikram.h | 72 ++++++++++++++++++++++----------------------- calendarwindow.cpp | 11 +++++-- calendarwindow.h | 2 +- mainwindow.cpp | 2 +- panchanga.h | 73 ++++++++++++++++++++++++++++------------------ 5 files changed, 89 insertions(+), 71 deletions(-) diff --git a/bikram.h b/bikram.h index daadb82..b3bc164 100644 --- a/bikram.h +++ b/bikram.h @@ -3,7 +3,7 @@ #include -class bikram { +class Bikram { private: int Year = 0; int Month = -1; @@ -11,15 +11,15 @@ class bikram { double YugaRotation_star = 1582237828; double YugaRotation_sun = 4320000; - double YugaCivilDays; - double PlanetApogee_sun = 77 + static_cast(17) / 60; - double PlanetCircumm_sun = 13 + static_cast(50) / 60; + double YugaCivilDays = YugaRotation_star - YugaRotation_sun; // Initialize here + double PlanetApogee_sun = 77 + 17.0 / 60; // Use double for division + double PlanetCircumm_sun = 13 + 50.0 / 60; // Use double for division static constexpr double rad = 57.2957795; // 180 / pi - void get_saura_masa_day(long ahar, int* m, int* d) const; - int today_saura_masa_first_p(long ahar) const; - double get_tslong(long ahar) const; - double get_julian_date(int year, int month, int day) const; + void getSauraMasaDay(long ahar, int* m, int* d) const; + int todaySauraMasaFirstP(long ahar) const; + double getTslong(long ahar) const; + double getJulianDate(int year, int month, int day) const; void fromJulianDate(double julian_date, int& year, int& month, int& day) const; public: @@ -31,32 +31,32 @@ class bikram { int daysInMonth(int year, int month); }; -inline void bikram::get_saura_masa_day(long ahar, int* m, int* d) const { +inline void Bikram::getSauraMasaDay(long ahar, int* m, int* d) const { double tslong_tomorrow; int month; int day; - if (today_saura_masa_first_p(ahar)) { + if (todaySauraMasaFirstP(ahar)) { day = 1; - tslong_tomorrow = get_tslong(ahar + 1); - month = (long)(tslong_tomorrow / 30) % 12; + tslong_tomorrow = getTslong(ahar + 1); + month = static_cast(tslong_tomorrow / 30) % 12; month = (month + 12) % 12; } else { - get_saura_masa_day(ahar - 1, &month, &day); + getSauraMasaDay(ahar - 1, &month, &day); day += 1; } *m = month; *d = day; } -inline int bikram::today_saura_masa_first_p(long ahar) const { - double tslong_today = get_tslong(ahar); - double tslong_tomorrow = get_tslong(ahar + 1); - tslong_today = tslong_today - (int)(tslong_today / 30) * 30; - tslong_tomorrow = tslong_tomorrow - (int)(tslong_tomorrow / 30) * 30; - return (25 < tslong_today) && (tslong_tomorrow < 5) ? 1 : 0; +inline int Bikram::todaySauraMasaFirstP(long ahar) const { + double tslong_today = getTslong(ahar); + double tslong_tomorrow = getTslong(ahar + 1); + tslong_today -= static_cast(tslong_today / 30) * 30; + tslong_tomorrow -= static_cast(tslong_tomorrow / 30) * 30; + return (25 < tslong_today && tslong_tomorrow < 5) ? 1 : 0; } -inline double bikram::get_tslong(long ahar) const { +inline double Bikram::getTslong(long ahar) const { double mslong; double t1 = (YugaRotation_sun * ahar / YugaCivilDays); t1 -= static_cast(t1); @@ -70,17 +70,17 @@ inline double bikram::get_tslong(long ahar) const { return x3; } -inline double bikram::get_julian_date(int year, int month, int day) const { +inline double Bikram::getJulianDate(int year, int month, int day) const { if (month <= 2) { year -= 1; month += 12; } double a = floor(year / 100.0); double b = 2 - a + floor(a / 4.0); - return floor(365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + b - 1524.5; + return floor (365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + b - 1524.5; } -inline void bikram::fromJulianDate(double julian_date, int& year, int& month, int& day) const { +inline void Bikram::fromJulianDate(double julian_date, int& year, int& month, int& day) const { int a = static_cast(julian_date + 0.5); int b = a + 1537; int c = static_cast((b - 122.1) / 365.25); @@ -92,13 +92,12 @@ inline void bikram::fromJulianDate(double julian_date, int& year, int& month, in year = (month > 2) ? (c - 4716) : (c - 4715); } -inline void bikram::fromGregorian(int y, int m, int d) { - YugaCivilDays = YugaRotation_star - YugaRotation_sun; - double julian = get_julian_date(y, m, d); +inline void Bikram::fromGregorian(int y, int m, int d) { + double julian = getJulianDate(y, m, d); long ahar = julian - 588465.5; int saura_masa_num; int saura_masa_day; - get_saura_masa_day(ahar, &saura_masa_num, &saura_masa_day); + getSauraMasaDay(ahar, &saura_masa_num, &saura_masa_day); long YearKali = static_cast(ahar * YugaRotation_sun / YugaCivilDays); int YearSaka = YearKali - 3179; int nepalimonth = (saura_masa_num) % 12; @@ -107,42 +106,41 @@ inline void bikram::fromGregorian(int y, int m, int d) { Day = saura_masa_day; } -inline void bikram::toGregorian(int bsYear, int bsMonth, int bsDay, int& gYear, int& gMonth, int& gDay) { - YugaCivilDays = YugaRotation_star - YugaRotation_sun; +inline void Bikram::toGregorian(int bsYear, int bsMonth, int bsDay, int& gYear, int& gMonth, int& gDay) { int YearSaka = bsYear - 135; long YearKali = YearSaka + 3179; long ahar = static_cast((YearKali * YugaCivilDays) / YugaRotation_sun); int saura_masa_num, saura_masa_day; - get_saura_masa_day(ahar, &saura_masa_num, &saura_masa_day); + getSauraMasaDay(ahar, &saura_masa_num, &saura_masa_day); bsMonth = (bsMonth + 11) % 12; while (saura_masa_num != bsMonth || saura_masa_day != bsDay) { ahar += (saura_masa_num < bsMonth || (saura_masa_num == bsMonth && saura_masa_day < bsDay)) ? 1 : -1; - get_saura_masa_day(ahar, &saura_masa_num, &saura_masa_day); + getSauraMasaDay(ahar, &saura_masa_num, &saura_masa_day); } double julian_date = ahar + 588465.5; fromJulianDate(julian_date, gYear, gMonth, gDay); } -inline int bikram::getYear() const { +inline int Bikram::getYear() const { return Year; } -inline int bikram::getMonth() const { +inline int Bikram::getMonth() const { return Month + 1; } -inline int bikram::getDay() const { +inline int Bikram::getDay() const { return Day; } -inline int bikram::daysInMonth(int bsYear, int bsMonth) { +inline int Bikram::daysInMonth(int bsYear, int bsMonth) { int gYear, gMonth, gDay; int nextMonth = (bsMonth % 12) + 1; int nextYear = (bsMonth == 12) ? bsYear + 1 : bsYear; toGregorian(bsYear, bsMonth, 1, gYear, gMonth, gDay); - double julian_date_start = get_julian_date(gYear, gMonth, gDay); + double julian_date_start = getJulianDate(gYear, gMonth, gDay); toGregorian(nextYear, nextMonth, 1, gYear, gMonth, gDay); - double julian_date_end = get_julian_date(gYear, gMonth, gDay); + double julian_date_end = getJulianDate(gYear, gMonth, gDay); return static_cast(julian_date_end - julian_date_start); } diff --git a/calendarwindow.cpp b/calendarwindow.cpp index ac02e5a..325b860 100755 --- a/calendarwindow.cpp +++ b/calendarwindow.cpp @@ -424,9 +424,14 @@ void CalendarWindow::onBsDayChanged(int /*index*/) { int day = ui->dayselectBS->currentText().toInt(); updateAdDateFromBs(year, month, day); + + // Ensure the selected day remains consistent + if (ui->dayselectBS->currentText().toInt() != day) { + ui->dayselectBS->setCurrentText(QString::number(day)); + } + blockSignals = false; } - void CalendarWindow::updateBsDateFromAd(int year, int month, int day) { converter.fromGregorian(year, month, day); @@ -496,14 +501,14 @@ void CalendarWindow::updateAdDateFromBs(int year, int month, int day) { int bsDaysInMonth = converter.daysInMonth(year, month); - QString bsMonthName = getBikramMonthName(month); + QString gmonthname = getEnglishMonthName(gMonth); double julianDate = gregorianToJulian(gYear, gMonth, gDay); Panchang panchang(julianDate); QString tithiName = QString::fromStdString(tithi[(int)panchang.tithi_index]); QString paksha = QString::fromStdString(panchang.paksha); QString tithipaksha = QString("%1 %2").arg(paksha).arg(tithiName); ui->output->setText(QString("ईसवी सन मा परिवर्तन गरियो: %1 %2 %3 गते %5 \n%2 %1 मा जम्मा दिन सङ्ख्या: %4") - .arg(convertToNepaliNumerals(gYear)).arg(bsMonthName).arg(convertToNepaliNumerals(gDay)).arg(convertToNepaliNumerals(bsDaysInMonth)).arg(tithipaksha)); + .arg(convertToNepaliNumerals(gYear)).arg(gmonthname).arg(convertToNepaliNumerals(gDay)).arg(convertToNepaliNumerals(bsDaysInMonth)).arg(tithipaksha)); // Update the calendar updateCalendar(year, month); diff --git a/calendarwindow.h b/calendarwindow.h index 2ca5648..6f33570 100644 --- a/calendarwindow.h +++ b/calendarwindow.h @@ -52,7 +52,7 @@ private slots: private: Ui::CalendarWindow *ui; - bikram converter; + Bikram converter; bool blockSignals; int gYear, gMonth, gDay; // Moved to private section for encapsulation QDate currentBikramDate; diff --git a/mainwindow.cpp b/mainwindow.cpp index 248bffd..4a2c385 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -145,7 +145,7 @@ MainWindow::~MainWindow() // considering bikram date as nepali date. int MainWindow::cnvToNepali(int mm, int dd, int yy) { // Perform the conversion using the bikram class - bikram bsdate; + Bikram bsdate; bsdate.fromGregorian(yy, mm, dd); // Convert the provided Gregorian date to Bikram Sambat // Get the converted Nepali date components from the bikram class diff --git a/panchanga.h b/panchanga.h index 783d5fd..e0e207e 100644 --- a/panchanga.h +++ b/panchanga.h @@ -3,17 +3,16 @@ #include #include -#include #define PI 3.14159265358979323846 -#define r2d 180.0 / PI -#define d2r PI / 180.0 +#define r2d (180.0 / PI) +#define d2r (PI / 180.0) // Tithi names in Nepali static const char tithi[][30] = { "प्रथमा", "द्वितीया", "तृतीया", "चतुर्थी", "पंचमी", "षष्ठी", "सप्तमी", "अष्टमी", - "नवमी", "दशमी", "एकादशी", "द्वादशी", "त्रयोदशी", "चतुर्दशी", "पूर्णिमा", "प्रथमा", - "द्वितीया", "तृतीया", "चतुर्थी", "पंचमी", "षष्ठी", "सप्तमी", "अष्टमी", "नवमी", "दशमी", - "एकादशी", "द्वादशी", "त्रयोदशी", "चतुर्दशी", "अमावस्या" }; + "नवमी", "दशमी", "एकादशी", "द्वादशी", "त्रयोदशी", "चतुर्दशी", "पूर्णिमा", "प्रथमा", + "द्वितीया", "तृतीया", "चतुर्थी", "पंचमी", "षष्ठी", "सप्तमी", "अष्टमी", "नवमी", "दशमी", + "एकादशी", "द्वादशी", "त्रयोदशी", "चतुर्दशी", "अमावस्या" }; class Panchang { public: @@ -21,6 +20,7 @@ class Panchang { double t, tithi_index; std::string paksha; + // Constructor accepting a Julian date Panchang(double julianDate) { tdays = julianDate - 2451545.0; // Days since J2000.0 t = tdays / 36525.0; @@ -36,40 +36,55 @@ class Panchang { double moon_longitude = getMoonLongitude(); double sun_longitude = getSunLongitude(); + // Adjust for Nepal time (UTC+5:45) + double nepalTimeOffset = 5.75; // in hours + double localTimeAdjustment = nepalTimeOffset * 15; // Convert hours to degrees + + // Adjust the longitudes + moon_longitude += localTimeAdjustment; + sun_longitude += localTimeAdjustment; + + // Wrap around to keep within 0-360 degrees + moon_longitude = fmod(moon_longitude + 360.0, 360.0); + sun_longitude = fmod(sun_longitude + 360.0, 360.0); + + // Calculate the difference double difference = moon_longitude - sun_longitude; if (difference < 0) difference += 360.0; - // Round the tithi index according to the specified rules - tithi_index = std::round(difference / 12.0); - tithi_index = static_cast(tithi_index) % 30; // Wrap around to ensure valid index - + tithi_index = std::floor(difference / 12.0); paksha = (tithi_index < 15) ? "शुक्ल पक्ष" : "कृष्ण पक्ष"; - - // Debug print - std::cout << "Tithi: " << tithi[static_cast(tithi_index)] << " (" << paksha << ")" << std::endl; - } - double getSunLongitude() { - double l0 = 280.4665 + 36000.7698 * t; - double m = 357.5291 + 35999.0503 * t; - double c = (1.9146 - 0.004817 * t - 0.000014 * t * t) * sin(m * d2r) - + (0.019993 - 0.000101 * t) * sin(2 * m * d2r) - + 0.00029 * sin(3 * m * d2r); - double theta = l0 + c; - double lambda = theta - 0.00569 - 0.00478 * sin((125.04 - 1934.136 * t) * d2r); - return fmod(lambda, 360.0); + // Debug print + // std::cout << "Tithi: " << tithi[(int)tithi_index] << " (" << paksha << ")" << std::endl; } double getMoonLongitude() { double L1 = 218.316 + 481267.8813 * t; - double M1 = 134.963 + 477198.8676 * t; + double D = 297.8502 + 445267.1115 * t; + // double M = 357.5291 + 35999.0503 * t; + double M1 = 134.963 + 477198.8671 * t; + // double F = 93.272 + 483202.0175 * t; + + double moonLongitude = L1 + + (6.289 * sin(M1 * M_PI / 180.0)) + - (1.274 * sin((2 * D - M1) * M_PI / 180.0)) + - (0.658 * sin(2 * D * M_PI / 180.0)) + - (0.214 * sin(2 * M1 * M_PI / 180.0)) + + (0.11 * sin(D * M_PI / 180.0)); + + return fmod(moonLongitude, 360.0); + } - L1 = fmod(L1, 360.0); - M1 = fmod(M1, 360.0); + double getSunLongitude() { + double l0 = 280.4665 + 36000.7698 * t; + double m = 357.5291 + 35999.0503 * t; + double c = (1.9146 - 0.004817 * t - 0.000014 * t * t) * sin(m * M_PI / 180.0) + + (0.019993 - 0.000101 * t) * sin(2 * m * M_PI / 180.0) + + 0.000289 * sin(3 * m * M_PI / 180.0); - double longitude = L1 + 6.289 * sin(M1 * d2r); - longitude = fmod(longitude, 360.0); - return longitude; + double sunLongitude = l0 + c; + return fmod(sunLongitude, 360.0); } };