Page 1 of 1
utime.localtime() giving 00:00 in hh:mm format
Posted: Wed Jun 16, 2021 9:38 pm
by moinologics
when my esp-12f board boots up, in main.py i called utime.localtime() function but it giving me 0 and 0 for h and m
when i call same function from webrepl console, it giving me time correctly.
so how i get correct localtime in main.py file.
Board - esp8266, esp-12f
micropython - v1.15
note - i need it without internet connection.
Re: utime.localtime() giving 00:00 in hh:mm format
Posted: Thu Jun 17, 2021 12:08 am
by davef
Code: Select all
https://docs.micropython.org/en/latest/library/utime.html
Maintaining actual calendar date/time: This requires a Real Time Clock (RTC). On systems with underlying OS (including some RTOS), an RTC may be implicit. Setting and maintaining actual calendar time is responsibility of OS/RTOS and is done outside of MicroPython, it just uses OS API to query date/time. On baremetal ports however system time depends on machine.RTC() object. The current calendar time may be set using machine.RTC().datetime(tuple) function, and maintained by following means:
By a backup battery (which may be an additional, optional component for a particular board).
Using networked time protocol (requires setup by a port/user).
Set manually by a user on each power-up (many boards then maintain RTC time across hard resets, though some may require setting it again in such case).
As I use NTP to get the time I haven't tried setting it using machine.RTC().datetime() ... so can't provide any sample code.
Be aware that the RTC on the ESP8266 doesn't keep very good time. For the ESP8266 I use NTP to kick-off a virtual timer that uses the system clock. I will provide code if you want it.
Re: utime.localtime() giving 00:00 in hh:mm format
Posted: Thu Jun 17, 2021 12:49 am
by davef
Just for setting the RTC
Code: Select all
import machine
rtc = machine.RTC()
datetime = rtc.datetime
year = 2019
month = 12
day = 31
_weekday = 0
hours = 19
minutes = 05
seconds = 0
subseconds = 0
rtc.init((year, month, day, _weekday, hours, minutes, seconds, subseconds))
print(datetime_format.format(datetime()[0],get_time_date('month', datetime()[1]),get_time_date('weekday', datetime()[3]),datetime()[2],datetime()[4],datetime()[5],datetime()[6],datetime()[7],))