通过 wiki 的简单数字脚本也不知道。请注意,这是使用 Python 3.6 编写的,所以我不确定它是否适用于 Python 2.7,但这也是一个老问题。
def julian_day(now):
"""
1. Get current values for year, month, and day
2. Same for time and make it a day fraction
3. Calculate the julian day number via https://en.wikipedia.org/wiki/Julian_day
4. Add the day fraction to the julian day number
"""
year = now.year
month = now.month
day = now.day
day_fraction = now.hour + now.minute / 60.0 + now.second / 3600.0 / 24.0
# The value 'march_on' will be 1 for January and February, and 0 for other months.
march_on = math.floor((14 - month) / 12)
year = year + 4800 - march_on
# And 'month' will be 0 for March and 11 for February. 0 - 11 months
month = month + 12 * march_on - 3
y_quarter = math.floor(year / 4)
jdn = day + math.floor((month * 153 + 2) / 5) + 365 * year + y_quarter
julian = year < 1582 or year == (1582 and month < 10) or (month == 10 and day < 15)
if julian:
reform = 32083 # might need adjusting so needs a test
else:
reform = math.floor(year / 100) + math.floor(year / 400) + 32030.1875 # fudged this
return jdn - reform + day_fraction
通常这只是为了自己尝试,因为最常见的算法给我带来了麻烦。这行得通,如果您四处搜索并使用它编写脚本,因为它有多种语言。但是这个在文档中有步骤来尽量保持简单。最大的决定是您要多久查找一次公历改革之前的日期。这就是为什么我还没有测试过它,而是继续玩它,因为它需要大量的按摩。 :-D 至少我认为它符合 PEP8,即使它不符合最佳实践。继续并对其进行 pylint。
您可以只使用 PyEphem 之类的源代码包或其他任何东西,但您仍然想知道它发生了什么,以便您可以编写自己的测试。我会为你链接PyEphem,但是有很多现成的包都有 Julian Day 计算。
如果您对这些类型的数字做了大量工作,最好的办法是获取一个常量列表,例如 J2000。
datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
datetime.datetime.toordinal() + 1721425 - 0.5 # not tested
# or even
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
如果您熟悉 datetime 库的功能,那么弄清楚这些并不难。只是为了好玩,您是否注意到 PyEphem 徽标?我怀疑它来自this
我看到的一个帖子似乎有效但没有测试是jiffyclub
现在这是使用日期时间对象计算两个值的更常见方法。
def jdn(dto):
"""
Given datetime object returns Julian Day Number
"""
year = dto.year
month = dto.month
day = dto.day
not_march = month < 3
if not_march:
year -= 1
month += 12
fr_y = math.floor(year / 100)
reform = 2 - fr_y + math.floor(fr_y / 4)
jjs = day + (
math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + reform - 1524)
if jjs < ITALY:
jjs -= reform
return jjs
# end jdn
def ajd(dto):
"""
Given datetime object returns Astronomical Julian Day.
Day is from midnight 00:00:00+00:00 with day fractional
value added.
"""
jdd = jdn(dto)
day_fraction = dto.hour / 24.0 + dto.minute / 1440.0 + dto.second / 86400.0
return jdd + day_fraction - 0.5
# end ajd
这可能不是 Python 中的最佳实践,但您确实问过如何计算它,而不仅仅是获取或提取它,尽管如果这是您想要的,那么这些问题最近已经得到解答。