问题描述
我正在尝试在 python 中创建一个朱利安约会,但遇到了重大困难.有没有比这更简单的了:
I'm trying to create a julian date in python and having major struggles. Is there nothing out there as simple as:
jul = juliandate(year,month,day,hour,minute,second)
在哪里 jul 会像 2457152.0(小数点随时间变化)?
where jul would be something like 2457152.0 (the decimal changing with the time)?
我试过 jdcal,但不知道如何添加时间组件(jdcal.gcal2jd() 只接受年、月和日).
I've tried jdcal, but can't figure out how to add the time component (jdcal.gcal2jd() only accepts year, month and day).
推荐答案
给你 - 一个带有日期时间和数学库的纯 Python 解决方案.
Here you go - a pure python solution with datetime and math library.
这是基于在此处找到的海军天文方程并使用他们自己的计算器进行验证:http://aa.usno.navy.mil/faq/docs/JD_Formula.php
This is based on the the Navy's Astronomical Equation found here and verified with their own calculator: http://aa.usno.navy.mil/faq/docs/JD_Formula.php
import datetime
import math
def get_julian_datetime(date):
"""
Convert a datetime object into julian float.
Args:
date: datetime-object of date in question
Returns: float - Julian calculated datetime.
Raises:
TypeError : Incorrect parameter type
ValueError: Date out of range of equation
"""
# Ensure correct format
if not isinstance(date, datetime.datetime):
raise TypeError('Invalid type for parameter "date" - expecting datetime')
elif date.year < 1801 or date.year > 2099:
raise ValueError('Datetime must be between year 1801 and 2099')
# Perform the calculation
julian_datetime = 367 * date.year - int((7 * (date.year + int((date.month + 9) / 12.0))) / 4.0) + int(
(275 * date.month) / 9.0) + date.day + 1721013.5 + (
date.hour + date.minute / 60.0 + date.second / math.pow(60,
2)) / 24.0 - 0.5 * math.copysign(
1, 100 * date.year + date.month - 190002.5) + 0.5
return julian_datetime
用法示例:
# Set the same example as the Naval site.
example_datetime = datetime.datetime(1877, 8, 11, 7, 30, 0)
print get_julian_datetime(example_datetime)
这篇关于在python中计算朱利安日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!