Python Ephem / Datetime计算 [英] Python Ephem / Datetime calculation
本文介绍了Python Ephem / Datetime计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输出应将第一个日期作为日,第二个日期作为夜。我已经玩了几个小时了,不知道我做错了什么。任何想法?
the output should process the first date as "day" and second as "night". I've been playing with this for a few hours now and can't figure out what I'm doing wrong. Any ideas?
修改
我假设问题是由于我的日期比较实现
Edit I assume that the problem is due to my date comparison implementation
输出:
$ python time_of_day.py
* should be day:
event date: 2010/4/6 16:00:59
prev rising: 2010/4/6 09:24:24
prev setting: 2010/4/5 23:33:03
next rise: 2010/4/7 09:22:27
next set: 2010/4/6 23:34:27
day
* should be night:
event date: 2010/4/6 00:01:00
prev rising: 2010/4/5 09:26:22
prev setting: 2010/4/5 23:33:03
next rise: 2010/4/6 09:24:24
next set: 2010/4/6 23:34:27
day
time_of_day.py
import datetime
import ephem # install from http://pypi.python.org/pypi/pyephem/
#event_time is just a date time corresponding to an sql timestamp
def type_of_light(latitude, longitude, event_time, utc_time, horizon):
o = ephem.Observer()
o.lat, o.long, o.date, o.horizon = latitude, longitude, event_time, horizon
print "event date ", o.date
print "prev rising: ", o.previous_rising(ephem.Sun())
print "prev setting: ", o.previous_setting(ephem.Sun())
print "next rise: ", o.next_rising(ephem.Sun())
print "next set: ", o.next_setting(ephem.Sun())
if o.previous_rising(ephem.Sun()) <= o.date <= o.next_setting(ephem.Sun()):
return "day"
elif o.previous_setting(ephem.Sun()) <= o.date <= o.next_rising(ephem.Sun()):
return "night"
else:
return "error"
print "should be day: ", type_of_light('45.959','-66.6405','2010/4/6 16:01','-4', '-6')
print "should be night: ", type_of_light('45.959','-66.6405','2010/4/6 00:01','-4', '-6')
推荐答案
o.date将始终在o.prev之间ious_settings和o.next_rising;),所以你可以这样检查:
o.date will always be between o.previous_settings and o.next_rising ;), so you can check it this way:
if o.previous_rising(ephem.Sun()) > o.previous_setting(ephem.Sun()):
return "day"
elif:
return "night"
这篇关于Python Ephem / Datetime计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文