Micropython get correct current time
Asked Answered
R

4

8

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!

Raffarty answered 12/12, 2019 at 1:55 Comment(2)
Google brings me this thread.Effuse
@Sangbok Lee Thanks to your reply.But RTC class return the 2000/1/1,too.It may need to set time manually.Raffarty
M
5

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.

Mahratta answered 12/12, 2019 at 3:42 Comment(4)
But how do I get (2019, 5, 1, 4, 13, 0, 0, 0)? Because when the board connect,it always get the 2000/1/1 by using these methods.Raffarty
You set it manually using the RTC object. Are you saying you want to set it off the internet? If so then you'll need to use an NTP server. How you do that may be dependent on the port of Micropython that you are using. Look for ntptime.Mahratta
Another way to get time is from GPS data if your board has that capability.Defective
The question is about GETTING the current time and not SETTING it.Ruction
D
16

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")
Deglutinate answered 27/10, 2020 at 13:39 Comment(1)
I use time-a-g.nist.gov. You can adjust UTC for local time by doing time.localtime(time.time() + (-4 * 3600))Merrimerriam
M
5

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.

Mahratta answered 12/12, 2019 at 3:42 Comment(4)
But how do I get (2019, 5, 1, 4, 13, 0, 0, 0)? Because when the board connect,it always get the 2000/1/1 by using these methods.Raffarty
You set it manually using the RTC object. Are you saying you want to set it off the internet? If so then you'll need to use an NTP server. How you do that may be dependent on the port of Micropython that you are using. Look for ntptime.Mahratta
Another way to get time is from GPS data if your board has that capability.Defective
The question is about GETTING the current time and not SETTING it.Ruction
E
2

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 connected via serial (and that nothing else uses that port at the same time).

Install mpremote: pip install -U mpremote
Set MCU time: mpremote rtc --set ( mpremote v1.20+)

Note that MCU time will reset if you reboot it.

PS C:\develop\MyPython\> mpremote rtc --set repl
Connected to MicroPython at COM14
Use Ctrl-] or Ctrl-x to exit this shell
>
MicroPython v1.22.1 on 2024-01-05; ESP32S3 module with ESP32S3
Type "help()" for more information.
>>> import time
>>> time.localtime()
(2024, 1, 15, 1, 13, 11, 0, 15)

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

Erechtheum answered 26/1, 2023 at 21:19 Comment(0)
S
1

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.
Scriabin answered 25/1, 2023 at 19:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.