Date/Time management

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Date/Time management

Post by JimTal001 » Wed Sep 30, 2015 6:16 pm

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.

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

Re: Date/Time management

Post by pythoncoder » Thu Oct 01, 2015 7:03 am

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.
Peter Hinch
Index to my micropython libraries.

User avatar
bmarkus
Posts: 111
Joined: Tue Oct 21, 2014 5:58 am

Re: Date/Time management

Post by bmarkus » Thu Oct 01, 2015 2:38 pm

I didn't check it on PyBoard, but in the Linux version there are no time module only utime and utime.time() returns float.
Tiny Core Linux (piCore) developer
HAM radio call: HA5DI (Béla)

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Date/Time management

Post by JimTal001 » Thu Oct 01, 2015 11:55 pm

thanks for the help.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Date/Time management

Post by dhylands » Fri Oct 02, 2015 12:18 am

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,

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Date/Time management

Post by JimTal001 » Fri Oct 02, 2015 12:52 am

Thanks Dave, that is exactly what I need ?

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Date/Time management

Post by weldeng » Fri Sep 30, 2016 4:38 pm

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

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

Re: Date/Time management

Post by pythoncoder » Fri Sep 30, 2016 4:42 pm

Peter Hinch
Index to my micropython libraries.

weldeng
Posts: 31
Joined: Thu Sep 15, 2016 12:47 am

Re: Date/Time management

Post by weldeng » Fri Sep 30, 2016 5:31 pm

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

riteman2
Posts: 2
Joined: Thu Jan 16, 2020 5:43 pm

Re: Date/Time management

Post by riteman2 » Tue Jan 28, 2020 11:18 am

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??

Post Reply