You should replace all this logic with
```Python
t = parse_time(time)
```
because parse_time will cope with datetime as an input as well as strings.