Code Monkey home page Code Monkey logo

Comments (4)

maateusx avatar maateusx commented on August 12, 2024

font: http://www.voidware.com/moon_phase.htm

`int moon_phase(int y, int m, int d)
{
//calculates the moon phase (0-7), accurate to 1 segment.
//0 = > new moon.
//4 => full moon.

int c,e;
double jd;
int b;

if (m < 3) {
    y--;
    m += 12;
}
++m;
c = 365.25*y;
e = 30.6*m;
jd = c+e+d-694039.09;  /* jd is total days elapsed */
jd /= 29.53;           /* divide by the moon cycle (29.53 days) */
b = jd;		   /* int(jd) -> b, take integer part of jd */
jd -= b;		   /* subtract integer part to leave fractional part of original jd */
b = jd*8 + 0.5;	   /* scale fraction from 0-8 and round by adding 0.5 */
b = b & 7;		   /* 0 and 8 are the same so turn 8 into 0 */
return b;

}
`

`int Moon_phase(int year,int month,int day) {
//Calculates the moon phase (0-7), accurate to 1 segment.
//0 = > new moon.
//4 => Full moon.

int g, e;

if (month == 1) --day;
else if (month == 2) day += 30;
else // m >= 3
{
    day += 28 + (month-2)*3059/100;

    // adjust for leap years
    if (!(year & 3)) ++day;
    if ((year%100) == 0) --day;
}

g = (year-1900)%19 + 1;
e = (11*g + 18) % 30;
if ((e == 25 && g > 11) || e == 24) e++;
return ((((e + day)*6+11)%177)/22 & 7);

}`

both formulae are simplified to work from 1900 to 2199 inclusive. however, ive discovered that they disagree. consider september 23, 2002. the second formula claims this a full moon, the first does not. the moon is not full on this night so the first seems more accurate.
here is a command line pc program that is more accurate than the above for reference.

from holidaysearch.

maateusx avatar maateusx commented on August 12, 2024

http://www.ben-daglish.net/moon.shtml

function Trig2(year,month,day) { n = Math.floor(12.37 * (year -1900 + ((1.0 * month - 0.5)/12.0))); RAD = 3.14159265/180.0; t = n / 1236.85; t2 = t * t; as = 359.2242 + 29.105356 * n; am = 306.0253 + 385.816918 * n + 0.010730 * t2; xtra = 0.75933 + 1.53058868 * n + ((1.178e-4) - (1.55e-7) * t) * t2; xtra += (0.1734 - 3.93e-4 * t) * Math.sin(RAD * as) - 0.4068 * Math.sin(RAD * am); i = (xtra > 0.0 ? Math.floor(xtra) : Math.ceil(xtra - 1.0)); j1 = julday(year,month,day); jd = (2415020 + 28 * n) + i; return (j1-jd + 30)%30; }

from holidaysearch.

maateusx avatar maateusx commented on August 12, 2024

https://stellafane.org/misc/equinox.html

