finding time difference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indiarocks
    New Member
    • Feb 2007
    • 20

    #1

    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_ti me' and 'time.struct_ti me'

    Any tips ??

    -V
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by indiarocks
    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_ti me' and 'time.struct_ti me'

    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(tzin fo=None) - a.utcoffset()) - (b.replace(tzin fo=None) - b.utcoffset()) except that the implementation never overflows.

    Comment

    Working...