I need create a variable that is the GMT offset where it's 7:XX AM right now
I've been working on this for a bit and not seeing the answer quite yet. Essentially, I need to add a conditional depending on a GMT of开发者_开发知识库fset into a script. The GMT offset needs to be wherever in the world it is 7 AM at the time the script runs.
In other words, if it's 7 AM in New York, I need to write a python script that will return '-4'.
Any ideas?
UPDATE: A script like this would work most of the time:
7 - time.gmtime().tm_hour
but when it's 11 PM (23) in GMT, the return is -15, and there isn't a GMT offset for -15 (only goes to -12).
def OfsToHour(targetHour = 7):
import datetime
t = datetime.datetime.utcnow() #or your favored UTC call
return (((targetHour - t.hour) + 12) % 24) - 12
If it's 07:00 in New York, then it's 11:00 GMT. 7 - 11 = -4.
If it's 07:00 in Los Angeles, then it's 14:00 GMT. 7 - 14 = -7.
So take the current time in GMT, round it to the hour of your choice, and subtract from 7.
[update]
Ah, right, there is no -15... But it all works mod 24. So take 7-time.gmtime().tm_hour
, and if it is less than -12, add 24 to get the right answer.
[Sorry, but I do not actually know Python :-)]
精彩评论