`

<title>Stellafane Equinox & Solstice Calculator</title> <script type="text/javascript" src="../common/STMcommon.js"></script> <script type="text/javascript"> var firstTime = true; // Initialization flag

//-----Date Input Functions--------------------------------------------------------
function setYear( y ) { document.frm.year.value = String( y ); }
function getYear() { return Number( document.frm.year.value ); }
function getTZ() { // this handles the time zone radio buttons
for( var n=0; n < document.frm.TZ.length; n++ ) {
if( document.frm.TZ[n].checked) { return document.frm.TZ[n].value; }
}
return "(None)";
}
function nextYear() { setYear( getYear() + 1); calc(); }
function prevYear() { setYear( getYear() - 1); calc(); }
function thisYear() { setCurrentYear(); calc(); }
function setCurrentYear() {
var now = new Date();
setYear( now.getFullYear() );
document.frm.year.value = now.getFullYear();
}
function valiDate(){
// Validates that the input year is a valid integer between -4712 to +3500
// Returns true if valid, false if not
var i,j;
if (document.frm.year.value === "") {
alert("Please enter a value for the "Year" field.");
document.frm.year.focus(); return false;
}
var checkOK = "0123456789-+"; // valid character list
var checkStr = document.frm.year.value;
var allValid = true;
for ( i = 0; i < checkStr.length; i++) {
for (j = 0; j < checkOK.length; j++)
{ if (checkStr.charAt(i) == checkOK.charAt(j)) { break; } }
if (j == checkOK.length) { allValid = false; break; }
}
if ( !allValid ) {
alert("Please enter only digit characters and "+" or "-" in the "Year" field.");
document.frm.year.focus(); return false;
}
if ( ( getYear() < 1000 ) || ( getYear() > 3000 ) ) {
alert("Please enter a value greater than or equal to "1000" and less than or equal to "3000" in the "Year" field.");
document.frm.year.focus(); return false;
}
return true;
}
//-----Output Functions------------------------------------------------------------
function clearAll() { for( var i=1; i<=4; i++ ) { writeN( i, "" ); } }
function writeN( n, str ) {
switch( n ) {
case 1: document.frm.out1.value = str; break;
case 2: document.frm.out2.value = str; break;
case 3: document.frm.out3.value = str; break;
case 4: document.frm.out4.value = str; break;
}
}

//-----Utility Funtions------------------------------------------------------------
function INT ( n ) { return Math.floor(n); } // Emulates BASIC's INT Funtion
function POW2( n ) { return Math.pow(n,2); } // Square a number
function POW3( n ) { return Math.pow(n,3); } // Cube a number
function POW4( n ) { return Math.pow(n,4); } // Number to the 4th power
function COS( deg ) { // Cosine function with degrees as input
return Math.cos( deg * Math.PI/180 );
}
function noSubmit() { return false; } //Prevent form submission

//-----Main Function---------------------------------------------------------------
function calc() {
if ( firstTime ) { setCurrentYear(); firstTime = false; }
clearAll();
if (!valiDate()) { return; }
var year = getYear();
//Main Calculations started here
for( var i=1; i<=4; i++ ) { // Loop over 4 events: 1=AE, 2=SS, 3-VE, 4=WS
calcEquiSol( i, year ); // This routine calculates and displays a single event
}
} // End calc

//-----Calculate and Display a single event for a single year (Either a Equiniox or Solstice)
// Meeus Astronmical Algorithms Chapter 27
function calcEquiSol( i, year ) {
var k = i - 1;
var str;
var JDE0 = calcInitial( k, year); // Initial estimate of date of event
var T = ( JDE0 - 2451545.0) / 36525;
var W = 35999.373T - 2.47;
var dL = 1 + 0.0334
COS(W) + 0.0007COS(2W);
var S = periodic24( T );
var JDE = JDE0 + ( (0.00001*S) / dL ); // This is the answer in Julian Emphemeris Days
var TDT = fromJDtoUTC( JDE ); // Convert Julian Days to TDT in a Date Object
var UTC = fromTDTtoUTC( TDT ); // Correct TDT to UTC, both as Date Objects
switch ( getTZ() ) {
case "LCL": str = UTC.toString() + "\n"; break; //Convert to Local time string
case "UTC": str = UTC.toUTCString() + "\n"; break; //Convert to UTC time string
case "DYN": str = TDT.toUTCString() + "\n"; //Convert to Dynamical Time String
str = str.replace( /UTC/g, "TDT" ); break; // Change UTC to TDT in this output string
}
writeN( i, str ); // Output date & time of event in Local Time
} // End calcEquiSol

//-----Calcualte an initial guess as the JD of the Equinox or Solstice of a Given Year
// Meeus Astronmical Algorithms Chapter 27
function calcInitial( k, year ) { // Valid for years 1000 to 3000
var JDE0=0, Y=(year-2000)/1000;
switch( k ) {
case 0: JDE0 = 2451623.80984 + 365242.37404Y + 0.05169POW2(Y) - 0.00411POW3(Y) - 0.00057POW4(Y); break;
case 1: JDE0 = 2451716.56767 + 365241.62603Y + 0.00325POW2(Y) + 0.00888POW3(Y) - 0.00030POW4(Y); break;
case 2: JDE0 = 2451810.21715 + 365242.01767Y - 0.11575POW2(Y) + 0.00337POW3(Y) + 0.00078POW4(Y); break;
case 3: JDE0 = 2451900.05952 + 365242.74049Y - 0.06223POW2(Y) - 0.00823POW3(Y) + 0.00032POW4(Y); break;
}
return JDE0;
} // End calcInitial

//-----Calculate 24 Periodic Terms----------------------------------------------------
// Meeus Astronmical Algorithms Chapter 27
function periodic24( T ) {
var A = new Array(485,203,199,182,156,136,77,74,70,58,52,50,45,44,29,18,17,16,14,12,12,12,9,8);
var B = new Array(324.96,337.23,342.08,27.85,73.14,171.52,222.54,296.72,243.58,119.81,297.17,21.02,
247.54,325.15,60.93,155.12,288.79,198.04,199.76,95.39,287.11,320.81,227.73,15.45);
var C = new Array(1934.136,32964.467,20.186,445267.112,45036.886,22518.443,
65928.934,3034.906,9037.513,33718.147,150.678,2281.226,
29929.562,31555.956,4443.417,67555.328,4562.452,62894.029,
31436.921,14577.848,31931.756,34777.259,1222.114,16859.074);
var S = 0;
for( var i=0; i<24; i++ ) { S += A[i]*COS( B[i] + (C[i]*T) ); }
return S;
}

//-----Correct TDT to UTC----------------------------------------------------------------
function fromTDTtoUTC( tobj ) {
// from Meeus Astronmical Algroithms Chapter 10
// Correction lookup table has entry for every even year between TBLfirst and TBLlast
var TBLfirst = 1620, TBLlast = 2002; // Range of years in lookup table
var TBL = new Array( // Corrections in Seconds
/1620/ 121,112,103, 95, 88, 82, 77, 72, 68, 63, 60, 56, 53, 51, 48, 46, 44, 42, 40, 38,
/1660/ 35, 33, 31, 29, 26, 24, 22, 20, 18, 16, 14, 12, 11, 10, 9, 8, 7, 7, 7, 7,
/1700/ 7, 7, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
/1740/ 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16,
/1780/ 16, 16, 16, 16, 16, 16, 15, 15, 14, 13,
/1800/ 13.1, 12.5, 12.2, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 11.9, 11.6, 11.0, 10.2, 9.2, 8.2,
/1830/ 7.1, 6.2, 5.6, 5.4, 5.3, 5.4, 5.6, 5.9, 6.2, 6.5, 6.8, 7.1, 7.3, 7.5, 7.6,
/1860/ 7.7, 7.3, 6.2, 5.2, 2.7, 1.4, -1.2, -2.8, -3.8, -4.8, -5.5, -5.3, -5.6, -5.7, -5.9,
/1890/ -6.0, -6.3, -6.5, -6.2, -4.7, -2.8, -0.1, 2.6, 5.3, 7.7, 10.4, 13.3, 16.0, 18.2, 20.2,
/1920/ 21.1, 22.4, 23.5, 23.8, 24.3, 24.0, 23.9, 23.9, 23.7, 24.0, 24.3, 25.3, 26.2, 27.3, 28.2,
/1950/ 29.1, 30.0, 30.7, 31.4, 32.2, 33.1, 34.0, 35.0, 36.5, 38.3, 40.2, 42.2, 44.5, 46.5, 48.5,
/1980/ 50.5, 52.5, 53.8, 54.9, 55.8, 56.9, 58.3, 60.0, 61.6, 63.0, 63.8, 64.3); /2002 last entry/
// Values for Delta T for 2000 thru 2002 from NASA
var deltaT = 0; // deltaT = TDT - UTC (in Seconds)
var Year = tobj.getUTCFullYear();
var t = (Year - 2000) / 100; // Centuries from the epoch 2000.0

if ( Year >= TBLfirst && Year <= TBLlast ) { // Find correction in table
	if (Year%2) { // Odd year - interpolate
		deltaT = ( TBL[(Year-TBLfirst-1)/2] + TBL[(Year-TBLfirst+1)/2] ) / 2;
	} else { // Even year - direct table lookup
		deltaT = TBL[(Year-TBLfirst)/2];
	}
} else if( Year < 948) { 
	deltaT = 2177 + 497*t + 44.1*POW2(t);
} else if( Year >=948) {
	deltaT =  102 + 102*t + 25.3*POW2(t);
	if (Year>=2000 && Year <=2100) { // Special correction to avoid discontinurity in 2000
		deltaT += 0.37 * ( Year - 2100 );
	}
} else { alert("Error: TDT to UTC correction not computed"); }
return( new Date( tobj.getTime() - (deltaT*1000) ) ); // JavaScript native time is in milliseonds

} // End fromTDTtoUTC

//-----Julian Date to UTC Date Object----------------------------------------------------
// Meeus Astronmical Algorithms Chapter 7
function fromJDtoUTC( JD ){
// JD = Julian Date, possible with fractional days
// Output is a JavaScript UTC Date Object
var A, alpha;
var Z = INT( JD + 0.5 ); // Integer JD's
var F = (JD + 0.5) - Z; // Fractional JD's
if (Z < 2299161) { A = Z; }
else {
alpha = INT( (Z-1867216.25) / 36524.25 );
A = Z + 1 + alpha - INT( alpha / 4 );
}
var B = A + 1524;
var C = INT( (B-122.1) / 365.25 );
var D = INT( 365.25C );
var E = INT( ( B-D )/30.6001 );
var DT = B - D - INT(30.6001
E) + F; // Day of Month with decimals for time
var Mon = E - (E<13.5?1:13); // Month Number
var Yr = C - (Mon>2.5?4716:4715); // Year
var Day = INT( DT ); // Day of Month without decimals for time
var H = 24*(DT - Day); // Hours and fractional hours
var Hr = INT( H ); // Integer Hours
var M = 60*(H - Hr); // Minutes and fractional minutes
var Min = INT( M ); // Integer Minutes
var Sec = INT( 60*(M-Min) ); // Integer Seconds (Milliseconds discarded)
//Create and set a JavaScript Date Object and return it
var theDate = new Date(0);
theDate.setUTCFullYear(Yr, Mon-1, Day);
theDate.setUTCHours(Hr, Min, Sec);
return( theDate );
} //End fromJDtoUTC
</script>

<style type="text/css"> div.result { margin-top: 15px; background-color: white; border: 1px gray solid; border-radius: 20px; box-shadow: 3px 3px 5px 0px gray; text-align: center; } div.result span { font-size: large; font-weight:bold; } div.result input { width: 500px; margin-bottom: 10px; font-family: Verdana; font-size: 10pt; font-weight: bold; text-align: center; border: none; } </style> <script> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-32536478-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>

<div class="STMbody">
	<header>
		<a href="../index.html" class="glow">
		<img id="STMlm" src="../common/littleman-pink-shdw-100x139.jpg" width="100" height="139" alt="Stellafane 'Little Man' - click for Stellafane Home Page" 
				title="Click to go to the Stellafane Home Page &reg;Stellafane Little Man"
				onMouseOver="this.className += ' pinkglow';" onMouseOut="this.className = this.className.replace('pinkglow', '');"></a>
		<img id="STMhdr" class="hdr2" alt="Page Header" width="640" height="140" src="topic_images/EquinoxHeader.jpg">
	</header>
	
	<div id="STMcontent"><!-- Begin Content Area -->
		<h1 class="PageTitle">Equinox &amp; Solstice Calculator</h1>

		<p>This page will calculate when the Equinoxes and Solstices are for a given year. Type in a year or use the 
		buttons to step forward and backward year-by-year:</p>

		<div class="bxrnd bxshdw " style="padding:10px; background-color: #FFE0FF; margin: auto; width:530px;">
			<form name="frm" onsubmit="noSubmit();">
				<table style="margin:auto;">
					<tr>
						<td class="R VM" style="height: 40px; width: 40%;">Valid Years: 1000-3000</td>
						<td class="C" style="height: 40px; width: 20%"><b>
							<input name="year" size="5" value="2006" onchange="calc();" style="font-family: Courier New; font-size: 14pt; font-weight: bold; text-align: center"></b></td>
						<td class="L VM" style="height: 40px; width: 40%"><input name="calcBtn" onclick="calc();" type="button" value="Calculate"></td>
					</tr>
					<tr>
						<td class="R"><input type="button" value="Previous Year" name="prev" onclick="prevYear();"></td>
						<td class="C"><input type="button" value="Now" name="thisYearBtn" onclick="thisYear();"></td>
						<td class="L"><input type="button" value="Next Year" name="next" onclick="nextYear();"></td>
					</tr>
					<tr>
						<td class="C" colspan="3">Calculated Time:&nbsp;
						<input type="radio" name="TZ" value="LCL" onclick="calc();" checked="checked">Local&nbsp;
						<input type="radio" name="TZ" value="UTC" onclick="calc();">UT&nbsp;
						<input type="radio" name="TZ" value="DYN" onclick="calc();">TDT </td>
					</tr>
				</table>
			<div class="result">
				<span>March Equinox</span>
				<input type="text" name="out1" value="?" readonly="readonly">
			</div>
			<div class="result">
				<span>June Solstice</span>
				<input type="text" name="out2" value="?" readonly="readonly">
			</div>
			<div class="result">
				<span>September Equinox</span>
				<input type="text" name="out3" value="?" readonly="readonly">				
			</div>
			<div class="result">
				<span>December Solstice</span>
				<input type="text" name="out4" value="?" readonly="readonly">
			</div>
			</form>
		</div>

		<p class="C">Also see our <b><a href="../observing/moon_phase.html">Moon Phase Calculator</a></b></p>

		<p><strong>TIME ACCURACY:</strong> <i>The calculated times are in </i>Terrestrial Dynamical Time (TDT <i>or
		</i>TT)<i>, a replacement for </i>Ephemeris Times (ET). <i>Meeus claims the error is less than one minute for 
		the years 1951-2050 for these calculations (the errors increase as you move farther away from these dates).
		</i></p>

		<p><b>CIVIL TIME: </b>TDT<i> is a uniform time used for astronomical calculations. Civil time, such as UTC (popularly 
		known as GMT, but technically incorrect) or your local time, is corrected for the non-linear changes in the 
		rotation of the earth. This is done accurately only through observations. This web page corrects the the calculated 
		TDT to civil time using a table of observations from 1620 thru 2002. Time corrections outside this range are 
		estimated from predictive equations. See Meeus&#39; book for additional details. </i></p>

		<p><b>LOCAL TIME: </b><i>Local time zone conversion is done by the JavaScript built-in runtime functions from 
		the corrected UTC (including daylight or summer time rules), based on information it gets from <b>your</b> computer&#39;s 
		environment settings. If the local time is in the wrong time zone for you, you need to check and adjust these 
		settings on your computer, there is nothing in this web page or the code behind it to change this. On Windows 
		machines, right click on the clock in the task bar and select &quot;Adjust Date/Time&quot;, and then click the &quot;Time Zone&quot; 
		tab.</i></p>

		<p><strong>CREDITS:</strong> <i>The algorithms and correction tables for this computation come directly from 
		the book </i><b>Astronomical Algorithms Second Edition </b><i>by Jean Meeus, ©1998, published by <b>
		<a href="http://www.willbell.com">Willmann-Bell</a></b>, Inc., Richmond, VA, ISBN
		0-943396-61-1. They were coded in JavaScript and built into this web page by the web page author, Ken 
		Slater.</i></p>

		<p><strong>CAUTION: </strong><em>While the output has been spot checked for correctness against known tables, 
		the code has not been thoroughly tested and could contain errors. The underlying algorithms are not exact and 
		times could vary by several minutes. Consult the book credited above for detailed information. </em><i>Note 
		also that the day-of-week will not be correct when the date is in the Julian calendar (earlier than 1582-Oct-15).</i></p>
		
	</div><!-- End Content Area -->
	<footer>
		<!-- Begin footer.php - Stellafane.org common footer file $rps:"../" -->

Page last revised on Monday, 2019-Apr-01 17:30:22 -0400

`

from holidaysearch.

maateusx avatar maateusx commented on August 12, 2024

Solstices and Equinoxes algorithm

from holidaysearch.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.