我正在尝试实现两个功能,1)将日期转换为儒略日数和 2)将儒略日数转换回日期(年、月和日)。我的代码返回的结果是关闭了一天。我不熟悉算法的工作原理,但我可以看到,如果我采用Math.Ceiling朱利安数,它可以工作,但我不确定这是修复代码的最佳方法。任何帮助将不胜感激。谢谢你。
public class Program
{
/// <summary>
/// Converts year, month and day to a Julian number
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="day"></param>
/// <returns></returns>
public static decimal JulianNumber(int year, int month, int day)
{
decimal a, b, c, e, f;
if (month == 1 || month == 2)
{
year -= 1;
month += 12;
}
a = Math.Truncate((decimal) year / 100);
b = Math.Truncate(a / 4);
c = 2 - a + b;
e = Math.Truncate((365.25m * (year + 4716)));
f = Math.Truncate((30.6001m * (month + 1)));
return (c + day + e + f - 1524.5m);
// return Math.Ceiling(c + day + e + f - 1524.5m);
}
/// <summary>
/// Converts Julian number to year, month and day
/// </summary>
/// <param name="julianNumber"></param>
/// <returns></returns>
public static (int year, int month, int day) GregorianDate(decimal julianNumber)
{
int l, n, i, j, k;
l = (int)julianNumber + 68569;
n = 4 * l / 146097;
l = l - (146097 * n + 3) / 4;
i = 4000 * (l + 1) / 1461001;
l = l - 1461 * i / 4 + 31;
j = 80 * l / 2447;
k = l - 2447 * j / 80;
l = j / 11;
j = j + 2 - 12 * l;
i = 100 * (n - 49) + i + l;
return (i, j, k);
}
public static void Main(string[] args)
{
var (year1, month1, day1) = (2010, 1, 2);
var (year2, month2, day2) = GregorianDate(JulianNumber(year1, month1, day1));
Console.WriteLine(year1 == year2); // True
Console.WriteLine(month1 == month2); // True
Console.WriteLine(day1 == day2); // False!
}
}
慕莱坞森
相关分类