Skip to content

Commit

Permalink
Feat(Time Sync): Use new internal API
Browse files Browse the repository at this point in the history
Uses a new internally hosted time API for date and time synchronisation.
  • Loading branch information
CodeGoat-dev committed Jan 27, 2025
1 parent 7422d76 commit 5d59840
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 8 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.0.4

### Changes

#### Date And Time Synchronisation

Date and time synchronisation now uses a new internal API to improve reliability.

## V1.0.3

### New Features
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Pico Network Manager comes packed with the following features:
Configure a web interface for your firmware to be used when connected to a network.

- **Time Synchronisation**
Enable automatic time synchronisation from World Time API when connected to wi-fi.
Enable automatic time synchronisation from our own internally hosted time API when connected to wi-fi.

---

Expand Down
17 changes: 8 additions & 9 deletions src/NetworkManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def serve_index(self):

async def get_ntp_time(self):
"""Fetches the current date and time from an NTP server or time API and sets the system time."""
url = "http://worldtimeapi.org/api/ip"
url = "https://goatbot.org/api/time"

try:
print("Fetching time from API...")
Expand All @@ -452,16 +452,15 @@ async def get_ntp_time(self):
data = response.json()

# Extract datetime string from response
datetime_str = data['datetime']
datetime_str = data['currentTime']

# Parse datetime string: "2025-01-26T12:34:56.789123+00:00"
date_time = datetime_str.split('T')
date = date_time[0].split('-')
time_ = date_time[1].split(':')
if not datetime_str:
raise ValueError("Missing 'currentTime' in API response.")

# Extract year, month, day, hour, minute, second
year, month, day = map(int, date)
hour, minute, second = map(int, time_[:2])
# 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(':'))

# Set the system time
time_tuple = (year, month, day, hour, minute, second, 0, 0, 0)
Expand Down

0 comments on commit 5d59840

Please sign in to comment.