Skip to content

Commit

Permalink
Fix(Time Sync): Fix setting time
Browse files Browse the repository at this point in the history
Fixes an issue where the time might not be properly set.
Switches to the internal RTC module for setting time.
  • Loading branch information
CodeGoat-dev committed Feb 3, 2025
1 parent 231be73 commit b8fdbfb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
14 changes: 14 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This document outlines the changes made between versions of the **Goat - Pico Network Manager** library.

## V1.1.1

### Bug Fixes

#### Time Synchronisation

Fixes an issue where the time might not always be synchronised correctly. The internal RTC module is now used to set the date and time.

## V1.1.0

## New Features
Expand All @@ -10,6 +18,12 @@ This document outlines the changes made between versions of the **Goat - Pico Ne

The **Time Synchronisation API** is now included as a NodeJS application. This enables users to host their own local Time Synchronisation API.

### changes

#### Instantiation

The `time_server` class instantiation property can now be used to set the time synchronisation server. Only use this if you are self-hosting the **Time Synchronisation API**.

## V1.0.4

### Changes
Expand Down
20 changes: 12 additions & 8 deletions src/NetworkManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Includes DNS redirection for captive portal compliance.

# Imports
import machine
import network
import socket
import time
Expand Down Expand Up @@ -458,16 +459,19 @@ async def get_ntp_time(self):
if not datetime_str:
raise ValueError("Missing 'currentTime' in API response.")

# Parse datetime string: "2025-01-26T12:34:56"
date_part, time_part = datetime_str.split('T')
year, month, day = map(int, date_part.split('-'))
hour, minute, second = map(int, time_part.split(':'))
# Convert to struct_time
parsed_time = time.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")

# Set the system time
time_tuple = (year, month, day, hour, minute, second, 0, 0, 0)
time.mktime(time_tuple)
# Convert to RTC-compatible tuple
rtc_time = (parsed_time[0], parsed_time[1], parsed_time[2],
parsed_time[6], parsed_time[3], parsed_time[4],
parsed_time[5], 0) # Subseconds set to 0

# Set RTC time
rtc = machine.RTC()
rtc.datetime(rtc_time)

print("Date and time set to:", datetime_str)
print("Date and time set to:", rtc.datetime())
else:
print(f"Failed to fetch time. Status code: {response.status_code}")
except Exception as e:
Expand Down

0 comments on commit b8fdbfb

Please sign in to comment.