Re: Julian date
Re: Julian date
- Subject: Re: Julian date
- From: Thomas Wetmore <email@hidden>
- Date: Thu, 07 Jan 2010 14:10:42 -0500
Here a couple methods that go to and from modified julian day numbers to <day, month, year> values. These algorithms are found in a number of standard sources for astronomical computing.
These are methods from a date class that I use. They only thing necessary to know about that class is that three of its instance variables are day, month and year. Making these methods work as an NSDate category should be straightforward.
Tom Wetmore
// Return the modified julian date of this day.
//---------------------------------------------------------------------------------------------
- (long) toJulian
{
NSInteger m = month;
NSInteger y = year;
if (m <= 2) {
m += 12;
--y;
}
NSInteger b;
if ((10000L*y + 100*m + day) <= 15821004L)
b = -2 + ((y + 4716)/4) - 1179; // Julian calendar
else
b = (y/400) - (y/100) + (y/4); // Gregorian calendar
return 365L*y - 679004L + b + (NSInteger)(30.6001*(m + 1)) + day;
}
// Return the day for a modified julian date.
//----------------------------------------------------------------------------------
+ (TWDay*) fromJulian: (long) mjd
{
long a = mjd + 2400001;
long b, c, d, e, f;
if (a < 2299161) {
b = 0;
c = a + 1524;
} else {
b = (long) ((a - 1867216.25)/36524.25);
c = a + b - (b/4) + 1525;
}
d = (long) ((c - 122.1)/365.25);
e = 365*d + d/4;
f = (long) ((c - e)/30.6001);
NSInteger day = (int) (c - e - (int) (30.6001*f));
NSInteger month = (int) (f - 1 - 12*(f/14));
NSInteger year = (int) (d - 4715 - (7 + month)/10);
return [[[TWDay alloc] initDay: day month: month year: year] autorelease];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden