Skip to content

Commit e66c42b

Browse files
committed
debug day difference fix between data driven and computed
1 parent 64cf547 commit e66c42b

File tree

1 file changed

+68
-61
lines changed

1 file changed

+68
-61
lines changed

resources/PanchangaCalculator.js

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ function generateDebugInfo(date, lat, lon, tz) {
632632
date.getUTCDate()
633633
);
634634
var ahar = jd - KaliEpoch + 0.25 + ((lon / 15 - tz) / 24);
635-
636635
var sunLong = trueLongitudeSun(ahar);
637636
var moonLong = trueLongitudeMoon(ahar);
638637
var tithiVal = getTithi(sunLong, moonLong);
@@ -642,101 +641,109 @@ function generateDebugInfo(date, lat, lon, tz) {
642641
var tithiName = resolveTithiName(tithiDay, paksha);
643642

644643
var karanaIdx = Math.floor(2 * tithiVal);
645-
var karanaName = karanaIdx > 0
646-
? (karanaIdx < 57 ? karanas[karanaIdx % 7 || 7] : karanas[karanaIdx - 57 + 8])
647-
: karanas[0];
644+
var karanaName = karanaIdx > 0 ? (karanaIdx < 57 ? karanas[(karanaIdx - 1) % 7 + 1] : karanas[karanaIdx - 57 + 8]) : karanas[0];
648645

649646
var bsInfoCalc = fromGregorianAstronomical(
650647
date.getUTCFullYear(),
651648
date.getUTCMonth() + 1,
652649
date.getUTCDate()
653650
);
654-
655651
var dayDifference = 0;
656-
var gregFromAstronomical = fromBikramSambat(
652+
var gregFromAstronomical_Today = fromBikramSambat(
657653
bsInfoCalc.year,
658654
bsInfoCalc.monthIndex,
659655
bsInfoCalc.day
660-
);
656+
);
661657

662-
if (gregFromAstronomical) {
663-
var timeDiff = date.getTime() - gregFromAstronomical.getTime();
658+
if (gregFromAstronomical_Today) {
659+
var timeDiff = date.getTime() - gregFromAstronomical_Today.getTime();
664660
dayDifference = Math.round(timeDiff / (1000 * 60 * 60 * 24));
665661
}
666662

663+
if (dayDifference === 0 && !bsInfoData.isComputed) {
664+
var isMismatch = (bsInfoData.day !== bsInfoCalc.day || bsInfoData.monthIndex !== bsInfoCalc.monthIndex);
665+
if (isMismatch) {
666+
var tomorrow = new Date(date.getTime());
667+
tomorrow.setUTCDate(date.getUTCDate() + 1);
668+
var bsInfoCalc_Tomorrow = fromGregorianAstronomical(
669+
tomorrow.getUTCFullYear(),
670+
tomorrow.getUTCMonth() + 1,
671+
tomorrow.getUTCDate()
672+
);
673+
674+
var gregFromAstronomical_Tomorrow = fromBikramSambat(
675+
bsInfoCalc_Tomorrow.year,
676+
bsInfoCalc_Tomorrow.monthIndex,
677+
bsInfoCalc_Tomorrow.day
678+
);
679+
680+
if (gregFromAstronomical_Tomorrow) {
681+
var timeDiff_Tomorrow = tomorrow.getTime() - gregFromAstronomical_Tomorrow.getTime();
682+
var dayDifference_Tomorrow = Math.round(timeDiff_Tomorrow / (1000 * 60 * 60 * 24));
683+
684+
if (dayDifference_Tomorrow !== 0) {
685+
dayDifference = dayDifference_Tomorrow;
686+
}
687+
}
688+
}
689+
}
690+
667691
var dayDifferenceDisplay = dayDifference;
668-
if (Math.abs(dayDifference) > 2) {  // Show in red if difference is more than 2 days
692+
if (Math.abs(dayDifference) > 2) {
669693
dayDifferenceDisplay = '<font color="#ff0000">' + dayDifference + '</font>';
670694
} else if (Math.abs(dayDifference) > 0) {
671695
dayDifferenceDisplay = '<font color="yellow">' + dayDifference + '</font>';
672696
}
697+
673698
var sunriseSunset = getSunriseSunset(date, lat, lon, tz);
674699
var isComputed = bsInfoData.isComputed;
675700
var computedbsdate = toDevanagari(bsInfoCalc.year) + " " + bsInfoCalc.monthName + " " + toDevanagari(bsInfoCalc.day);
676-
677701
var dataDrivenBsDateString = "N/A";
678-
var acceptedBsDate = computedbsdate; // fallback by default
702+
var acceptedBsDate = computedbsdate;
679703
var dataDrivenInfo = "";
680704

681705
if (bsInfoData && !isComputed) {
682706
dataDrivenBsDateString = toDevanagari(bsInfoData.year) + " " + bsInfoData.monthName + " " + toDevanagari(bsInfoData.day);
683707
acceptedBsDate = dataDrivenBsDateString;
684-
dataDrivenInfo =
685-
`<span style="font-size: 10pt; color: #00CED1;">Debug Information\n` +
686-
`(Data-Driven for date conversion):</span>\n` +
687-
"Gregorian Date: " + date + "\n" +
688-
"accepted BS Date: " + acceptedBsDate + "\n" +
689-
"(panchanga is based on data-driven conversion)\n";
708+
dataDrivenInfo = `<span style="font-size: 10pt; color: #00CED1;">Debug Information\n(Data-Driven for date conversion):</span>\n` + "Gregorian Date: " + date + "\n" + "accepted BS Date: " + acceptedBsDate + "\n" + "(panchanga is based on data-driven conversion)\n";
690709
} else {
691-
dataDrivenInfo =
692-
`<span style="font-size: 10pt; color: #FF6347;">Debug Information\n` +
693-
`(Astronomical Calculation - outside data range):</span>\n` +
694-
"Gregorian Date: " + date + "\n" +
695-
"accepted BS Date: " + acceptedBsDate + "\n" +
696-
"(panchanga is based on astronomical computation)\n";
710+
dataDrivenInfo = `<span style="font-size: 10pt; color: #FF6347;">Debug Information\n(Astronomical Calculation - outside data range):</span>\n` + "Gregorian Date: " + date + "\n" + "accepted BS Date: " + acceptedBsDate + "\n" + "(panchanga is based on astronomical computation)\n";
697711
}
698712

699-
700-
701713
var lunarMonthInfo = getLunarMonthNameWithAdhik(ahar);
702714
var lunarMonthDisplay = lunarMonthInfo.isAdhika ? "अधिक " + lunarMonthInfo.monthName + " " + paksha : lunarMonthInfo.monthName + " " + paksha;
703715

704716
var debugOutput =
705-
'<pre style="font-family: monospace; font-size: 8pt; color: white; white-space: pre; line-height: 1.2;">' +
706-
dataDrivenInfo +
707-
`<span style="color: orange;">Consistency Check:</span>\n` +
708-
"Data-Driven BS Date: " + dataDrivenBsDateString + "\n" +
709-
"Astronomical BS Date (Computed): " + computedbsdate + "\n" +
710-
"Day Difference: " + dayDifferenceDisplay + " " + (Math.abs(dayDifference) === 1 ? 'day' : 'days') + "\n" +
711-
`<span style="color: red;">Note: Positive = Astronomical date is behind;\n` +
712-
`Negative = Astronomical date is ahead</span>\n` +
713-
714-
" " +`<span style="color: yellow;">Lunar Month (Purnimanta): ` + lunarMonthDisplay + "</span>\n" +
715-
716-
`<span style="color: #B118E7;">--- Solar Outputs ---</span>\n` +
717-
"gregorianDate: " + Qt.formatDateTime(date, "dddd, MMMM d, yyyy") + "\n" +
718-
"sunrise: " + sunriseSunset.sunrise + "\n" +
719-
"sunset: " + sunriseSunset.sunset + "\n" +
720-
"sunRashi: " + rashis[Math.floor(sunLong / 30) % 12] + " | index: " + (Math.floor(sunLong / 30) % 12 + 1) + "\n" +
721-
722-
" " +`<span style="color: #B118E7;">--- Lunar Outputs ---</span>\n` +
723-
"tithi: " + tithiName + " | index: " + tithiDay + "\n" +
724-
"tithiAngle: " + (tithiVal * 12).toFixed(4) + "°\n" +
725-
"paksha: " + paksha + "\n" +
726-
"nakshatra: " + nakshatras[Math.floor(moonLong * 27 / 360) % 27] + " | index: " + (Math.floor(moonLong * 27 / 360) % 27 + 1) + "\n" +
727-
"yoga: " + yogas[Math.floor(zero360(sunLong + moonLong) * 27 / 360) % 27] + " | index: " + (Math.floor(zero360(sunLong + moonLong) * 27 / 360) % 27 + 1) + "\n" +
728-
"yogaAngle: " + zero360(sunLong + moonLong).toFixed(4) + "°\n" +
729-
"karana: " + karanaName + " | index: " + karanaIdx + "\n" +
730-
"karanaAngle: " + (2 * tithiVal).toFixed(4) + "\n" +
731-
"moonRashi: " + rashis[Math.floor(moonLong / 30) % 12] + " | index: " + (Math.floor(moonLong / 30) % 12 + 1) + "\n" +
732-
"adhikaMasa: " + calculateAdhikaMasa(ahar) + " (computed)\n" +
733-
734-
" " + `<span style="color: #B118E7;">--- Metadata ---</span>\n` +
735-
"bsMonthIndex (0-based): " + bsInfoCalc.monthIndex + "\n" +
736-
"weekday (UTC): " + weekdays[date.getUTCDay()] + "\n" +
737-
"isComputed: " + isComputed +
738-
'</pre>';
739-
717+
'<pre style="font-family: monospace; font-size: 8pt; color: white; white-space: pre; line-height: 1.2;">' +
718+
dataDrivenInfo +
719+
`<span style="color: orange;">Consistency Check:</span>\n` +
720+
"Data-Driven BS Date: " + dataDrivenBsDateString + "\n" +
721+
"Astronomical BS Date (Computed): " + computedbsdate + "\n" +
722+
"Day Difference: " + dayDifferenceDisplay + " " + (Math.abs(dayDifference) === 1 ? 'day' : 'days') + "\n" +
723+
`<span style="color: red;">Note: Positive = Astronomical date is behind;\n` +
724+
`Negative = Astronomical date is ahead</span>\n` +
725+
" " + `<span style="color: yellow;">Lunar Month (Purnimanta): ` + lunarMonthDisplay + "</span>\n" +
726+
`<span style="color: #B118E7;">--- Solar Outputs ---</span>\n` +
727+
"gregorianDate: " + Qt.formatDateTime(date, "dddd, MMMM d, yyyy") + "\n" +
728+
"sunrise: " + sunriseSunset.sunrise + "\n" + "sunset: " + sunriseSunset.sunset + "\n" +
729+
"sunRashi: " + rashis[Math.floor(sunLong / 30) % 12] + " | index: " + (Math.floor(sunLong / 30) % 12 + 1) + "\n" +
730+
" " + `<span style="color: #B118E7;">--- Lunar Outputs ---</span>\n` +
731+
"tithi: " + tithiName + " | index: " + tithiDay + "\n" +
732+
"tithiAngle: " + (tithiVal * 12).toFixed(4) + "°\n" + "paksha: " + paksha + "\n" +
733+
"nakshatra: " + nakshatras[Math.floor(moonLong / (360 / 27))] + " | index: " + (Math.floor(moonLong / (360 / 27)) + 1) + "\n" +
734+
"yoga: " + yogas[Math.floor(zero360(sunLong + moonLong) / (360 / 27))] + " | index: " + (Math.floor(zero360(sunLong + moonLong) / (360 / 27)) + 1) + "\n" +
735+
"yogaAngle: " + zero360(sunLong + moonLong).toFixed(4) + "°\n" +
736+
"karana: " + karanaName + " | index: " + karanaIdx + "\n" +
737+
"karanaAngle: " + (2 * tithiVal).toFixed(4) + "\n" +
738+
"moonRashi: " + rashis[Math.floor(moonLong / 30) % 12] + " | index: " + (Math.floor(moonLong / 30) % 12 + 1) + "\n" +
739+
"adhikaMasa: " + calculateAdhikaMasa(ahar) + " (computed)\n" +
740+
741+
" " + `<span style="color: #B118E7;">--- Metadata ---</span>\n` +
742+
"Julian Day: " + jd.toFixed(4) + "\n" +
743+
"Ahar: " + ahar.toFixed(4) + "\n" +
744+
"weekday (UTC): " + weekdays[date.getUTCDay()] + "\n" +
745+
"isComputed: " + isComputed +
746+
'</pre>';
740747

741748
var result = { debug: debugOutput.trim().replace(/^\s*[\r\n]/gm, "") };
742749
calculationCache[cacheKey] = result;

0 commit comments

Comments
 (0)