Search code examples
pythontimeutc

How to get UTC time given timezone?


I am in the US and I want to get the UTC time (without the effect of Daylight Saving Time) for a piece of code:

localtime = strftime("%m%d%y%H%M", gmtime())

Right now that code give me time in Greenwich, England. I know that I have a time offset of -8 (GMT -8). How can I get the UTC time to a specific timezone? (using Python library, not casting the hour to integer, - 8, and than convert it back)


Solution

  • Try the datetime module:

    datetime.datetime.utcnow() + datetime.timedelta(hours=-8)
    

    BTW, UTC doesn't have timezones, it's Universal Co-ordinated Time, it's the same everywhere.