4 ms·
The C (or at least POSIX) functions that accept broken-down (i.e. seconds, minutes, hours) datetimes allow numbers outside of the range which makes it possible
by Erwin 15y ago
The C (or at least POSIX) functions that accept broken-down (i.e. seconds, minutes, hours) datetimes allow numbers outside of the range which makes it possible to advance the calendar one month even if you're already in December. For example, give me the date for 8am yesterday:
>>> t = list(time.localtime()); t[2] -= 1; t[3:6] = [8,0,0]; print time.ctime(time.mktime(t))
Wed Feb 29 08:00:00 2012
That correctly handled the leap day and the change in month number -- the 0th day of March became the last day of February.
Give the 1st of the month, 11 months from now:
>>> t = list(time.localtime()); t[1] += 11; t[2:6] = [1,8,0,0]; print time.ctime(time.mktime(t))
Fri Feb 1 08:00:00 2013
That also converted the 14th month of 2012 into the 2nd month of 2013.
- lysium 15y agoI figured you talk Python, that's good to know (x)! However, if you continue: >>> t = list(time.localtime()); t[2] -= 1; t[3:6] = [8,0,0]; print time.ctime(time.mktime(t)) Wed Feb 29 08:00:00 2012 >>> t[1] -= 1; >>> time.ctime(time.mktime(t)) 'Tue Jan 31 08:00:00 2012' So if you have a `t` that is Feb 29, one month ago is Jan 31 or Feb 29, depending on how `t` was constructed. (x) Edit: I meant, it's good to know about the time module in Python, not that you know Python...
- Erwin 15y agoI don't follow -- the first time which translates to Feb 29th is entered as 0th March. So that underflows to Feb 29th -- the day before March 1st. The second time you have, is 0th February. So that underflows back to the previous day, Jan 31st -- the day before February 1st.
- lysium 15y agoI mean there are two t (0th March and Feb 29th) that print as Feb 29 (so any user seeing the value of those t may rightly assume they are the same), but for one t one month earlier is Jan 31, while for the other t one month earlier is Jan 29. Of course, internally, all is correct, but for the user, things may behave strangely.