我有一个在英国工作正常的时间流逝python脚本,它现在我住在法国它仍然在标准GMT时间计算。如何更改代码以便它代表我的实际位置?
import sys
import os
import time
import ephem
#find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '45.226691', '0.013133' #your lat long
sun.compute(home)
sunrise, sunset = (home.next_rising(sun),
home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
daylightminutes = (round(daylightminutes))
sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)
print "prog started at(home.date) = %s" %(home.date)
print "datetime = %s" % time.strftime("%Y/%-m/%-d %H:%M:%S")
print "sunrise = %s" %sunrise
print "sunset = %s" %sunset
print "daylight mins = %s" %(daylightminutes)
....
while ephem.now() <= sunrise:
time.sleep(1)
dostuff()
代码假设它在gmt时间是sunrize / sunset而不是我当地的时间,尽管知道我的实际纬度/经度。因此,目前在法国,我们比gmt领先2个小时,当然没有用。
如果没有世界时区边界的内置地图,PyEphem就无法将纬度/经度转换为时区,唉,事实并非如此。
但是,如果你设置了计算机操作系统,你的计算机操作系统会知道你的时区,PyEphem有一个“localtime()”函数,它会要求操作系统将时区用于打印时间: