UTC equivalent of time.mktime() ?

Mark McEahern marklists at mceahern.com
Wed Apr 17 13:29:51 EDT 2002


[Graham Ashton]
> I appreciate that I could (and thanks for the suggestion), but does this
> really mean that via the standard library there is no easy way to
> convert "1 Jan 1970" to 0 (i.e. string to seconds since epoch)?
>
> Perhaps I should inspect the mxDateTime code...

Anyway, mxDateTime rocks.  I don't know why it's not part of the standard
distribution.

Here's a fer instance on why you oughter use it:

	Windows doesn't have strptime

cygwin:

$ python
Python 2.2 (#1, Dec 31 2001, 15:21:18)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strptime("1 Jan 1970", "%d %b %Y")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'strptime'

plain win:

C:\Python22>python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strptime("1 Jan 1970", "%d %b %Y")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'strptime'

linux:

$ python
Python 2.2.1 (#1, Apr 10 2002, 09:15:55)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strptime("1 Jan 1970", "%d %b %Y")
(1970, 1, 1, 0, 0, 0, 3, 1, 0)

whereas with mxDateTime, you don't even have to bother with the format:

>>> mx.DateTime.DateTimeFrom("1 Jan 1970")
<DateTime object for '1970-01-01 00:00:00.00' at 816d7b8>

Of course, epoch-relative values are still in local time:

>>> epoch = mx.DateTime.DateTimeFrom("1 Jan 1970")
>>> epoch.ticks()
21600.0

You want that to say zero, right, no matter what the local time zone is?

Thinking about UTC vs. localtime always starts to hurt my head...

// m






More information about the Python-list mailing list