Get the current time from the internet in Python
Get the real current time in Python from a time API instead of the system clock: requests examples, UTC, time zones with DST, clock offset, async.
Updated
Short answer: datetime.now() returns the machine's clock. For the real current time from the internet in Python, call a time API with requests and read the timestamp from the JSON.
import os, requests
r = requests.get(
"https://api.timelogicapi.com/v1/time/current",
params={"tz": "UTC"},
headers={"X-API-Key": os.environ["TIMELOGIC_API_KEY"]},
timeout=5,
)
r.raise_for_status()
now = r.json()
print(now["utc"], now["unix"]) # 2026-10-30T16:22:54Z 1793377374
How do I turn the response into a datetime?
from datetime import datetime, timezone
utc_now = datetime.fromtimestamp(now["unix_ms"] / 1000, tz=timezone.utc)
local = datetime.fromisoformat(now["iso_local"]) # keeps the offset
How do I get the current time in another time zone?
r = requests.get(
"https://api.timelogicapi.com/v1/time/current",
params={"tz": "Asia/Kolkata"},
headers={"Authorization": f"Bearer {os.environ['TIMELOGIC_API_KEY']}"},
timeout=5,
)
print(r.json()["iso_local"]) # local time with its +05:30 offset
Comma-separate up to 10 zones (tz=Europe/London,Asia/Tokyo) for a bulk request. You can also pass ip= or lat= and lon= instead of tz; see timezone from an IP address and timezone from coordinates.
How do I correct a machine whose clock is wrong?
Measure the offset once and apply it, instead of calling the API in a loop:
import time
t0 = time.time()
unix_ms = requests.get(
"https://api.timelogicapi.com/v1/time/current",
params={"tz": "UTC"},
headers={"X-API-Key": os.environ["TIMELOGIC_API_KEY"]},
timeout=5,
).json()["unix_ms"]
t1 = time.time()
offset = unix_ms / 1000 - (t0 + t1) / 2 # seconds to add to time.time()
def trusted_time() -> float:
return time.time() + offset
For keeping a server's own clock in sync, use the operating system's NTP service; the NTP vs time API guide explains when each fits.
Async code (aiohttp)
import asyncio, os, aiohttp
async def main():
headers = {"X-API-Key": os.environ["TIMELOGIC_API_KEY"]}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get("https://api.timelogicapi.com/v1/time/current", params={"tz": "Europe/Berlin"}) as resp:
print((await resp.json())["iso_local"])
asyncio.run(main())
Is there a Python SDK?
Yes, an official Python client is listed in the SDK guide. The examples above use plain requests so they work anywhere.
I used worldtimeapi.org from Python. What changes?
Only the host and a key. requests.get("http://worldtimeapi.org/api/timezone/Europe/London") becomes a request to https://api.timelogicapi.com/api/timezone/Europe/London with your key, and r.json()["datetime"] keeps working. The migration guide shows the diff.
Related guides
- World Time API guide (pillar)
- The same current time API call in JavaScript, for the browser and Node.js
- Raspberry Pi and MicroPython
- Time conversion API
Frequently asked questions
How do I get the current time from the internet in Python?
Send a GET request to a time API and parse the JSON. With requests: requests.get('https://api.timelogicapi.com/v1/time/current', params={'tz': 'UTC'}, headers={'X-API-Key': key}).json() returns unix, unix_ms, utc and iso_local. datetime.now() only reads the local clock.
How do I get UTC time in Python?
From the system clock: datetime.now(timezone.utc). From the internet, independent of the machine's clock: call GET /v1/time/current?tz=UTC and read utc or unix.
How do I get the current time in a specific time zone with Python?
Pass the IANA zone to the API (tz=Asia/Kolkata) and read iso_local, which carries the offset. Locally, zoneinfo.ZoneInfo('Asia/Kolkata') does the same if the machine's clock and tz database are current.
Should I use NTP or an HTTP API from Python?
Use NTP (the operating system's time sync, or the ntplib package) to keep a machine's clock correct. Use an HTTP time API when you need the time in a given zone, DST details, or a signed timestamp, or when UDP port 123 is blocked.