1212from ltr559 import LTR559
1313
1414import pytz
15+ from pytz import timezone
1516from astral .geocoder import database , lookup
1617from astral .sun import sun
1718from datetime import datetime , timedelta
@@ -79,19 +80,21 @@ def x_from_sun_moon_time(progress, period, x_range):
7980 return x
8081
8182
82- def sun_moon_time (dt , city_name , time_zone ):
83+ def sun_moon_time (city_name , time_zone ):
8384 """Calculate the progress through the current sun/moon period (i.e day or
8485 night) from the last sunrise or sunset, given a datetime object 't'."""
8586
8687 city = lookup (city_name , database ())
8788
8889 # Datetime objects for yesterday, today, tomorrow
89- today = dt .date ()
90- dt = pytz .timezone (time_zone ).localize (dt )
90+ utc = pytz .utc
91+ utc_dt = datetime .now (tz = utc )
92+ local_dt = utc_dt .astimezone (pytz .timezone (time_zone ))
93+ today = local_dt .date ()
9194 yesterday = today - timedelta (1 )
9295 tomorrow = today + timedelta (1 )
9396
94- # Sun objects for yesterfay , today, tomorrow
97+ # Sun objects for yesterday , today, tomorrow
9598 sun_yesterday = sun (city .observer , date = yesterday )
9699 sun_today = sun (city .observer , date = today )
97100 sun_tomorrow = sun (city .observer , date = tomorrow )
@@ -103,29 +106,29 @@ def sun_moon_time(dt, city_name, time_zone):
103106 sunrise_tomorrow = sun_tomorrow ["sunrise" ]
104107
105108 # Work out lengths of day or night period and progress through period
106- if sunrise_today < dt < sunset_today :
109+ if sunrise_today < local_dt < sunset_today :
107110 day = True
108111 period = sunset_today - sunrise_today
109112 mid = sunrise_today + (period / 2 )
110- progress = dt - sunrise_today
113+ progress = local_dt - sunrise_today
111114
112- elif dt > sunset_today :
115+ elif local_dt > sunset_today :
113116 day = False
114117 period = sunrise_tomorrow - sunset_today
115118 mid = sunset_today + (period / 2 )
116- progress = dt - sunset_today
119+ progress = local_dt - sunset_today
117120
118121 else :
119122 day = False
120123 period = sunrise_today - sunset_yesterday
121124 mid = sunset_yesterday + (period / 2 )
122- progress = dt - sunset_yesterday
125+ progress = local_dt - sunset_yesterday
123126
124127 # Convert time deltas to seconds
125128 progress = progress .total_seconds ()
126129 period = period .total_seconds ()
127130
128- return (progress , period , day )
131+ return (progress , period , day , local_dt )
129132
130133
131134def draw_background (progress , period , day ):
@@ -139,7 +142,7 @@ def draw_background(progress, period, day):
139142 if day :
140143 x = WIDTH - x
141144
142- # Calculate position on sun/moon's curve
145+ # Calculate position on sun/moon's curve
143146 centre = WIDTH / 2
144147 y = calculate_y_pos (x , centre )
145148
@@ -319,7 +322,6 @@ def describe_light(light):
319322# Margins
320323margin = 3
321324
322- dt = datetime .now ()
323325
324326# Set up BME280 weather sensor
325327bus = SMBus (1 )
@@ -346,14 +348,13 @@ def describe_light(light):
346348
347349while True :
348350 path = os .path .dirname (os .path .realpath (__file__ ))
349- dt = datetime .now ()
350- progress , period , day = sun_moon_time (dt , city_name , time_zone )
351+ progress , period , day , local_dt = sun_moon_time (city_name , time_zone )
351352 background = draw_background (progress , period , day )
352353
353354 # Time.
354355 time_elapsed = time .time () - start_time
355- date_string = dt .strftime ("%d %b %y" ).lstrip ('0' )
356- time_string = dt .strftime ("%H:%M" )
356+ date_string = local_dt .strftime ("%d %b %y" ).lstrip ('0' )
357+ time_string = local_dt .strftime ("%H:%M" )
357358 img = overlay_text (background , (0 + margin , 0 + margin ), time_string , font_lg )
358359 img = overlay_text (img , (WIDTH - margin , 0 + margin ), date_string , font_lg , align_right = True )
359360
0 commit comments