Can't subtract departure time from current time


#1

Hi Guys,

So I’am having a slight problem with the departure time in the trip planner api. I am trying to get the time until departure to print. To do this i am subtracting the current time from departure time. However, when I do this it gives me the error:

Traceback (most recent call last):
  File "C:\Python27\curl test v2.1.py", line 26, in <module>
    tdelta = datetime.strptime(timesTD, FMT) - realtime
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

I am unsure how to fix this and any help would be appreciated. Thanks very much. here is my code:

import requests, json
import os
import time
from datetime import datetime
FMT = '%H:%M'



busNumbers = ''
str(busNumbers)

url = 'https://api.transport.nsw.gov.au/v1/tp/departure_mon?outputFormat=rapidJSON&coordOutputFormat=EPSG%3A4326&mode=direct&type_dm=stop&name_dm=10137620&itdDate=20180112&itdTime=1220&departureMonitorMacro=true&TfNSWDM=true&version=10.2.1.42'
payload = ''

headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'Authorization': 'apikey ' }
r = requests.get(url, data=payload, headers=headers).json()

for buses in r['stopEvents'][0:6]:
    busNumbers = busNumbers + (buses['transportation']['disassembledName']) + ' '
print(busNumbers)

for times in r['stopEvents'][0:6]:
    timesTD = (times['departureTimePlanned'][11:16])
    realtime = datetime.now().strftime('%H:%M')
    print(realtime)
    tdelta = datetime.strptime(timesTD, FMT) - realtime

#2

I’ll try and give you a better answer when I get home, but you can use combine as seen in this StackOverflow thread https://stackoverflow.com/questions/5259882/subtract-two-times-in-python to get the departure time.