-3
pol = True
if GPIO.input(inPut) == 1:
   while pol == True:
      start_time = time.localtime()
      start_time_format = time.strftime("%H:%M:%S:", start_time)
      pol == False
else:
   pol == True
current_time = time.localtime()
current_time_format = time.strftime("%H:%M:%S:", current_time)
work_time = current_time_format - start_time_format
print work_time

when i run it i got this error

     work_time = current_time_format - start_time_format
 TypeError: unsupported operand type(s) for -: 'str' and 'str'

how can i subtract current_time_format from start_time_format ? any correction for this ? thanks in advance

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

2 Answers2

1

Issue:

The main issue with your code is that you are trying to substract a variable that it is of type string from another one which is of type string too, and this is not possible (at least in this manner) because the class doesn't support this kind of operand

work_time = current_time_format - start_time_format

and so you get the error below:

work_time = current_time_format - start_time_format
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Solution:

Althought I don't know what your overall code is or what you really want to achive with it (apart from getting the work_time), one way of doing this could be by using deltatime instead of time, like this:

from datetime import datetime

# ...

pol = True
if GPIO.input(inPut) == 1:
   while pol == True:
      start_time = datetime.now() 
      pol == False
else:
   pol == True

current_time = datetime.now()
work_time = (current_time - start_time) # or 
# work_time = datetime.fromtimestamp((current_time - start_time).seconds).strftime("%H:%M:%S")

print work_time

References:

Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
0

thanks for all responses i solved my issue by this method :-

import time
import RPi.GPIO as GPIO
import ast
...
pol = True
if GPIO.input(inPut) == 1:
    while pol == True:
      start_time = time.localtime()
      global houre
      houre = time.strftime("%H", start_time)
      global  minute
      minute = time.strftime("%M", start_time)
      global second
      second = time.strftime("%S", start_time)
      #time.sleep(5)
      pol = False
else:
    pol = True
current_time = time.localtime()
c_houre = time.strftime("%H", current_time)
c_minute = time.strftime("%M",current_time)
c_second = time.strftime("%S", current_time)

w_houre = ast.literal_eval(c_houre) - ast.literal_eval(houre)
w_minute = ast.literal_eval(c_minute) - ast.literal_eval(minute)
w_second = ast.literal_eval(c_second) - ast.literal_eval(second)

print(w_houre,":",w_minute,":",w_second)

maybe not true method but work for me, thanks for all