Date/Time management
Date/Time management
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Date/Time management
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.
Index to my micropython libraries.
Re: Date/Time management
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)
HAM radio call: HA5DI (Béla)
Re: Date/Time management
thanks for the help.
Re: Date/Time management
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:
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,
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)
Re: Date/Time management
Thanks Dave, that is exactly what I need ?
Re: Date/Time management
>>> import timepythoncoder 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.
>>> 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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Date/Time management
See http://docs.micropython.org/en/latest/e ... utime.html - notably localtime().
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Date/Time management
>>> year, month, day, hour, minute, second, ms, dayinyear = utime.localtime()pythoncoder wrote:See http://docs.micropython.org/en/latest/e ... utime.html - notably localtime().
>>> year
2016
>>> day
30
>>> minute
28
Thanks again works great
Re: Date/Time management
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??
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??