1import time
2
3"""
4Unix uses 1970 as the epoch, but some embedded ports use 2000.
5You can check the actual epoch with gmtime(0)[0].
6On ESP32, MicroPython’s epoch is 2000 while the IDF SDK still uses 1970,
7so localtime is offset by 30 years; time.time() itself is correct and unaffected.
8"""
9print(time.gmtime(0))
10print(time.localtime())
11
12EPOCH_DIFF = 946684800 # 1970-2000
13
14
15def unix_localtime(secs=None):
16 if secs is None:
17 secs = time.time()
18 return time.gmtime(secs - EPOCH_DIFF)
19
20
21print(unix_localtime())
22
23
24def format_log_time():
25 ms = time.ticks_ms()
26 t_s = unix_localtime()
27 stamp = "{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}".format(
28 *t_s[:6], ms % 1000
29 )
30 return stamp
31
32
33print(format_log_time())