0

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
fear_matrix
  • 4,912
  • 10
  • 44
  • 65
  • Have you tried anything? please include what you have tried in here – GLaDOS Mar 03 '16 at 08:37
  • What type is the time: a) a string, b) a datetime, c) a timezone aware datetime, d) something else? – Alexander Mar 03 '16 at 08:37
  • What is timesplithour? is that a string? datetime? please include the initialization of the variables. – GLaDOS Mar 03 '16 at 08:41
  • If you want to get the current time in the given timezone then avoid string operations, get the time directly e.g.: [`datetime.now(pytz.timezone('Asia/Calcutta'))`](http://stackoverflow.com/a/5096669/4279) – jfs Mar 03 '16 at 12:47

5 Answers5

2

You could use Python's datatime 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.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
1

Use the datetime module:

from datetime import datetime, timedelta

dt = datetime.strptime('13:11:06', '%H:%M:%S')
time_gmt = (dt - timedelta(hours=5, minutes=30)).time()

print(time_gmt.hour)
print(time_gmt.minute)
print(time_gmt.second)
s = time_gmt.strftime('%H:%M:%S')
print(s)

Output

7
41
6
07:41:06

Note that this subtracts 5 hours and 30 minutes as initially mentioned in the question. If you really only want to subtract 5 hours, use timedelta(hours=5).

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

You can use datetimes timedelta.

print datetime.datetime.today()
>>> datetime.datetime(2016, 3, 3, 10, 45, 6, 270711)
print datetime.datetime.today() - datetime.timedelta(days=3)
>>> datetime.datetime(2016, 2, 29, 10, 45, 8, 559073)

This way you can subtract easily

GLaDOS
  • 620
  • 6
  • 17
0

Assuming the time is a datetime instance

import datetime as dt
t = datetime(2015,12,31,13,11,06)
#t.time() # gives time object. ie no date information

offset = dt.timedelta(hours=5,minutes=30) # or hours=5.5

t -= offset    

t.strftime(("%H:%M:%S") # output as your desired string
#'18:41:06'
M.T
  • 4,917
  • 4
  • 33
  • 52
0

If the object is datetime and you don't care about DST, the simplest thing you can do is,

In [1]: from datetime import datetime

In [2]: curr = datetime.now()

In [3]: curr
Out[3]: datetime.datetime(2016, 3, 3, 9, 57, 31, 302231)

In [4]: curr.utcnow()
Out[4]: datetime.datetime(2016, 3, 3, 8, 57, 57, 286956)
Abhinav
  • 992
  • 2
  • 11
  • 26