本文整理汇总了Python中astral.Location方法的典型用法代码示例。如果您正苦于以下问题:Python astral.Location方法的具体用法?Python astral.Location怎么用?Python astral.Location使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astral
的用法示例。
在下文中一共展示了astral.Location方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculate_sunrise
# 需要导入模块: import astral [as 别名]
# 或者: from astral import Location [as 别名]
def calculate_sunrise(year_to_simulate, longitude, latitude):
"""
Calculate the hour of sunrise for a given year, longitude and latitude. Returns an array
of hours.
"""
# get the time zone name
tf = TimezoneFinder()
time_zone = tf.timezone_at(lng=longitude, lat=latitude)
# define the city_name
location = Location()
location.name = 'name'
location.region = 'region'
location.latitude = latitude
location.longitude = longitude
location.timezone = time_zone
location.elevation = 0
sunrise = []
for day in range(1, 366): # Calculated according to NOAA website
dt = datetime.datetime(year_to_simulate, 1, 1) + datetime.timedelta(day - 1)
dt = pytz.timezone(time_zone).localize(dt)
sun = location.sun(dt)
sunrise.append(sun['sunrise'].hour)
print('complete calculating sunrise')
return sunrise
示例2: get_sunrise_sunset
# 需要导入模块: import astral [as 别名]
# 或者: from astral import Location [as 别名]
def get_sunrise_sunset(self, date = None):
if self.data['sunrise_time'] is not None and self.data['sunset_time'] is not None:
if date is None:
date = dt_now(self.data['timezone'])
sunrise = date.replace(hour=int(self.data['sunrise_time'].strftime("%H")), minute=int(self.data['sunrise_time'].strftime("%M")), second=int(self.data['sunrise_time'].strftime("%S")), microsecond=int(self.data['sunrise_time'].strftime("%f")))
sunset = date.replace(hour=int(self.data['sunset_time'].strftime("%H")), minute=int(self.data['sunset_time'].strftime("%M")), second=int(self.data['sunset_time'].strftime("%S")), microsecond=int(self.data['sunset_time'].strftime("%f")))
solar_noon = sunrise + (sunset - sunrise)/2
solar_midnight = sunset + ((sunrise + timedelta(days=1)) - sunset)/2
else:
import astral
location = astral.Location()
location.name = 'name'
location.region = 'region'
location.latitude = self.data['latitude']
location.longitude = self.data['longitude']
location.elevation = self.data['elevation']
_LOGGER.debug("Astral location: " + str(location))
if self.data['sunrise_time'] is not None:
if date is None:
date = dt_now(self.data['timezone'])
sunrise = date.replace(hour=int(self.data['sunrise_time'].strftime("%H")), minute=int(self.data['sunrise_time'].strftime("%M")), second=int(self.data['sunrise_time'].strftime("%S")), microsecond=int(self.data['sunrise_time'].strftime("%f")))
else:
sunrise = location.sunrise(date)
if self.data['sunset_time'] is not None:
if date is None:
date = dt_now(self.data['timezone'])
sunset = date.replace(hour=int(self.data['sunset_time'].strftime("%H")), minute=int(self.data['sunset_time'].strftime("%M")), second=int(self.data['sunset_time'].strftime("%S")), microsecond=int(self.data['sunset_time'].strftime("%f")))
else:
sunset = location.sunset(date)
solar_noon = location.solar_noon(date)
solar_midnight = location.solar_midnight(date)
if self.data['sunrise_offset'] is not None:
sunrise = sunrise + self.data['sunrise_offset']
if self.data['sunset_offset'] is not None:
sunset = sunset + self.data['sunset_offset']
return {
SUN_EVENT_SUNRISE: sunrise.astimezone(self.data['timezone']),
SUN_EVENT_SUNSET: sunset.astimezone(self.data['timezone']),
'solar_noon': solar_noon.astimezone(self.data['timezone']),
'solar_midnight': solar_midnight.astimezone(self.data['timezone'])
}
示例3: make_sky
# 需要导入模块: import astral [as 别名]
# 或者: from astral import Location [as 别名]
def make_sky(self):
self.sky = ""
self.dt = pytz.timezone('America/New_York').localize(datetime.now())
self.loc = astral.Location(("New York","New York", 40.7527, -73.9772,"America/New_York","0"))
if self.dt >= self.loc.sunrise(self.dt.date()) and self.dt <= self.loc.sunset(self.dt.date()):
self.make_daysky()
self.get_weather()
else:
self.make_nightsky()
return self.sky
示例4: main
# 需要导入模块: import astral [as 别名]
# 或者: from astral import Location [as 别名]
def main(us_se):
if 'location' not in us_se:
logger.error('Invalid config file')
return
if us_se['location']['auto_enabled']:
loc = get_loc_from_ip()
if loc:
loc = loc.json()
else:
logger.error('Couldn\'t connect to ipinfo, giving up')
return
loc['latitude'], loc['longitude'] = (float(x) for x in loc['loc'].strip().split(','))
loc['time_zone'] = tzlocal.get_localzone().zone
else:
for k, v in us_se['location']['manual'].items():
try:
val = v.strip()
except AttributeError:
pass
else:
if not val:
logger.error('Auto location is not enabled and some manual values are missing')
return
loc = us_se['location']['manual']
try:
location = Location()
location.name = loc['city']
location.region = loc['region']
location.latitude = loc['latitude']
location.longitude = loc['longitude']
location.timezone = loc['time_zone']
except ValueError as e:
logger.error(str(e))
return
sunrise = location.sun()['sunrise'].replace(second=0) + timedelta(minutes=us_se['offset']['sunrise'])
sunset = location.sun()['sunset'].replace(second=0) + timedelta(minutes=us_se['offset']['sunset'])
# Convert to UTC for storage
return sunrise.astimezone(pytz.utc), sunset.astimezone(pytz.utc)
# This should only be called when running through systemd