Raspberry Pi and MicroPython: get the correct time from the internet with an API
Fix a Raspberry Pi with the wrong time, or give a Pico W the local time: shell, Python and MicroPython examples over HTTPS, time zone and DST included.
Updated
Short answer: most Raspberry Pi boards have no battery-backed clock, and a Pico W has no idea what time zone it is in. One HTTPS request to a time API returns the correct Unix time and the local time with DST applied; use it to set the clock or the RTC.
Fix a Raspberry Pi's clock from the shell
# what time does the API say? (key in the query string, so no header is needed)
curl -s "https://api.timelogicapi.com/api/timezone/Etc/UTC.txt?api_key=$TIMELOGIC_API_KEY"
# set the system clock from it (needs sudo)
sudo date -s "@$(curl -s "https://api.timelogicapi.com/api/timezone/Etc/UTC.txt?api_key=$TIMELOGIC_API_KEY" | sed -n 's/^unixtime: //p')"
date -s @<seconds> takes Unix seconds; the .txt response's unixtime: line is exactly that. This is a fallback for networks that block NTP or for a first boot without network time; for continuous sync, keep the system's NTP service (systemd-timesyncd or chrony) running. The NTP vs time API guide explains the trade-offs.
Raspberry Pi with Python
import os, requests
r = requests.get(
"https://api.timelogicapi.com/v1/time/current",
params={"tz": "Europe/London"},
headers={"X-API-Key": os.environ["TIMELOGIC_API_KEY"]},
timeout=5,
)
data = r.json()
print(data["iso_local"], data["day_full"])
More Python patterns, including correcting for a wrong clock without changing it: Get the current time from the internet in Python.
Raspberry Pi Pico W (MicroPython)
import network, time, machine
import urequests as requests # newer MicroPython builds also ship "requests"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("your-ssid", "your-password")
while not wlan.isconnected():
time.sleep(0.2)
r = requests.get("https://api.timelogicapi.com/api/timezone/Europe/London?api_key=YOUR_KEY")
data = r.json()
r.close()
dt = data["datetime"] # e.g. "2025-11-30T20:23:23.016000+00:00", local time
year, month, day = int(dt[0:4]), int(dt[5:7]), int(dt[8:10])
hour, minute, second = int(dt[11:13]), int(dt[14:16]), int(dt[17:19])
weekday = (data["day_of_week"] + 6) % 7 # API: 0 = Sunday; RTC: 0 = Monday
machine.RTC().datetime((year, month, day, weekday, hour, minute, second, 0))
print("RTC set to local time:", dt, data["abbreviation"])
The RTC now holds local time for that zone. Re-sync after a DST change (dst_until in the response says when the current DST period ends) or once a day.
Let the board find its own time zone
Request /api/ip instead of a fixed zone and the API resolves the zone from the public IP address the request comes from. On the /v1 API, GET /v1/time/current?auto_tz=true does the same. Details: Get the timezone from an IP address.
Related guides
Frequently asked questions
Why is my Raspberry Pi's time wrong?
Most Raspberry Pi boards have no battery-backed clock, so after a power cut they start from an old time until network time sync succeeds. If NTP traffic (UDP port 123) is blocked or the network comes up late, the clock stays wrong.
How do I set a Raspberry Pi's time from the internet without NTP?
Fetch the current Unix time over HTTPS and pass it to date: sudo date -s @$(curl -s 'https://api.timelogicapi.com/api/timezone/Etc/UTC.txt?api_key=YOUR_KEY' | sed -n 's/^unixtime: //p'). Use NTP for continuous sync whenever the network allows it.
How do I get the current time on a Raspberry Pi Pico W?
Connect to Wi-Fi, then request https://api.timelogicapi.com/api/timezone/Europe/London?api_key=YOUR_KEY with urequests (requests on newer MicroPython builds) and read datetime or unixtime from the JSON. You can then set machine.RTC() from it.
Does MicroPython handle time zones and DST?
MicroPython has no tz database. The API returns the local time with the offset and DST already applied, so the board does not need one.