Python Ephem/日期时间计算

发布于 2024-08-27 12:06:06 字数 1595 浏览 7 评论 0原文

输出应将第一个日期处理为“白天”,将第二个日期处理为“夜晚”。我已经玩了几个小时了,不明白我做错了什么。有什么想法吗?

编辑 我认为问题是由于我的日期比较实现

输出:

$ 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')

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

Output:

$ 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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-09-03 12:06:06

o.date 总是在 o.previous_settings 和 o.next_rising 之间;),所以你可以这样检查:

if o.previous_rising(ephem.Sun()) > o.previous_setting(ephem.Sun()):
  return "day"
elif:
  return "night"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文