Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion examples/word_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

# Length of time between updates in minutes.
UPDATE_INTERVAL = 15
# Offset in hours from UTC, ie -5 for NY (UTC - 5), 1 for Paris
UTC_OFFSET = 0

rtc = machine.RTC()
time_string = None
Expand All @@ -32,6 +34,27 @@
# Set the correct time using the NTP service.
ntptime.settime()

# adjust utc time by offset hours
def adjust_to_timezone(rtc_datetime, offset_hours):

# extract the time components from our tuple
year, month, day, _, hours, minutes, seconds, _ = rtc_datetime

# convert the current datetime tuple to a timestamp
utc_timestamp = time.mktime((year, month, day, hours, minutes, seconds, 0, 0))

# apply the timezone offset in seconds
adjusted_timestamp = utc_timestamp + (offset_hours * 3600)

# convert the adjusted timestamp back to a local datetime tuple
adjusted_time = time.localtime(adjusted_timestamp)

# extract adjusted values
hours, minutes = (adjusted_time[3], adjusted_time[4])

return hours, minutes


def approx_time(hours, minutes):
nums = {0: "twelve", 1: "one", 2: "two",
3: "three", 4: "four", 5: "five", 6: "six",
Expand Down Expand Up @@ -61,7 +84,10 @@ def update():
print("Unable to contact NTP server")

current_t = rtc.datetime()
time_string = approx_time(current_t[4] - 12 if current_t[4] > 12 else current_t[4], current_t[5])
# perform timezone adjustment here (relative to UTC)
adjusted_hr, adjusted_min = adjust_to_timezone(current_t, UTC_OFFSET)

time_string = approx_time(adjusted_hr - 12 if adjusted_hr > 12 else adjusted_hr, adjusted_min)

# Splits the string into an array of words for displaying later
time_string = time_string.split()
Expand Down