thebjorn wrote: > John Machin wrote: > > thebjorn wrote: > [...] > > > > > > def age(born): > > > now = date.today() > > > birthday = date(now.year, born.month, born.day) > > > > Bad luck if the punter was born on 29 Feb and the current year is not a > > leap year. > > Good catch! Thanks!
Easy catch -- happens at least one per inning[s] :-) > > [..] > > Holy code bloat, Batman! Try this: > > > > return now.year - born.year - (birthday > now) > > yuck :-) But this: return now.year - born.year - (birthday > now and 1 or 0) is not yuck??? > > [...] > > It's the irregular-size months that cause the problems. If you can work > > out the months difference, then just floor_div by 12 to get the years > > difference. > > I don't agree that the irregular sized months cause a problem in this > case. They do cause a problem if you're asking "when is today + one > month?", i.e. there isn't an unambiguous answer to that question in > general (e.g. if today was January 31). We're asking a different kind > of question though: "has it been at least one month since January 31?", > the answer would be no on Feb 29 and yes on Mar 1. If a bank were paying you interest on a monthly basis, and you deposited money on Jan 31 and pulled it out on the last day of February, that would count as one month. This is what I call the "today - yesterday == 1" rule. For computing things like duration of employee service, you need the "today - yesterday == 2" rule -- on the assumption that service counts from start of business yesterday to close of business today. So hire date of 1 Feb to fire date of (last day of Feb) would count as one month. > > > Below is some code from the ancient times when everybody and his dog > > each had their own date class :-) > [...] > > Wow. I'm speechless. (any reason you didn't want to use the calendar > module?) Sorry, I don't understand. Why are you speechless? What would I want to use the calendar module for? Apart from the leap() function and the table of days in a month, the calendar module doesn't have any of the functionality that one would expect in a general-purpose date class. -- http://mail.python.org/mailman/listinfo/python-list