虽然方法
public static double ToJulianDate(this DateTime date) { return date.ToOADate() + 2415018.5; }
作品为现代的日期,它有显著的缺点。
Julian日期定义为负日期 - 即BCE(在共同时代之前)日期,并且在天文计算中很常见。您无法构造年份小于0的DateTime对象,因此无法使用上述方法为BCE日期计算Julian日期。
1582年的公历改革在10月4日至15日的日历中出现了11天的漏洞。这些日期没有在Julian日历或格里历日历中定义,但DateTime接受它们作为参数。此外,使用上述方法不会为任何儒略日期返回正确的值。使用System.Globalization.JulianCalendar.ToDateTime()或将JulianCalendar时代传递给DateTime构造函数的实验在1582年10月5日之前的所有日期仍然会产生不正确的结果。
以下例程改编自Jean Meeus的“天文算法”,从Julian日历上的零时间-4月12日开始的所有日期返回正确的结果。如果传递了无效日期,他们还会抛出ArgumentOutOfRangeException。
public class JulianDate
{
public static bool isJulianDate(int year, int month, int day)
{
// All dates prior to 1582 are in the Julian calendar
if (year < 1582)
return true;
// All dates after 1582 are in the Gregorian calendar
else if (year > 1582)
return false;
else
{
// If 1582, check before October 4 (Julian) or after October 15 (Gregorian)
if (month < 10)
return true;
else if (month > 10)
return false;
else
{
if (day < 5)
return true;
else if (day > 14)
return false;
else
// Any date in the range 10/5/1582 to 10/14/1582 is invalid
throw new ArgumentOutOfRangeException(
"This date is not valid as it does not exist in either the Julian or the Gregorian calendars.");
}
}
}
static private double DateToJD(int year, int month, int day, int hour, int minute, int second, int millisecond)
{
// Determine correct calendar based on date
bool JulianCalendar = isJulianDate(year, month, day);
int M = month > 2 ? month : month + 12;
int Y = month > 2 ? year : year - 1;
double D = day + hour/24.0 + minute/1440.0 + (second + millisecond/1000.0)/86400.0;
int B = JulianCalendar ? 0 : 2 - Y/100 + Y/100/4;
return (int) (365.25*(Y + 4716)) + (int) (30.6001*(M + 1)) + D + B - 1524.5;
}
static public double JD(int year, int month, int day, int hour, int minute, int second, int millisecond)
{
return DateToJD(year, month, day, hour, minute, second, millisecond);
}
static public double JD(DateTime date)
{
return DateToJD(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond);
}
}
谢谢你这个详细的解释。 – cweston 2011-03-10 14:21:17
+1。真棒帖子。 – BSalita 2012-02-06 11:27:58
对不起,我不喜欢数学,但你做得很好。谢谢 – fiberOptics 2012-02-22 02:37:23