Skip to content

Commit

Permalink
fixed daylight saving time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jfabernathy committed Mar 26, 2021
1 parent f0a8128 commit a806b92
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions display_environment_in_5th_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,37 @@ def Temperature_transform(val3):
return "Temperature: " + str(val3)

def time_transform(val):
# val is in format '2021-03-25T20:07:39.402Z'

dstStart = {2021: 14, 2022: 13, 2023: 12, 2024: 10, 2025: 9, 2026: 8, 2027: 14, 2028: 12, 2029: 11, 2030: 10, 2031: 9}

dstStop = {2021: 7, 2022: 6, 2023: 5, 2024: 3, 2025: 2, 2026: 1, 2027: 7, 2028: 5, 2029: 4, 2030: 3, 2031: 2}

if val == None:
val = "When: Unavailable"
year = int(val[0:4])
month = int(val[5:7])
day = int(val[8:10])
hour = int(val[11:13]) - 5
hour = int(val[11:13]) # hour is in UTC
if month in range(3, 12):
if month == 3 and day >= int(dstStart[year])+1 or month == 3 and day == int(dstStart[year]) and hour >= 7:
timezoneOffset = 4

elif month == 11 and day <= int(dstStop[year]):
if day <= int(dstStop[year])-1 or day == int(dstStop[year]) and hour <= 6:
timezoneOffset = 4
else:
timezoneOffset = 5

elif month in range(4,11):
timezoneOffset = 4
else:
timezoneOffset = 5

else:
timezoneOffset = 5

hour = hour - timezoneOffset
if hour < 0:
hour = hour + 24
day = day -1
Expand All @@ -40,7 +66,7 @@ def time_transform(val):
else:
timestring = "%d:%02d am" % (hour, min)

return "%s %d, at %s" % (months[month-1], day, timestring)
return "%s %d, %d, at %s" % (months[month-1], day, year, timestring)

def Humidity_transform(val2):
if val2 == None:
Expand All @@ -63,21 +89,21 @@ def Humidity_transform(val2):

# Formatting for the Time text
magtag.add_text(
text_font="/fonts/Arial-12.bdf",
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 38),
text_transform=time_transform
)

# Formatting for the Temperature text
magtag.add_text(
text_font="/fonts/Arial-12.bdf",
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 60),
text_transform=Temperature_transform
)

# Formatting for the Humidity text
magtag.add_text(
text_font="/fonts/Arial-12.bdf",
text_font="/fonts/Arial-Bold-12.pcf",
text_position=(10, 82),
text_transform=Humidity_transform
)
Expand All @@ -88,9 +114,9 @@ def Humidity_transform(val2):
# This statement gets the JSON data and displays it automagically
value = magtag.fetch()
print("Response is", value)
except (ValueError, RuntimeError) as e:
except Exception as e:
print("Some error occured, retrying! -", e)

magtag.exit_and_deep_sleep(2)
# wait 2 seconds for display to complete
time.sleep(2)
magtag.exit_and_deep_sleep(60 * 5)

0 comments on commit a806b92

Please sign in to comment.