edu*_*dub 4 julian-date python-2.7
我正在尝试在 python 中创建一个朱利安约会并且遇到了重大的困难。有没有比这更简单的了:
jul = juliandate(year,month,day,hour,minute,second)
Run Code Online (Sandbox Code Playgroud)
其中 jul 将类似于 2457152.0(小数点随时间变化)?
我试过 jdcal,但不知道如何添加时间组件(jdcal.gcal2jd() 只接受年、月和日)。
小智 5
不是纯 Python 解决方案,但您可以在内存数据库中使用 SQLite,它具有以下julianday()
功能:
import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]
Run Code Online (Sandbox Code Playgroud)
返回: 2457754.5
给你 - 一个带有日期时间和数学库的纯 python 解决方案。
这是基于此处找到的海军天文方程并用他们自己的计算器验证的: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
Run Code Online (Sandbox Code Playgroud)
使用示例:
# Set the same example as the Naval site.
example_datetime = datetime.datetime(1877, 8, 11, 7, 30, 0)
print get_julian_datetime(example_datetime)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22992 次 |
最近记录: |