ntptime — Time Synchronization

This module is used for time synchronization, providing accurate time in Coordinated Universal Time (UTC).

What is NTP

Network Time Protocol (NTP) is a protocol used to synchronize computer clocks. It can synchronize computers with their servers or clock sources (such as quartz clocks, GPS, etc.). It provides high-precision time correction.

Methods

ntptime.settime(timezone=8, server='ntp.ntsc.ac.cn')

Synchronize local time.

  • timezone - Time zone offset, default is UTC+8, compensating 8 hours

  • server - You can specify your own time server. The server parameter is a string type. The default time server is “ntp.ntsc.ac.cn”.

Example:

from mpython import *
import ntptime

mywifi=wifi()
mywifi.connectWiFi('tang','tang123456')

print("Local time before sync: %s" %str(time.localtime()))
ntptime.settime()
print("Local time after sync: %s" %str(time.localtime()))

Output:

Connecting to network...
WiFi Connection Successful,Network Config:('172.20.10.4', '255.255.255.240', '172.20.10.1', '172.20.10.1')
Local time before sync: (2000, 1, 1, 0, 40, 8, 5, 1)
Local time after sync: (2018, 12, 27, 12, 10, 7, 3, 361)
MicroPython v1.0.1-dirty on 2018-11-23; mPython with ESP32
Type "help()" for more information.
>>>