E pur si muove


Tuesday, January 27, 2009

There exists a datetime.datetime.fromtimestamp() and many a time I've wondered why there is no .totimestamp() equivalent. The answer is that not all datetime objects can be represented by a timestamp. But if you know this restriction and want it anyway, this is the magic:


Would be so cool if the docs actually mentioned this.

PS: Both functions have a utc variant too



This solution does not give you sub-second resolution, but otherwise is rather elegant. Funny thing is that the %s specifier is not documented by the stdlib, but seems to exist on the underlying implementations at least on UNIX.

And for completeness, these issues are being discussed in the bug tracker. See issue2736 and issue1673409.

Tuesday, January 27, 2009 | Labels: |


rgz said...

Personally I feel very very very much let down by Python date and time classes. Zope has a far superior DateTime class with lotsa useful methods which you can get prom the pypi

But then again I feel troubled using a third party class from a bloated library for any kind of serious development with aims to portability.

Just now I've discovered that DateTime isn't importing in my home system, it must have something to do with my failed attempts to install jinja2 and pyquery.

I seriously need to use the local user site-packages dir and virtualenv for everything I do but then again, for personal scripts having everything installed globally is very convenient.

New comments are not allowed.

Subscribe to: Post Comments (Atom)