# Caculate age

Manuel M. Garcia mail at manuelmgarcia.com
Sun Feb 2 03:31:27 CET 2003

```On 1 Feb 2003 14:51:18 -0800, benvpatel at yahoo.com (Ben) wrote:

>I am new at Python. I have an assignment to be written by tomorrow in Python.
>- Calculate a person's age based on a brithdate in any format

3 ways to do it

1) use Python's built in 'time' module.  This is a crummy way, but it
may be the only way your teacher accepts, because it uses only
built-ins.   Cutting and pasting from some fiscal calendar code I
wrote that does everything the hard way (blech!), I can give you some
hints (some variable names changed or deleted to protect the
innocent):

x = time.mktime(
(year, month, day, 12, 0, 0, 0, 1, -1) )

t = time.localtime(y)

# 86400.0 = 24 * 60 * 60 * 1.0

int(round( a + (b - c) / 86400.0 ))

c + (d - a) * 86400.0

re.compile(r"""
(\d+) \/ (\d+) \/ (\d+) \$ | # dd/dd/dd
(\d+) \- (\d+) \- (\d+) \$   # dd-dd-dd
""", re.VERBOSE)

m = re0.match(s)

int(m.expand(r'\3'))
int(m.expand(r'\1'))
int(m.expand(r'\2'))

Start by reading the documentation on the built-in modules 're' and
'time'.  By the time you are comfortable with both, you will be a 2
weeks late for this assignment, but you will know a lot more Python
before the midterm ;-)

This way really blows.  Better write the code on the same setup your
teacher will use to check it, because how mktime interprets tuples is
*highly* variable from OS to OS, from compiler to compiler.

Also the C library is only guaranteed to work between 1970 and 2038.
I would consider this unacceptable to do birth date arithmetic for
humans.  And don't even get me started on leap seconds...

2) use Zope's pure Python implementation of datetime.  It runs at
Python speed, but you can use it with Python 2.2:

http://cvs.zope.org/Zope3/src/datetime/

docs are at

http://www.python.org/dev/doc/devel/lib/module-datetime.html

I am guessing using the C extension 'datetime' in a build of Python
2.3 alpha is out of the question.

Nice, nice, nice!  Much better than using 'time'.  It didn't exist
when I wrote my code, however :-(

3) use mxDateTime, a C extension that is the fastest way to do it in
Python 2.2

http://www.lemburg.com/files/python/mxDateTime.html

Nice, nice, nice!  Much better than using 'time'.  I would have used
mxDateTime instead of the 'time', if I didn't have so many client
machine to support, and if the program wasn't swamped by I/O, so that
doing everything the hard slow way wasn't an issue.

Frankly, you may be beyond help (you started a bit late), but I am
hoping this will help somebody who is about to code up their company's
fiscal calendar ;-)

<RANT>

Frankly, this assignment reminds me of some of the stupid assignments
I had in programming classes in college.  The teacher would blurt out
an idea off the top of his head, but to really write a robust version
of the program would be many programmer hours (let the user use any
sensible date format, check for ambiguous dates, account for
differences in C libraries, using on built-ins, leap seconds, etc.).
I would always write a program much longer and polished than the
professor's, but my code would have the benefit of actually working
with real-world data, on the full range of compilers used to grade the
programs, and with the program helping the user with deficiencies in
their input.  It was typical for the professors to be unable to
anticipate any of these problems, and the people who suffered the most
were the kids in the class who didn't already know how to program.
The professors were unable to verbalize what things were outside of
the scope of what they expected from the program.  I would expect this
from end users from the marketing department in a business setting,
but these guys represented themselves as university professors of
computer science.  The students who really needed help learning,
instead of being led through progressively harder concepts in
manageable pieces, those students ended up puzzling through the code
of the class's gurus, trying to change the code enough so it didn't
look like a copy, but not enough to dump core.  The kids who had been
programming for years got A's, and the rest of the class got C's or
worse, and headaches to boot.  I guess now with grade inflation, the
lower range of the grades would be a B-, but woe to the B- graduates
who were expected to perform on the first day of their programming
job.  I have interviewed programming applicants, gave them 45 minutes
to do something I could do in less than 10, sat them down on a
computer where I disabled Internet access and no access to code to
cut-and-paste from, and watched as they sat motionless, dripping flop
sweat, until I mercifully called time.

</RANT>

Anyway, it is nice to see people learning to program in Python, so
they can concentrate on algorithms and concepts, instead of trying to
keep a compiler happy.

Manuel

```