Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Created March 13, 2025 10:41
Show Gist options
  • Save ArthurDelannoyazerty/8f6422f9145bb559f9f509cc6426fa08 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/8f6422f9145bb559f9f509cc6426fa08 to your computer and use it in GitHub Desktop.
Python script that get the UTC time from a NTP server. Usefull when the machine clock is wrong.
import time
import ntplib
import logging
logger = logging.getLogger(__name__)
def get_current_utc_time() -> float:
logger.debug('Requesting current UTC time')
response = ntplib.NTPClient().request('pool.ntp.org')
logger.debug(f'UTC time : {response.tx_time} | {time.strftime('%Y-%m-%dT%H-%M-%S',time.gmtime(response.tx_time))}')
return response.tx_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment