v.0.7.3 time_utils ntp access
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
import time
|
||||
import ntptime # type: ignore
|
||||
|
||||
_UTC_OFFSET: int = 1 * 3600 # +1 or +2 hours for CEST, adjust for your timezone
|
||||
|
||||
def local_time_with_offset():
|
||||
# Set timezone offset (in seconds)
|
||||
current_time = time.time() + _UTC_OFFSET
|
||||
return time.localtime(current_time)
|
||||
|
||||
|
||||
def get_datetime_string(format='full'):
|
||||
"""
|
||||
Return date/time as string with different formats
|
||||
@@ -8,7 +17,7 @@ def get_datetime_string(format='full'):
|
||||
format: "full", "date", "time", "short", "ticks"
|
||||
"""
|
||||
try:
|
||||
year, month, day, hour, minute, second, weekday, yearday = time.localtime()
|
||||
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
|
||||
|
||||
if format == 'full':
|
||||
return f'{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}'
|
||||
@@ -28,7 +37,7 @@ def get_datetime_string(format='full'):
|
||||
def get_german_datetime():
|
||||
"""Return German date and time"""
|
||||
try:
|
||||
year, month, day, hour, minute, second, weekday, yearday = time.localtime()
|
||||
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
|
||||
|
||||
weekdays = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
|
||||
weekday_name = weekdays[weekday]
|
||||
@@ -81,3 +90,13 @@ def get_german_date_ticks():
|
||||
month_name = months[month - 1]
|
||||
|
||||
return f'{day:02d}.{month_name}.{str(year)[-2:]}'
|
||||
|
||||
def sync_ntp_time():
|
||||
try:
|
||||
print("Syncing time via NTP...")
|
||||
ntptime.settime() # Default uses pool.ntp.org
|
||||
print("Time synchronized successfully!")
|
||||
return True
|
||||
except Exception as e:
|
||||
print("NTP sync failed:", e)
|
||||
return False
|
||||
Reference in New Issue
Block a user