From bef724068c7fdefe19c043ee5f6e8cfc788c7877 Mon Sep 17 00:00:00 2001 From: theelectricmayhem <51097226+theelectricmayhem@users.noreply.github.com> Date: Sun, 10 May 2020 16:50:52 -0600 Subject: [PATCH 1/2] Add optional timezone offset for NTP.set_time() --- adafruit_ntp.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/adafruit_ntp.py b/adafruit_ntp.py index b5165a4..5da875c 100644 --- a/adafruit_ntp.py +++ b/adafruit_ntp.py @@ -48,7 +48,7 @@ class NTP: """Network Time Protocol (NTP) helper module for CircuitPython. - This module does not handle daylight savings or local time. + This module does not handle daylight savings time. :param adafruit_esp32spi esp: ESP32SPI object. """ @@ -61,13 +61,16 @@ def __init__(self, esp): raise TypeError("Provided object is not an ESP_SPIcontrol object.") self.valid_time = False - def set_time(self): + def set_time(self, tz_offset=0): """Fetches and sets the microcontroller's current time - in seconds since since Jan 1, 1970. + in seconds since since Jan 1, 1970. Optionally offsets + the current time to that of the requested timezone. + + :param int tz_offset: Timezone offset from GMT in hours """ try: now = self._esp.get_time() - now = time.localtime(now[0]) + now = time.localtime(now[0]+ (tz_offset * 3600)) # 3600 seconds in an hour rtc.RTC().datetime = now self.valid_time = True except ValueError: From 059aeebb15d0b884705f2adcdbfedb53031cd474 Mon Sep 17 00:00:00 2001 From: theelectricmayhem <51097226+theelectricmayhem@users.noreply.github.com> Date: Tue, 12 May 2020 14:12:36 -0600 Subject: [PATCH 2/2] Fix formatting for Black --- adafruit_ntp.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/adafruit_ntp.py b/adafruit_ntp.py index 5da875c..1a4a27a 100644 --- a/adafruit_ntp.py +++ b/adafruit_ntp.py @@ -48,7 +48,7 @@ class NTP: """Network Time Protocol (NTP) helper module for CircuitPython. - This module does not handle daylight savings time. + This module does not handle daylight savings or local time. :param adafruit_esp32spi esp: ESP32SPI object. """ @@ -63,15 +63,15 @@ def __init__(self, esp): def set_time(self, tz_offset=0): """Fetches and sets the microcontroller's current time - in seconds since since Jan 1, 1970. Optionally offsets - the current time to that of the requested timezone. - - :param int tz_offset: Timezone offset from GMT in hours + in seconds since since Jan 1, 1970. + + :param int tz_offset: Timezone offset from GMT """ try: now = self._esp.get_time() - now = time.localtime(now[0]+ (tz_offset * 3600)) # 3600 seconds in an hour + now = time.localtime(now[0] + (tz_offset * 3600)) # 3600 seconds in an hour rtc.RTC().datetime = now self.valid_time = True - except ValueError: + except ValueError as error: + print(str(error)) return