time.mktime problem

M

McBooCzech

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?


Thanks for your comments

Petr Jakes
 
E

Edvard Majakari

McBooCzech said:
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!

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

McBooCzech

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:(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
 
E

Edvard Majakari

===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:(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:
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!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top