Time-date as an integer

Jeff Epler jepler at unpythonic.net
Tue Aug 24 14:45:54 EDT 2004


datetime.utcnow() is implemented in terms of gettimeofday, for platforms
that have it.  So is time.time().

I suggest that you use
    def calcNodeId(self): return long(time.time() * 1e6)

Note that on this system (a 2.4GHz desktop PC), I can write a Python
program that calls time() close enough together to get the same timestamp
twice, though calls to calcNodeId are consistently taking over 1usec.
If you're relying on this method to give you unique identifiers on
a system that will run a few years from now---or even a machine just
50% faster than this one, or after the next, faster version of Python
is released--- with identifiers being generated at full tilt, you'll
probably be disappointed.

    def main():
        from time import time
        min_diff = 1
        min_t = None
     
        for i in range(1000):
            t = time(), time()
            diff = t[1]-t[0]
            if diff < min_diff:
                min_t = t
                min_diff = diff
     
        print min_diff, min_t
     
    main()

    $ python mindiff.py
    0.0 (1093372528.113651, 1093372528.113651)
    $ python mindiff2.py # version using calcNodeId
    1 (1093372993811409L, 1093372993811410L)

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040824/ed0ce610/attachment.sig>


More information about the Python-list mailing list