Search code examples
pythonmicropython

Micropython get correct current time


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!


Solution

  • 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.