我目前正在使用输入(位置,时间)“城市,州,国家/地区”和“年/月-日时:分:秒”构造一个函数,并附带可以用于天文计算的恒星实时输出。目前,我正在通过Geopy传递位置信息以查找经度/纬度,然后使用timezonefinder查找确切的时区。
从那里,我使用pytz将本地时间转换为UTC,同时考虑了DST。我有一个内置数据库,可以使用转换后的时间和日期找到恒星时间。
我的问题是准确无误,我无法使用本地时间,我需要使用本地平均时间,这是根据与时区子午线相距多远而调整的本地时间。例如,如果您的城市的经度为-80,并且位于美国/纽约(东部时间),则实际上与太阳对齐的子午线为-75经度,这意味着相对于太阳,您实际上比东部时间早20分钟。但是由于DST以及时区并未在地图上真正被视为统一段的事实,我找不到一种可靠的方式来计算这种差异。
我能想到的最好的方法是将我的位置转换为长/纬度。找到该位置的时区,然后以某种方式将该时区隐藏到子午线上。
我将把代码发布给视觉效果更好的其他人。
from timezonefinder import TimezoneFinder
import geopy.geocoders, pytz, certifi, ssl, datetime, ephem, math
def position(city, state, country):
"""
:param city: String of city Ex. Chattannoga
:param state: String of state Ex. TN
:param country: String USA
:return: latitude and longitude
"""
ctx = ssl.create_default_context(cafile=certifi.where())
geopy.geocoders.options.default_ssl_context = ctx
geo_locator = geopy.geocoders.Nominatim(user_agent="my-application", scheme='http')
location = geo_locator.geocode(city + ' ' + state + ' ' + country)
return location.longitude, location.latitude
def timezone(longitude, latitude):
"""
:param longitude:
:param latitude:
:return: timezone
"""
tf = TimezoneFinder()
zone = (tf.certain_timezone_at(lng=longitude, lat=latitude))
return zone
# def localmeantime(longitude):
# zones = [-0, -15, -30, -45, -60, -75, -90, -105, -120, -135, -150, -165, -180]
# x = min(zones, key=lambda x: abs(x - longitude))
# print(x)
def localtoutc(time, timezo):
"""
:param time: Ex. "2001-2-3 10:11:12" String
:param timezo: Ex. America/New_York String
:return:
"""
local = pytz.timezone(timezo)
naive = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
return utc_dt
我能想到的另一种方法是找出该位置是否在DST中,然后通过计算经过了多少小时来找出子午线。每小时15度。
我想知道是否有更好的方法来获取本地平均时间,或者是否有一种方法可以更轻松地获得恒星实时时间。
答案 0 :(得分:0)
万一有人偶然发现此问题,我可以通过求出上述通用时间并减去经度x 4分钟以找到当地平均时间来解决我的问题。
def localmeantime(utc, longitude):
"""
:param utc: string Ex. '2008-12-2'
:param longitude: longitude
:return: Local Mean Time Timestamp
"""
lmt = utc + datetime.timedelta(seconds=round(4*60*longitude))
lmt = lmt.replace(tzinfo=None)
return lmt