user3525290
user3525290

Reputation: 1607

How to convert string to gmt time

I am getting a string that I need converted into GMT time. I think this is correct but I am unsure.

t = 'Jan 17 2003 12:00:05:000 AM'
t = datetime.datetime.strptime(t,'%b %d %Y %I:%M:%S:%f %p')

It prints 2003-01-17 00:00:05, which I believe is correct. But how do I determine if this is GMT? I am unsure if there is a way to convert the time on the strptime line.

Upvotes: 0

Views: 617

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

In the question as you present it, there's no way to tell which timezone the time is for. Which makes sense - If I tell you "January 17, 2003, 5 minutes after midnight", you would have no idea which timezone I was talking about. You can maybe assume that the given time applies to the timezone you're currently in, but that's dangerous and, due to the increasing frequency of cloud execution, likely to give the wrong answer a lot of the time (as the server may be in a different timezone from the user).

One way you might be able to confirm that the given time is GMT is to cross-reference it relative to another event whose time you know is in GMT, and then you can determine its timezone based on the difference. Another way you might be able to figure out the timezone is to ask whoever's providing the date (if it's user input; you'll note that many websites have you input your timezone when you're creating an account, for this reason), or determine the location of whatever entity is providing the date (if possible), or find some other way to get that information. It's impossible to give more specific advice without knowing where the date is supposed to come from.

The IANA provides a database of existing timezones to help with actually converting from one timezone to another (since it's not always as simple as changing an hour forward or back). This answer also provides some insight on converting datetime objects from one timezone to another.

Upvotes: 2

Related Questions