问题描述
我需要从一个标准的阳历日期转换为朱利安天数。
I need to convert from a standard Gregorian date to a Julian day number.
我什么也没看见记录在C#中直接做到这一点,但我发现很多帖子(在谷歌搜索)建议使用的。
I've seen nothing documented in C# to do this directly, but I have found many posts (while Googling) suggesting the use of ToOADate.
在 ToOADate 文档并不表明这是一个有效的转换方法朱利安日期。
The documentation on ToOADate does not suggest this as a valid conversion method for Julian dates.
任何人都可以澄清,如果此功能将准确地进行转换,或者更适当的方法为DateTime转换为朱利安格式的字符串。
Can anyone clarify if this function will perform conversion accurately, or perhaps a more appropriate method to convert DateTime to a Julian formatted string.
这为我提供了预期数量时,反对的页
This provides me with the expected number when validated against Wikipedia's Julian Day page
public static long ConvertToJulian(DateTime Date)
{
int Month = Date.Month;
int Day = Date.Day;
int Year = Date.Year;
if (Month < 3)
{
Month = Month + 12;
Year = Year - 1;
}
long JulianDay = Day + (153 * Month - 457) / 5 + 365 * Year + (Year / 4) - (Year / 100) + (Year / 400) + 1721119;
return JulianDay;
}
然而,这是不被使用的幻数的理解。
However, this is without an understanding of the magic numbers being used.
感谢
参考文献:
推荐答案
OADate类似于朱利安日期,但使用不同的出发点(1899年12月30日主场迎战1月1日公元前4713年),和不同的新的一天' 点。朱利安日期考虑中午是新的一天的开始,OADates用现代定义,午夜。
OADate is similar to Julian Dates, but uses a different starting point (December 30, 1899 vs. January 1, 4713 BC), and a different 'new day' point. Julian Dates consider noon to be the beginning of a new day, OADates use the modern definition, midnight.
午夜的儒略历,1899年12月30日是2415018.5。此方法应该给你正确的价值观:
The Julian Date of midnight, December 30, 1899 is 2415018.5. This method should give you the proper values:
public static double ToJulianDate(this DateTime date)
{
return date.ToOADate() + 2415018.5;
}
至于算法:
-
如果(月3;)...
:为了让幻数工作的我们的权利,他们把2月的一年结束。 -
(153 *月 - 457)/ 10
:哇,这是一些严重的幻数。- 通常,天各月的数量是31 28 31 30 31 30 31 31 30 31 30 31,但在if语句的调整后,就变成31 30 31 30 31 31 30 31 30 31 31 28。或者,减去30,你最终以1 0 1 0 1 1 0 1 0 1 1 -2。他们做了分工整数空间创建1和0的模式。
- 重新写入浮点,这将是
(INT)(30.6 *月 - 91.4)
。 30.6的平均每月天数,不包括二月(30.63重复,要准确)。 91.4是天在3平均无月月几乎数。 (30.6 * 3为91.8)。 - 那么,让我们除去30,并只专注于用0.6天。如果我们乘它以月数,然后截断为整数,我们会得到0和1的模式。
- 0.6 * 0 = 0.0 - > 0
- 0.6 * 1 = 0.6 - > 0(0差)
- 0.6 * 2 = 1.2 - > 1(1差)
- 0.6 * 3 = 1.8 - > 1(0差)
- 0.6 * 4 = 2.4 - > 2(1差)
- 0.6 * 5 = 3.0 - > 3(1差)
- 0.6 * 6 = 3.6 - > 3(0差)
- 0.6 * 7 = 4.2 - > 4(1差)
- 0.6 * 8 = 4.8 - > 4(0差)
if (Month < 3) ...
: To make the magic numbers work our right, they're putting February at the 'end' of the year.(153 * Month - 457) / 5
: Wow, that's some serious magic numbers.- Normally, the number of days in each month is 31 28 31 30 31 30 31 31 30 31 30 31, but after that adjustment in the if statement, it becomes 31 30 31 30 31 31 30 31 30 31 31 28. Or, subtract 30 and you end up with 1 0 1 0 1 1 0 1 0 1 1 -2. They're creating that pattern of 1s and 0s by doing that division in integer space.
- Re-written to floating point, it would be
(int)(30.6 * Month - 91.4)
. 30.6 is the average number of days per month, excluding February (30.63 repeating, to be exact). 91.4 is almost the number of days in 3 average non-February months. (30.6 * 3 is 91.8). - So, let's remove the 30, and just focus on that 0.6 days. If we multiply it by the number of months, and then truncate to an integer, we'll get a pattern of 0s and 1s.
- 0.6 * 0 = 0.0 -> 0
- 0.6 * 1 = 0.6 -> 0 (difference of 0)
- 0.6 * 2 = 1.2 -> 1 (difference of 1)
- 0.6 * 3 = 1.8 -> 1 (difference of 0)
- 0.6 * 4 = 2.4 -> 2 (difference of 1)
- 0.6 * 5 = 3.0 -> 3 (difference of 1)
- 0.6 * 6 = 3.6 -> 3 (difference of 0)
- 0.6 * 7 = 4.2 -> 4 (difference of 1)
- 0.6 * 8 = 4.8 -> 4 (difference of 0)
这篇关于转换日期时间朱利安日期在C#(ToOADate安全吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!