Velocity Reviews > time.mktime problem

# time.mktime problem

McBooCzech
Guest
Posts: n/a

 08-30-2005
Hi, on Linux (Fedora FC4) and Python 2.4.1
I am trying to know the time delta in seconds between two times given
in the HHMMSS format. My code looks like:

import datetime, time
ta1=(time.strptime('000001', '%H%M%S'))
ta2=(time.strptime('230344', '%H%M%S'))
t1=time.mktime(ta1)
t2=time.mktime(ta2)
print t1, t2

-2147483648.0 -2147483648.0

I just can not figure out, why the t1 and t2 are the same?

Petr Jakes

Matt Hammond
Guest
Posts: n/a

 08-30-2005
I don't get the same results:

>>> import datetime, time
>>> ta1=(time.strptime('000001', '%H%M%S'))
>>> ta2=(time.strptime('230344', '%H%M%S'))
>>> t1=time.mktime(ta1)
>>> t2=time.mktime(ta2)
>>> print t1, t2

-2208988799.0 -2208905776.0
>>> print t1-t2

-83023.0

Suse 9.3, python 2.4 (all 64bit)

Matt

--

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.

Edvard Majakari
Guest
Posts: n/a

 08-30-2005
"McBooCzech" <(E-Mail Removed)> writes:

> Hi, on Linux (Fedora FC4) and Python 2.4.1
> I am trying to know the time delta in seconds between two times given
> in the HHMMSS format. My code looks like:
>
> import datetime, time
> ta1=(time.strptime('000001', '%H%M%S'))
> ta2=(time.strptime('230344', '%H%M%S'))
> t1=time.mktime(ta1)
> t2=time.mktime(ta2)
> print t1, t2
>
> -2147483648.0 -2147483648.0
>
> I just can not figure out, why the t1 and t2 are the same?

Hm. You are trying to convert (1900, 1, 1, 0, 0, 1, 0, 1, -1) to epoch.
However, epochs start from 1970-01-01 00:00. So that at least is not right.

Hint... see what var ta1 is. With python2.3 you'll get overflow error, becuase
mktime argument is out of range.

--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!

\$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";

McBooCzech
Guest
Posts: n/a

 08-30-2005
according to the Python documentation:
http://docs.python.org/lib/module-time.html

===snip===
Values 100-1899 are always illegal.
..
..
strptime(string[, format])
..
..
The default values used to fill in any missing data are:
(1900, 1, 1, 0, 0, 0, 0, 1, -1)
===snip===

BTW, check the following code:
>>import datetime, time
>>print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

(1901, 12, 13, 20, 45, 52, 4, 347, 0)

but (1900, 1, 1, 0, 0, 0, 0, 1, -1) is (IMHO) expected.... Hmmm. But I
am just a newbie!!!

Anyway, maybe I am just using a wrong way how to calculate time delta
between two time values given in the format "HHMMSS".

Does Python provide some other ways how to calculate it?

Petr Jakes

Edvard Majakari
Guest
Posts: n/a

 09-05-2005
"McBooCzech" <(E-Mail Removed)> writes:

> ===snip===
> Values 100-1899 are always illegal.
> .
> .
> strptime(string[, format])
> .
> .
> The default values used to fill in any missing data are:
> (1900, 1, 1, 0, 0, 0, 0, 1, -1)
> ===snip===
>
> BTW, check the following code:
>>>import datetime, time
>>>print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

> (1901, 12, 13, 20, 45, 52, 4, 347, 0)
>
> but (1900, 1, 1, 0, 0, 0, 0, 1, -1) is (IMHO) expected.... Hmmm. But I
> am just a newbie!!!

You are comparing apples and oranges here. You checked documentation of
strptime, and the problem is in the use of time.mktime().

The point: time.mktime() returns Epoch time (seconds since 1970) and you are
passing it a tuple which is (way before) 1970. There is no such thing as
negative epoch. It is like computing packaging day of milk which hasn't been
milked from the cow yet

I really wonder what version of Python you are running:

>>> import datetime, time
>>> print time.gmtime(time.mktime((1900, 1, 1, 0, 0, 0, 0, 1, -1)))

Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: mktime argument out of range

Python 2.3 and 2.4 both give the same error. As for the python version 2.2, no
datetime module was implemented.

--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!

\$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";