Micropython get correct current time

Question:

Because of micropython doesn’t import the datetime.

I want use time or utime modules to get current time.

But the time.localtime() result is
like (2000, 1, 1, 0, 12, 35, 5, 1)

I guess the time start at the 2000/1/1.

How to set the start time on that?

Or other recommanded way can do the correct result?

Thanks!

Asked By: Hsinhsin Hung

||

Answers:

Use RTC to set the time:

from pyb import RTC   # or import from machine depending on your micropython version
rtc = RTC()
rtc.datetime((2019, 5, 1, 4, 13, 0, 0, 0))

You can then just use time.localtime() and string formatting to make it look however you want.

Answered By: John S

You can use a library to set time via internet over NTP protocol. You have to be connected with the internet for example wifi on esp32

import ntptime
import time

#if needed, overwrite default time server
ntptime.host = "1.europe.pool.ntp.org"

try:
  print("Local time before synchronization:%s" %str(time.localtime()))
  #make sure to have internet connection
  ntptime.settime()
  print("Local time after synchronization:%s" %str(time.localtime()))
except:
  print("Error syncing time")
Answered By: emch2

With utime you can get the local time as below.

#Get the current time
current_time = utime.localtime()
#Format the current time as "dd/mm/yyyy HH:MM"
formatted_time = "{:02d}/{:02d}/{} {:02d}:{:02d}".format(current_time[2], current_time[1], current_time[0], current_time[3], current_time[4])

This code uses the localtime() function to get the current time, it then uses string formatting to format the time in the desired format. It uses the tm_mday, tm_mon, tm_year, tm_hour, tm_min attributes of the struct_time object to create the formatted string.
The :02d is used to format the values as 2 digit numbers, this ensures that single digit days and months are padded with a leading zero.

tm_year: the current year (e.g. 2022)
tm_mon: the current month (1-12)
tm_mday: the current day of the month (1-31)
tm_hour: the current hour (0-23)
tm_min: the current minute (0-59)
tm_sec: the current second (0-59)
tm_wday: the current day of the week (0-6, Monday is 0)
tm_yday: the current day of the year (1-366)
tm_isdst: 1 if Daylight Saving Time is in effect, 0 otherwise.
Answered By: Angad Cheema

If your question is: how do I set the time on a board without network connection

The simplest way (nowadays) is using mpremote

mpremote is a MicroPython utility that you can install to your PC, that has a command to set the MCU time to that of your PC.

The requirement is that the MCU is connection via serial, (and that nothing else uses that port at the same time)

Install mpremote : pip install -U mpremote

PS C:developMyPython> mpremote setrtc repl
Connected to MicroPython at COM14
Use Ctrl-] to exit this shell
>
MicroPython v1.19.1 on 2022-06-18; ESP32S3 module with ESP32S3
Type "help()" for more information.
>>> import time
>>> time.localtime()
(2020, 1, 1, 10, 0, 17, 2, 1)

here mpremote autodetects the first device, connects to that, sets the time, and enters the MicroPython repl

more info: https://docs.micropython.org/en/latest/reference/mpremote.html
and

Answered By: Jos Verlinde
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.