Hi all.
The latest build 357 of the core PixInsight application comes with two new methods added to the Math JavaScript object. These methods allow fast and accurate conversions between julian day numbers and complex time representations.
The Julian Day Number (or Julian Day (JD) for short) is a time scale independent time measurement system consisting of a continuous count of days and fractions. The origin of the JD count is -4712 January 1.5. For that instant, we have JD=0.
Traditionally, the Julian Day begins at noon (12 hours). So all dates at 0 hours in any time scale correspond to Julian Day Numbers whose fractions are 0.5.
Our julian day number calculation routines implement original algorithms by Jean Meeus (Meeus, Jean. 1991, Astronomical Algorithms, Willmann-Bell, Inc., ch. 7). However, we have modified original Meeus' algorithms to allow for negative julian day numbers.
In our implementation, the Gregorian Calendar Reform is taken into account. This means that the day following 1582 October 4 (Julian Calendar) is 1582 October 15 (Gregorian Calendar). Refer to the original Meeus' book referenced above for more information about the calendar reform, which is of particular relevance to historical applications.
Of course, our routines follow the astronomical counting of years for B.C. years: the year before year 1 is the year 0, and the year preceding year 0 is year -1.
Static Methods
Number Math.complexTimeToJD( int year, int month, int day[, Number dayFrac=0] )
Returns the Julian Day Number corresponding to the specified time.
year
The year number in the Julian (<= 1582/10/04) or Gregorian Calendar (>= 1582/10/15). Negative years, along with the year zero, are B.C. years.
month
The month number, where 1=January and 12=December. Month numbers outside the nominal [1,12] range are fully supported, including negative months.
day
The integer day number. The nominal range is [1, 28|29|30|31] (the higher bound depending on the date's month, and on whether the date is in a leap year); however, our implementation fully supports arbitrary day numbers, including negative days.
dayFrac
The day fraction. Can be an arbitrary value expressed in days, although the nominal range is [0,1[. To calculate a day fraction from hours, minutes and seconds, use the simple relation:
dayFrac = (hour + (minute + seconds/60)/60)/24;
or, equivalently (although with a higher risk of roundoff errors):
dayFrac = hour/24 + minute/1440 + seconds/86400;
The returned value will be the Julian Day Number (JD) corresponding to the specified time point. The fractional part of the JD is zero at noon (12 hours), and 0.5 at zero hours.
Array Math.jdToComplexTime( Number jd )
Returns the complex time representation corresponding to a time point expressed as a Julian Day Number.
jd
The Julian Day Number for which the complex time representation is to be obtained.
Returns an array of complex time components: Element at array index 0 is the year, element 1 is the month in the range [1,12], element 2 is the day in the range [1,31], taking into account the number of days in the month (and also if the year is a leap year), and element 3 is the day fraction in the range [0,1[.
To obtain the hour, minute and seconds from the day fraction, one must be careful to avoid rounding problems. This example shows the correct procedure:
var T = Math.jdToComplexTime( 2451545.0 );
var year = T[0];
var month = T[1];
var day = T[2];
var dayf = T[3];
var h = 24*dayf; // hours with fraction
var hour = Math.trunc( h ); // integer hour in the range [0,23]
var m = 60*Math.frac( h ); // minutes with fraction
var minute = Math.trunc( m ); // integer minute in the range [0,59]
var seconds = 60*Math.frac( m ); // seconds with fraction
Usage Examples
Using Julian Day Numbers, it is trivial to calculate time intervals in days elapsed between two given time points. It's just a matter of subtracting the two JD values corresponding to the desired time points. For example:
var daysElapsed =
Math.complexTimeToJD( 2008, 3, 31.0 ) - Math.complexTimeToJD( 2007, 12, 16.0 );
Time intervals so calculated will be negative if the first time operand precedes the second time operand.
In a similar way, it is very easy to calculate the date that results after elapsing a number of days from a given date:
var jd1 = Math.complexTimeToJD( 2007, 12, 16.31 ) + 127;
var newDateAndTime = Math.jdToComplexTime( jd1 );
In the Meeus' book referenced at the beginning of this post you'll find many more interesting and useful calculations that can be made very easily by calculating the JD corresponding to a given date.