Datetime utility functions

Carel Fellinger carel.fellinger at chello.nl
Tue Sep 16 12:44:00 EDT 2003


On Tue, Sep 16, 2003 at 05:19:50PM +0300, Christos TZOTZIOY Georgiou wrote:
...
> I sent my own version without having seen your own --and mine might seem
> obfuscated, compared to yours.

you both use the same indirect way of getting to the end of the month.
why not do what datetime.c does, like:

    _days_in_month = [
        0, # unused; this vector uses 1-based indexing */
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
        ]
    
    def _is_leap_year(year):
        return year % 4 == 0 and (year % 100 !=0 or year % 400 == 0)
    
    def days_in_month(year, month):
        if month == 2 and _is_leap_year(year):
            return 29
        else:
            return _days_in_month[month]
    
    def end_of_month(d):
        date(d.y, d.m, days_in_month(d.y, d.m))


-- 
groetjes, carel





More information about the Python-list mailing list