I need to convert a time given by the user (as a string) to UTC depending on the timezone the user is in. So all I need is the offset of the timezone (considering daylight saving time) to UTC. The problem seems to be, that there is no builtin timezone info in python, so I need to require pytz (which I'd rather not). Is there an easier way?
from datetime import datetime
import pytz
TIME_FORMAT = '%H:%M:%S'
TIMEZONE = 'Europe/Berlin'
def utctime_for_timestring(timestring, tz=TIMEZONE, tfmt=TIME_FORMAT):
t = datetime.strptime(timestring, tfmt)
offset = datetime.now(pytz.timezone(tz)).utcoffset()
return (t - offset).time()
# >>> utctime_for_timestring('20:00:00')
# datetime.time(18, 0)