469,645 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

finding time difference

This is probably a very basic question, how do I find out the difference between two system time.

I tried
x1 = t1.clock
x2 = t2.clock
x2 - x1
doesn't seem to give me the difference.

Also trying this way ,

x = time.localtime()
y = time.localtime()

tmp_time = str(x[3])+ ':' +str(x[4]) + ':' + str(x[5])

gives me output as 11:34:5 but I cannot find the difference between these two as it gives me the following error

y-x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'

Any tips ??

-V
Mar 8 '07 #1
1 7149
bartonc
6,596 Expert 4TB
This is probably a very basic question, how do I find out the difference between two system time.

I tried
x1 = t1.clock
x2 = t2.clock
x2 - x1
doesn't seem to give me the difference.

Also trying this way ,

x = time.localtime()
y = time.localtime()

tmp_time = str(x[3])+ ':' +str(x[4]) + ':' + str(x[5])

gives me output as 11:34:5 but I cannot find the difference between these two as it gives me the following error

y-x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'

Any tips ??

-V
datetime objects can be added and subtracted. The result is a timedelta object.
In version 2.4.4 of the Python Library Reference it's in section 6.10.
datetime2 = datetime1 + timedelta (1)
datetime2 = datetime1 - timedelta (2)
timedelta = datetime1 - datetime2 (3)

(1)
datetime2 is a duration of timedelta removed from datetime1, moving forward in time if timedelta.days > 0, or backward if timedelta.days < 0. The result has the same tzinfo member as the input datetime, and datetime2 - datetime1 == timedelta after. OverflowError is raised if datetime2.year would be smaller than MINYEAR or larger than MAXYEAR. Note that no time zone adjustments are done even if the input is an aware object.


(2)
Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same tzinfo member as the input datetime, and no time zone adjustments are done even if the input is aware. This isn't quite equivalent to datetime1 + (-timedelta), because -timedelta in isolation can overflow in cases where datetime1 - timedelta does not.

(3)
Subtraction of a datetime from a datetime is defined only if both operands are naive, or if both are aware. If one is aware and the other is naive, TypeError is raised.
If both are naive, or both are aware and have the same tzinfo member, the tzinfo members are ignored, and the result is a timedelta object t such that datetime2 + t == datetime1. No time zone adjustments are done in this case.

If both are aware and have different tzinfo members, a-b acts as if a and b were first converted to naive UTC datetimes first. The result is (a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None) - b.utcoffset()) except that the implementation never overflows.
Mar 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Robizzle | last post: by
15 posts views Thread by Benjamin Rutt | last post: by
11 posts views Thread by srkkreddy | last post: by
4 posts views Thread by David Warner | last post: by
22 posts views Thread by Annajiat | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.