Orbiter MJD to Year Month Day

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,868
Reaction score
4
Points
0
Location
San Diego
I would have thought this would be a simple question but searches have come up empty.

How do I convert Orbiter's sim MJD to UTC date/time.
 
There are also online tools to do this but I'm amazed at the amount of people who ignore the utils directory. It's very useful! :lol:
 
I was hoping for an actual equation or function that I could call from within a module.

---------- Post added at 09:28 PM ---------- Previous post was at 09:24 PM ----------

Crap I feel sheepish :embarrassed:

About 30 seconds after posting I found the following SDK sample...

Code:
VECTOR3 CalcSimDate(void)
{
	double MJD = oapiGetSimMJD();
	int JD = MJD + 2400000.5;
	int j = JD + 32044;
	int g = j / 146097; 
	int dg = j % 146097;
	int c = (dg / 36524 + 1) * 3 / 4; 
	int dc = dg - c * 36524;
	int b = dc / 1461; 
	int db = dc % 1461;
	int a = (db / 365 + 1) * 3 / 4; 
	int da = db - a * 365;
	int y = g * 400 + c * 100 + b * 4 + a;	//note: this is the integer number of full years elapsed since March 1, 4801 BC at 00:00 UTC);
	int m = (da * 5 + 308) / 153 - 2;		//note: this is the integer number of full months elapsed since the last March 1 at 00:00 UTC);
	int d = da - (m + 4) * 153 / 5 + 122;	//note: this is the number of days elapsed since day 1 of the month at 00:00 UTC, including fractions of one day);

	int Year = y - 4800 + (m + 2) / 12; 
	int Month = (m + 2) % 12 + 1; 
	int Day = d + 1;

	VECTOR3 output = {Day, Month, Year};
	return output;
}

Its still signifigantl more complex than I expected.
 
It might be insignificant for your purposes, but Orbiter's clock does not track UTC, but rather TDB (Barycentric Dynamical Time). To get UTC you need to account for leap seconds.

Currently: UTC = TDB - 67.184 s

For more technical details, see here: http://www.orbiter-forum.com/showthread.php?t=8855. Note that the reason that the above formula differs from the one shown in that thread is due to the leap second that occured on 2012-06-30.
 
Here is the code used by date.exe to convert MJD to TDB. I can't remember where this has come from originally. It looks slightly different from the code posted above (whose origin I also can't recall), but may be equivalent.
Code:
struct tm *mjddate (double mjd)
{
	static struct tm date;
	double ijd, c, e, h;
	int a, b, f;

	h = 24.0 * modf (mjd, &ijd);
	if (ijd < -100840) {
		c = ijd + 2401525.0;
	} else {
		b = (int)((ijd + 532784.75) / 36524.25);
		c = ijd + 2401526.0 + (b - b/4);
	}
	a = (int)((c-122.1)/365.25);
	e = 365.0 * a + a/4;
	f = (int)((c-e)/30.6001);
	date.tm_wday  = ((int)mjd + 3) % 7;
	date.tm_mday  = (int)(c-e+0.5) - (int)(30.6001*f);
	date.tm_mon   = f-1 - 12 * (f/14);
	date.tm_year  = a-4715 - ((7 + date.tm_mon)/10) - 1900;
	date.tm_hour  = (int)h;
	date.tm_min   = (int)(h = 60.0 * (h - date.tm_hour));
	date.tm_sec   = (int)(h = 60.0 * (h - date.tm_min));
	date.tm_isdst = 0;
	return &date;
}
 
Back
Top