Skip to content

Conversation

Copy link

Copilot AI commented Oct 30, 2025

All worldtimeapi.org API calls were failing, causing the Christmas app to display incorrect dates from 2021 (device default on startup with no RTC persistence).

Changes

  • Removed HTTP API dependencies: Replaced urllib.urequest + json with ntptime module
  • NTP sync on WiFi connect: Calls ntptime.settime() to sync system clock, retries every 5 seconds until successful
  • System time reading: Uses time.localtime() to read date after NTP sync with year validation (2025-2100)
  • Display behavior: Shows "thinking..." until NTP syncs instead of incorrect 2021 date
# After WiFi connection
sync_time_via_ntp()  # Sets system time via NTP

# Then read from system clock
current_time = time.localtime()
year, month, day = current_time[0:3]

One file changed: badge/apps/christmas/__init__.py (70 insertions, 68 deletions)

Original prompt

For the Christmas app, we still have the problem where it is showing the date from 01 Jan 2021. It turns out, this is because the device has no persistence for keeping a date. Every time it starts, it defaults back to that date.

I want to use MicroPython's ntptime module to sync time over the network upon startup of the christmas app.

An example of this might be:


import network
import ntptime
import time

Connect to WiFi first

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('YOUR_SSID', 'YOUR_PASSWORD')

Wait for connection

while not wlan.isconnected():
pass

Sync time via NTP

ntptime.settime()

Now time.localtime() will show correct time

print(time.localtime())

Please investigate and provide a PR that does this correctly, using NTP to set the local date so the app correctly shows how many days it will be until Christmas. NOTE: Until ntp has correctly updated the date, it should show something like, "thinking." It should not show the incorrect date.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Implement time synchronization for Christmas app Replace worldtimeapi.org HTTP calls with NTP time sync Oct 30, 2025
Copilot AI requested a review from atarukun October 30, 2025 19:22
@atarukun atarukun marked this pull request as ready for review October 30, 2025 19:23
Copilot AI review requested due to automatic review settings October 30, 2025 19:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Christmas countdown badge app to use NTP (Network Time Protocol) for time synchronization instead of fetching time from the worldtimeapi.org REST API. This simplifies the implementation and reduces dependencies.

Key changes:

  • Replaces HTTP API calls to worldtimeapi.org with native NTP synchronization using the ntptime module
  • Simplifies state management by replacing caching logic with a one-time sync approach
  • Updates the code to read from the system time after NTP sync instead of parsing JSON responses

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

_last_fetch_attempt = current_ticks
# Update last sync attempt timestamp
_last_sync_attempt = current_ticks
_ntp_sync_attempt += 1
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global variable _ntp_sync_attempt is incremented without thread-safety mechanisms. If sync_time_via_ntp() could be called from multiple contexts concurrently, this could lead to race conditions. Consider adding synchronization or documenting that this function must only be called from a single thread.

Copilot uses AI. Check for mistakes.
Comment on lines +195 to +196
if _ntp_synced:
return True
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once NTP sync succeeds, the function returns early and never attempts to re-sync. If the system time drifts significantly or the device runs for an extended period, the time could become inaccurate. Consider implementing periodic re-synchronization (e.g., once per day) to maintain accuracy over longer runtime periods.

Copilot uses AI. Check for mistakes.
Comment on lines +235 to +236
# Valid range: 2025-2100 (the badge was created in 2025)
if year < 2025 or year > 2100:
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hard-coded year range 2025-2100 will cause this code to fail in 2101. Consider making the lower bound more flexible (e.g., checking year >= 2024 to allow for some margin) or using a constant that can be easily updated. The upper bound check may be unnecessary unless there's a specific reason to distrust years beyond 2100.

Suggested change
# Valid range: 2025-2100 (the badge was created in 2025)
if year < 2025 or year > 2100:
# Valid lower bound: badge was created in 2025, but allow margin
MIN_VALID_YEAR = 2024
if year < MIN_VALID_YEAR:

Copilot uses AI. Check for mistakes.
# Update last fetch attempt timestamp before trying
_last_fetch_attempt = current_ticks
# Update last sync attempt timestamp
_last_sync_attempt = current_ticks
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global variable '_last_sync_attempt' is not used.

Copilot uses AI. Check for mistakes.
@atarukun atarukun merged commit 1258040 into christmas Oct 30, 2025
6 checks passed
@atarukun atarukun deleted the copilot/sync-time-on-startup branch October 30, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants