Page 1 of 3

Date/Time management

Posted: Wed Sep 30, 2015 6:16 pm
by JimTal001
Is there a library available (like datetime.py) to help with date/time math/management?

For example I need to:
1. Create a date/time variable.
2. Add 10 hr to the current date/time and store it to the variable.
3. Write the variable's value to a file.
4. After waking from a standby(), I must read the data/time value from file and store to variable.

Re: Date/Time management

Posted: Thu Oct 01, 2015 7:03 am
by pythoncoder
MicroPython supports the time module so times can be stored to a file as an integer (seconds since 1st Jan 2000). time.time() returns this number from the RTC. So you need to add 36000 to this before storing it. On waking you can restore it and perform calculations based on the stored value and the current value of time.time(). The RTC continues running in standby: see http://docs.micropython.org/en/latest/l ... b.RTC.html for details of how to use it.

Re: Date/Time management

Posted: Thu Oct 01, 2015 2:38 pm
by bmarkus
I didn't check it on PyBoard, but in the Linux version there are no time module only utime and utime.time() returns float.

Re: Date/Time management

Posted: Thu Oct 01, 2015 11:55 pm
by JimTal001
thanks for the help.

Re: Date/Time management

Posted: Fri Oct 02, 2015 12:18 am
by dhylands
Not sure why float is a problem. If you take the integer portion of the float you get the number of seconds from the epoch.

For adding anything up to days, then figuring out how many seconds to add (i.e. 86400 seconds in a day, 3600 seconds in an hour) and doing that works well.

For anything else, you can use utime.localtime() to break the result from time.time() out into separate fields, and then use utime.mktime() to pack it all back up.

So to add 7 months to a time:

Code: Select all

>>> import utime as time
>>> rtc = pyb.RTC()
>>> rtc.datetime((2015,10,1,4,17,11,0,0))
>>> now = time.time()
>>> tm = time.localtime(now)
>>> tm
(2015, 10, 1, 17, 11, 10, 3, 274)
>>> future_time = time.mktime((tm[0], tm[1] + 7, tm[2], tm[3], tm[4], tm[5], tm[6], tm[7]))
>>> time.localtime(future_time)
(2016, 5, 1, 17, 11, 10, 6, 122)
So the cool thing is that you can just add/subtract to each of the individual fields, and not have to worry about wraparound effects. The first thing that mktime does is to normalize everything to be in an appropriate range,

Re: Date/Time management

Posted: Fri Oct 02, 2015 12:52 am
by JimTal001
Thanks Dave, that is exactly what I need ?

Re: Date/Time management

Posted: Fri Sep 30, 2016 4:38 pm
by weldeng
pythoncoder wrote:MicroPython supports the time module so times can be stored to a file as an integer (seconds since 1st Jan 2000). time.time() returns this number from the RTC. So you need to add 36000 to this before storing it. On waking you can restore it and perform calculations based on the stored value and the current value of time.time(). The RTC continues running in standby: see http://docs.micropython.org/en/latest/l ... b.RTC.html for details of how to use it.
>>> import time
>>> time.time()
528444018

In MicroPython for ESP8266 is there a script that allows me to convert this time integer to extract just the hour (0-24) or hour, minute?

Thanks

Re: Date/Time management

Posted: Fri Sep 30, 2016 4:42 pm
by pythoncoder

Re: Date/Time management

Posted: Fri Sep 30, 2016 5:31 pm
by weldeng
pythoncoder wrote:See http://docs.micropython.org/en/latest/e ... utime.html - notably localtime().
>>> year, month, day, hour, minute, second, ms, dayinyear = utime.localtime()
>>> year
2016
>>> day
30
>>> minute
28

Thanks again works great

Re: Date/Time management

Posted: Tue Jan 28, 2020 11:18 am
by riteman2
Hi

I have a strange issue:

print(year,month,day,weekday,hour,minutes,seconds)
rtc.datetime((year, month, day, weekday,hour, minutes, seconds,0)) # set date and time
year, month, day, weekday, hour, minutes, second, ms = rtc.datetime()
print(rtc.datetime())

results in
2020 1 28 2 11 59 18
(2020, 1, 28, 1, 11, 59, 18, 0)

so weekday is offset by 1??