@@ -226,39 +226,134 @@ function fromGregorianAstronomical(gYear, gMonth, gDay) {
226226    } ; 
227227} 
228228
229+ 
230+ 
229231function  findNewMoon ( ahar )  { 
230-     var  getElongation  =  function  ( a )  {  return  zero360 ( trueLongitudeMoon ( a )  -  trueLongitudeSun ( a ) ) ;  } ; 
231-     var  lo  =  ahar  -  2 ,  hi  =  ahar  +  2 ; 
232-     var  el  =  getElongation ( lo ) ,  eh  =  getElongation ( hi ) ; 
233-     if  ( el  <  eh )  el  +=  360 ; 
234-     for  ( var  i  =  0 ;  i  <  30 ;  i ++ )  { 
232+     var  getElongation  =  function ( a )  { 
233+         return  zero360 ( trueLongitudeMoon ( a )  -  trueLongitudeSun ( a ) ) ; 
234+     } ; 
235+ 
236+     var  guess  =  ahar ; 
237+     var  step  =  0.5 ; 
238+ 
239+     for  ( var  i  =  0 ;  i  <  10 ;  i ++ )  { 
240+         var  elong  =  getElongation ( guess ) ; 
241+ 
242+         if  ( elong  <  5  ||  elong  >  355 )  { 
243+             break ; 
244+         } 
245+ 
246+         if  ( elong  <  180 )  { 
247+             guess  -=  ( elong  /  360 )  *  29.53 ; 
248+         }  else  { 
249+             guess  +=  ( ( 360  -  elong )  /  360 )  *  29.53 ; 
250+         } 
251+     } 
252+ 
253+     var  lo  =  guess  -  1 ; 
254+     var  hi  =  guess  +  1 ; 
255+ 
256+     for  ( let  i  =  0 ;  i  <  50 ;  i ++ )  { 
235257        var  mid  =  ( lo  +  hi )  /  2 ; 
236258        var  em  =  getElongation ( mid ) ; 
237-         if  ( el  <  em )  em  -=  360 ; 
238-         if  ( em  >  0 )  {  lo  =  mid ;  el  =  em ;  } 
239-         else  {  hi  =  mid ;  eh  =  em ;  } 
259+ 
260+         if  ( em  <  180 )  { 
261+             hi  =  mid ; 
262+         }  else  { 
263+             lo  =  mid ; 
264+         } 
240265    } 
266+ 
241267    return  ( lo  +  hi )  /  2 ; 
242268} 
243269
244270function  calculateAdhikaMasa ( ahar )  { 
245-     const  tithi  =  getTithi ( trueLongitudeSun ( ahar ) ,  trueLongitudeMoon ( ahar ) ) ; 
246-     const  daysPerTithi  =  29.530588  /  30 ;  // ~0.9843 days 
247-     const  nextNewMoon  =  findNewMoon ( ahar  +  ( 30  -  tithi )  *  daysPerTithi ) ; 
248-     const  prevNewMoon  =  findNewMoon ( nextNewMoon  -  29.6 ) ; 
271+     var  lunarMonthStart  =  findNewMoon ( ahar ) ; 
272+     if  ( lunarMonthStart  >  ahar )  { 
273+         lunarMonthStart  =  findNewMoon ( lunarMonthStart  -  29.530588853 ) ; 
274+     } 
275+ 
276+     var  lunarMonthEnd  =  findNewMoon ( lunarMonthStart  +  29.530588853 ) ; 
277+     var  sunLongStart  =  trueLongitudeSun ( lunarMonthStart ) ; 
278+     var  sunLongEnd  =  trueLongitudeSun ( lunarMonthEnd ) ; 
279+ 
280+     var  startSign  =  Math . floor ( sunLongStart  /  30 ) ; 
281+     var  endSign  =  Math . floor ( sunLongEnd  /  30 ) ; 
282+ 
283+     var  signCrossings  =  0 ; 
284+     var  currentSign  =  startSign ; 
285+ 
286+     for  ( var  i  =  1 ;  i  <=  29 ;  i ++ )  { 
287+         var  checkAhar  =  lunarMonthStart  +  i ; 
288+         var  checkSunLong  =  trueLongitudeSun ( checkAhar ) ; 
289+         var  checkSign  =  Math . floor ( checkSunLong  /  30 ) ; 
290+ 
291+         if  ( checkSign  <  currentSign )  { 
292+             checkSign  +=  12 ; 
293+         } 
294+ 
295+         if  ( checkSign  >  currentSign )  { 
296+             signCrossings  +=  ( checkSign  -  currentSign ) ; 
297+             currentSign  =  checkSign  %  12 ; 
298+         } 
299+     } 
300+ 
301+     if  ( endSign  <  currentSign )  { 
302+         endSign  +=  12 ; 
303+     } 
304+     if  ( endSign  >  currentSign )  { 
305+         signCrossings  +=  ( endSign  -  currentSign ) ; 
306+     } 
307+ 
308+     var  result  =  "छैन" ; 
309+ 
310+     if  ( signCrossings  ===  0 )  { 
311+         result  =  "अधिक "  +  solarMonths [ startSign ] 
312+     } 
313+ 
314+     if  ( signCrossings  >=  2 )  { 
315+         var  skippedSign  =  ( startSign  +  1 )  %  12 ; 
316+         result  =  "क्षय "  +  solarMonths [ skippedSign ] 
317+     } 
249318
250-     const   signAtPrev   =   Math . floor ( trueLongitudeSun ( prevNewMoon )   /   30 ) ; 
251-      const   signAtNext   =   Math . floor ( trueLongitudeSun ( nextNewMoon )   /   30 ) ; 
319+     return   result ; 
320+ } 
252321
253-     if  ( signAtPrev  ===  signAtNext )  { 
254-         return  "अधिक "  +  solarMonths [ signAtNext ] ; 
322+ function  getLunarMonthNameWithAdhik ( ahar )  { 
323+     var  lunarMonthStart  =  findNewMoon ( ahar ) ; 
324+     if  ( lunarMonthStart  >  ahar )  { 
325+         lunarMonthStart  =  findNewMoon ( lunarMonthStart  -  29.53 ) ; 
255326    } 
256327
257-     if  ( ( signAtNext  -  signAtPrev  +  12 )  %  12  ===  2 )  { 
258-         return  "क्षय मास" ; 
328+     var  purnima  =  findNewMoon ( lunarMonthStart  +  14.77 ) ;  // ~14.77 days after new moon 
329+ 
330+     var  sunLongPurnima  =  trueLongitudeSun ( purnima ) ; 
331+     var  purnimaSign  =  Math . floor ( sunLongPurnima  /  30 ) ; 
332+ 
333+     var  signCrossings  =  0 ; 
334+     var  currentSign  =  Math . floor ( trueLongitudeSun ( lunarMonthStart )  /  30 ) ; 
335+ 
336+     for  ( var  i  =  1 ;  i  <=  29 ;  i ++ )  { 
337+         var  checkAhar  =  lunarMonthStart  +  i ; 
338+         var  checkSunLong  =  trueLongitudeSun ( checkAhar ) ; 
339+         var  checkSign  =  Math . floor ( checkSunLong  /  30 ) ; 
340+ 
341+         if  ( checkSign  <  currentSign )  { 
342+             checkSign  +=  12 ; 
343+         } 
344+ 
345+         if  ( checkSign  >  currentSign )  { 
346+             signCrossings  +=  ( checkSign  -  currentSign ) ; 
347+             currentSign  =  checkSign  %  12 ; 
348+         } 
259349    } 
260350
261-     return  "छैन" ; 
351+     var  isAdhik  =  ( signCrossings  ===  0 ) ; 
352+     var  result  =  { 
353+         monthName : solarMonths [ purnimaSign ] , 
354+         isAdhik : isAdhik 
355+     } ; 
356+     return  result ; 
262357} 
263358
264359function  getSunriseSunset ( date ,  lat ,  lon ,  tz )  { 
@@ -608,6 +703,12 @@ function generateDebugInfo(date, lat = 27.7172, lon = 85.3240, tz = 5.75) {
608703    // Calculate difference in milliseconds 
609704    var  timeDiff  =  date . getTime ( )  -  gregFromAstronomical . getTime ( ) ; 
610705    dayDifference  =  Math . round ( timeDiff  /  ( 1000  *  60  *  60  *  24 ) ) ; 
706+     var  dayDifferenceDisplay  =  dayDifference ; 
707+         if  ( Math . abs ( dayDifference )  >  2 )  {   // Show in red if difference is more than 2 days 
708+             dayDifferenceDisplay  =  `<font color="#ff0000">${ dayDifference }  ; 
709+         }  else  if  ( Math . abs ( dayDifference )  >  0 )  { 
710+             dayDifferenceDisplay  =  `<font color="yellow">${ dayDifference }  ; 
711+         } 
611712    var  sunriseSunset  =  getSunriseSunset ( date ,  lat ,  lon ,  tz ) ; 
612713    var  isComputed  =  ( bsInfoData . year  <  Bsdata . BS_START_YEAR  ||  bsInfoData . year  >  Bsdata . BS_END_YEAR ) ; 
613714
@@ -623,13 +724,23 @@ Data-Driven BS Date: Not available (using astronomical calculation)
623724` ; 
624725    } 
625726
727+     var  lunarMonthInfo  =  getLunarMonthNameWithAdhik ( ahar ) ; 
728+        var  lunarMonthDisplay ; 
729+        if  ( lunarMonthInfo . isAdhik )  { 
730+            lunarMonthDisplay  =  "अधिक "  +  lunarMonthInfo . monthName  +  " "  +  paksha ; 
731+        }  else  { 
732+            lunarMonthDisplay  =  lunarMonthInfo . monthName  +  " "  +  paksha ; 
733+        } 
734+ 
626735    var  debugOutput  =  ` 
736+     <pre style="font-family: monospace; font-size: 9pt; color: white; white-space: pre; line-height: 1.2;"> 
627737${ dataDrivenInfo } 
628738Data-Driven BS Date: ${ bsInfoData  ? `${ toDevanagari ( bsInfoData . year ) } ${ bsInfoData . monthName } ${ toDevanagari ( bsInfoData . day ) }   : 'N/A' }  
629739Astronomical BS Date (Corrected): ${ toDevanagari ( bsInfoCalc . year ) } ${ bsInfoCalc . monthName } ${ toDevanagari ( bsInfoCalc . day ) }  
630- Day Difference: ${ dayDifference } ${ Math . abs ( dayDifference )  ===  1  ? 'day'  : 'days' }  
631- (Note: Positive = Astronomical date is behind; Negative = Astronomical date is ahead) 
632- 
740+ Day Difference: ${ dayDifferenceDisplay } ${ Math . abs ( dayDifference )  ===  1  ? 'day'  : 'days' }  
741+ <span style="margin: 0; color: yellow; padding: 0;">lunar Month(testing): ${ lunarMonthDisplay }  
742+ <span style="margin: 0; color: red; padding: 0;">Note: Positive = Astronomical date is behind; 
743+ Negative = Astronomical date is ahead)</span> 
633744Detailed Astronomical Calculation: 
634745gregorianDate: ${ Qt . formatDateTime ( date ,  "dddd, MMMM d, yyyy" ) }  
635746bsMonthIndex(0 based index): ${ bsInfoCalc . monthIndex }  
@@ -646,8 +757,9 @@ karana: ${karanaName} | index: ${karanaIdx}
646757karanaAngle: ${ ( 2  *  tithiVal ) . toFixed ( 4 ) }  
647758sunRashi: ${ rashis [ Math . floor ( sunLong  /  30 )  %  12 ] } ${ Math . floor ( sunLong  /  30 )  %  12  +  1 }  
648759moonRashi: ${ rashis [ Math . floor ( moonLong  /  30 )  %  12 ] } ${ Math . floor ( moonLong  /  30 )  %  12  +  1 }  
649- adhikaMasa: ${ calculateAdhikaMasa ( ahar ) }  
760+ adhikaMasa: ${ calculateAdhikaMasa ( ahar ) } (approximation)  
650761isComputed: ${ isComputed }  
762+     </pre> 
651763    ` . trim ( ) ; 
652764
653765    var  result  =  {  debug : debugOutput . replace ( / ^ \s * [ \r \n ] / gm,  "" )  } ; 
0 commit comments