time-module questions

Harald Hanche-Olsen hanche at math.ntnu.no
Fri Apr 7 05:12:48 CEST 2000


+ "Warren Postma" <embed at geocities.com>:

| First, it appears that time.localtime returns fractions of a second
| in it's values, but that time.mktime does not add this millisecond
| component back in, nor does it appear to be part of the time-tuple
| returned by time.localtime, and time.gmtime:

The time module is little more than an interface to the standard C
library, which does not make provisions for fractions of a second.  If
you wish to preserve that information, you'll have to hack it
yourself, or perhaps Marc-André Lemburg's mxDateTime module has the
answers.

In fact, ``use mxDateTime'' appears to be the suggested solution to
every time/date-related problem in python.  I downloaded it but still
haven't had the time to check it out carefully, so I can't say for
sure whether it solves your problem.

| Secondly, mktime accepts localtimes only, how to I take y/m/d/h/m/s
| in GMT instead of the current timezone, and convert back to a
| time.time() type value (float).

Yes, mktime is the inverse of localtime.  You need the inverse of
gmtime, which is not in the standard C library, and hence not in the
time module.  Again, you have to hack it yourself, with a *large*
probability of getting it wrong, or use the mxDateTime module, which
seems to have functions for converting between UTC and localtime.

I once looked at the code in the GNU date program to see how it coped
with the situation, and it's not pretty.  After staring at the code
for half an hour, I found myself unable to prove its correctness,
though it performed correctly on all times that I threw at it (using
date -d '<some date and time> UTC'), even around the times when DST
starts or stops.

Normally I am kind of a peaceful guy, but if I ever meet the designers
of the standard C time conversion functions, I am not sure what I'll
do.  Fortunately I am not very big and strong, and I don't carry any
weapons -- so I'll probably settle for some choice verbal abuse, which
is unlikely to land me in jail.  8-)
-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- "There arises from a bad and unapt formation of words
   a wonderful obstruction to the mind."  - Francis Bacon



More information about the Python-list mailing list