Date/Time management

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Date/Time management

Post by jimmo » Tue Jan 28, 2020 1:09 pm

Looks like you're probably on ESP32.

On ESP32, the weekday param in the tuple is ignored in rtc.datetime(timetuple) (as it can be inferred from the date). ESP32 knows the calendar, and 28th Jan 2020 is a Tuesday (monday=0, tuesday=1), so that's why it's a 1 when you read the tuple back out again. You could change that 2 to any number and the behavior of your test program would be the same.

This matches regular Python's behavior where Monday=0 -- https://docs.python.org/3/library/datet ... te.weekday

On STM32, it relies on the hardware RTC to manage both the day/month/year as week as the weekday, so it will happily take whatever weekday you tell it and give the same weekday back to you.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Date/Time management

Post by stanely » Tue Jan 28, 2020 3:28 pm

Yes, I was surprised a while back by my ESP32 in a similar way. It does its own weekday calculation. It also assumes UTC so if you pull time from NTP, you might get back an unexpected weekday.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Date/Time management

Post by pythoncoder » Tue Jan 28, 2020 5:20 pm

Unfortunately machine.RTC documentation is poor, and the datetime method is not documented at all. In practice it corresponds to that in the pyb module, where the docs state that the weekday is 1-7 for Monday through Sunday.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Date/Time management

Post by kevinkk525 » Tue Jan 28, 2020 7:08 pm

Often there is a confusion because python and linux have a different weekday logc. Python having monday as 0 while linux has Sunday as 0.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Date/Time management

Post by stanely » Tue Jan 28, 2020 7:45 pm

I was very excited to see that the ESP32 MicroPython RTC now supports microseconds. Reading some earlier posts made me think that wouldn't happen, but it does... and very well!

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Date/Time management

Post by stanely » Wed Jan 29, 2020 12:18 am

Oh no! I think I spoke too soon. Microseconds roll, but whenever I set the RTC, it looks like the microseconds are set to 0. Is this a MicroPython or ESP32 thing? I need to sync this thing to a time standard using IEEE 1588 PTP so that observations correlate to an actual time. Is there a way system calls let me do this?

Hopefully it won't be necessary to keep a microsecond offset outside the RTC.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Date/Time management

Post by stanely » Wed Jan 29, 2020 12:31 am

I found a github issue that was fixed in November 2019. Sadly I'm using V 1.11 because it's a build that supports 8MB flash.

Updating to 1.12 made it possible to set the RTC microseconds.

User avatar
RWLTOK
Posts: 53
Joined: Thu Dec 14, 2017 7:24 pm

Re: Date/Time management

Post by RWLTOK » Mon Mar 09, 2020 4:13 am

I tried the example:

Code: Select all

import machine
rtc = machine.RTC()
rtc.init((2020, 3, 8, 23, 51, 0, 0, 0))
which results with:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given
pythoncoder wrote:
Fri Sep 30, 2016 4:42 pm
See http://docs.micropython.org/en/latest/e ... utime.html - notably localtime().

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Date/Time management

Post by pythoncoder » Mon Mar 09, 2020 7:29 am

The documentation is poor; so poor that the example code doesn't actually work. I have raised this and offered a partial fix. This has not been taken up.

You haven't said what platform you're using but if it's a Pyboard I suggest you use pyb.RTC and rtc.datetime. The pyb docs are correct and datetime does work on the platforms I tested.
Peter Hinch
Index to my micropython libraries.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: Date/Time management

Post by stanely » Thu Mar 12, 2020 1:37 pm

I wrote a clock app for the ESP32 where I messed with time. I got the NTP time and then offset it by timezone. Here's how I set the localtime:

Code: Select all

# Set the RTC to local time.  (I don't think the uP docs are exactly right about this.)
RTC().datetime((yy, mo, dd, 0, hh, mm, ss, 0))
You can see all of the code here: https://github.com/the-stanely/Clock-ESP32-Nokia5110

Post Reply