Substract current time to -GMT in python
Question: I have a time which is 13:11:06 and i want to -GMT (i.e -0530). I can minus it
by simply doing -5 by splitting the string taking the first digit (convert to
int) and then minus it and then re-join. But then i get it in a format which
is 8:11:06 which is not right as it should be 08:11:06, secondly its a lengthy
process. Is there a easy way to get my time in -GMT format (08:11:06)

This is what i did to get -GMT time after getting the datetime

    
    
    timesplithour = int(timesplit[1]) + -5
    timesplitminute = timesplit[2]
    timesplitseconds = timesplit[3]
    print timesplithour
    print timesplitminute
    print timesplitseconds
    print timesplithour + ":" + timesplitminute + ":" + timesplitseconds
    


Answer: You could use Python's
[`datatime`](https://docs.python.org/3.5/library/datetime.html?highlight=timedelta#module-
datetime) library to help you as follows:

    
    
    import datetime
    
    my_time = "13:11:06"
    new_time = datetime.datetime.strptime("2016 " + my_time, "%Y %H:%M:%S") - datetime.timedelta(hours=5, minutes=30)
    print new_time.strftime("%H:%M:%S")
    

This would print:

    
    
     07:41:06
    

First it converts your string into a `datetime` object. It then creates a
`timedelta` object allowing you to subtract 5 hours 30 minutes from the
datetime object. Finally it uses `strftime` to format the resulting datetime
into a string in the same format.

