By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

time.mktime problem

P: n/a
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

Aug 30 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Aug 30 '05 #2

P: n/a
"McBooCzech" <pe**@tpc.cz> 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";
Aug 30 '05 #3

P: n/a
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

Aug 30 '05 #4

P: n/a
"McBooCzech" <pe**@tpc.cz> 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";
Sep 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.