Files

39 lines
1.2 KiB
Python
Raw Permalink Normal View History

2025-05-26 11:11:13 +02:00
import time
2026-01-24 19:31:54 +01:00
from .time_zone import TimeZone
2025-07-02 12:43:01 +02:00
import localPTZtime
2025-05-26 11:11:13 +02:00
def epoch_seconds():
import sys
if sys.platform == "esp32":
# on esp32, it needs this correction:
return time.time() + 946684800
else:
return round(time.time())
2025-06-06 21:58:40 +02:00
def sync_time():
import ntptime
print("Synchronizing clock...")
# Set the NTP server and sync time
ntptime.host = 'pool.ntp.org' # Set NTP server
try:
print('Syncing time with', ntptime.host)
ntptime.settime() # Fetch and set time (in UTC)
2025-07-02 12:43:01 +02:00
print("Time sync'ed successfully")
TimeZone.refresh_timezone_preference() # if the time was sync'ed, then it needs refreshing
2025-06-06 21:58:40 +02:00
except Exception as e:
print('Failed to sync time:', e)
2025-07-02 12:43:01 +02:00
def localtime():
if not TimeZone.timezone_preference: # if it's the first time, then it needs refreshing
TimeZone.refresh_timezone_preference()
ptz = TimeZone.timezone_to_posix_time_zone(TimeZone.timezone_preference)
2025-07-02 12:43:01 +02:00
t = time.time()
2025-10-20 22:17:43 +02:00
try:
localtime = localPTZtime.tztime(t, ptz)
except Exception as e:
#print(f"localPTZtime setting got exception {e}, defaulting to non-localized time...") # this gets called too often to print
return time.localtime()
return localtime
2025-07-02 12:43:01 +02:00