[Python-Dev] datetime issues (original) (raw)
Gustavo Niemeyer niemeyer at conectiva.com
Tue Sep 16 09:54:36 EDT 2003
- Previous message: [Python-Dev] datetime issues
- Next message: [Python-Dev] python/dist/src/Lib/bsddb __init__.py,1.5,1.6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
The mxDateTime code comes under the eGenix Public License, so reuse is permittted under the terms of the license. That said, I doubt that you want to maintain a date/time parser in the Python core -- I certainly don't :-)
Thanks. I've got the relativedelta type ready. It's based on your specification (thanks for it), but uses no code from your module (you'll notice that clearly when looking at the code). Indeed, it has even a different algorithm, which solves some of the current "problems" with your implementation (like getting 03/03 when adding one month to 31/01). You're free to adopt it, if you feel like so. I'll be releasing that code under the PSF license.
I'll probably work on a generic parser, once I get some more time. Depending on the results, I'll submit it for review and possible inclusion as well.
Here is the documentation of relativedelta, FWIW:
""" The relativedelta type is based on the specification of the excelent work done by M.-A. Lemburg in his mx.DateTime extension. However, notice that this type does NOT implement the same algorithm as his work. Do NOT expect it to behave like mx.DateTime's counterpart.
There's two different ways to build a relativedelta instance. The first one is passing it two date/datetime classes:
relativedelta(datetime1, datetime2)
And the other way is to use the following keyword arguments:
year, month, day, hour, minute, seconds, microseconds:
Absolute information.
years, months, weeks, days, hours, minutes, seconds, microseconds:
Relative information, may be negative.
weekday:
Tuple with (wday, nth), specifying the nth relative weekday.
Here is the behavior of operations with relativedelta:
Calculate the absolute year, using the 'year' argument, or the original datetime year, if the argument is not present.
Add the relative 'years' argument to the absolute year.
Do steps 1 and 2 for month/months.
Calculate the absolute day, using the 'day' argument, or the original datetime day, if the argument is not present. Then, subtract from the day until it fits in the year and month found after their operations.
Add the relative 'days' argument to the absolute day. Notice that the 'weeks' argument is multiplied by 7 and added to 'days'.
Do steps 1 and 2 for hour/hours, minute/minutes, second/seconds, microsecond/microseconds.
If the 'weekday' argument is present, calculate the weekday, with the given (wday, nth) tuple. wday is the index of the weekday (0-6, 0=Mon), and nth is the number of weeks to add forward or backward, depending on its signal. Notice that if the calculated date is already Monday, for example, using (0, 1) or (0, -1) won't change the day. """
And here is a little demo:
from dtutil import *; from datetime import *
relativedelta(datetime.now(),datetime(1983,9,10,10,00)) relativedelta(years=+20, days=+6, minutes=+25, seconds=+4, microseconds=+231402)
datetime(1983,9,10,10,00)+relativedelta(years=+20, days=+6, minutes=+25, seconds=+4, microseconds=+231402) datetime.datetime(2003, 9, 16, 10, 25, 4, 231402)
relativedelta(datetime(2000,2,29),datetime(2001,3,29)) relativedelta(years=-1, months=-1) datetime(2001,3,29)+relativedelta(years=-1, months=-1) datetime.datetime(2000, 2, 29, 0, 0) relativedelta(datetime(2000,2,29),datetime(2001,3,30)) relativedelta(years=-1, months=-1) datetime(2001,3,30)+relativedelta(years=-1, months=-1) datetime.datetime(2000, 2, 29, 0, 0)
relativedelta(datetime(2000,2,29),datetime(2001,2,28)) relativedelta(months=-11, days=-28) relativedelta(datetime(2000,2,28),datetime(2001,2,28)) relativedelta(years=-1) relativedelta(datetime(2000,2,27),datetime(2001,2,28)) relativedelta(years=-1, days=-1)
relativedelta(datetime(2001,2,28),datetime(2000,2,29)) relativedelta(years=+1) relativedelta(datetime(2001,2,28),datetime(2000,2,28)) relativedelta(years=+1) relativedelta(datetime(2001,2,28),datetime(2000,2,27)) relativedelta(years=+1, days=+1)
import calendar date.today()+relativedelta(weekday=(calendar.MONDAY, +2)) datetime.date(2003, 9, 29) date.today()+relativedelta(weekday=(calendar.MONDAY, -1)) datetime.date(2003, 9, 15)
-- Gustavo Niemeyer http://niemeyer.net
- Previous message: [Python-Dev] datetime issues
- Next message: [Python-Dev] python/dist/src/Lib/bsddb __init__.py,1.5,1.6
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]