Skip to content

Instantly share code, notes, and snippets.

@lov3catch
Created July 20, 2015 11:27
Show Gist options
  • Save lov3catch/d2b0aa54a7b7ff23e294 to your computer and use it in GitHub Desktop.
Save lov3catch/d2b0aa54a7b7ff23e294 to your computer and use it in GitHub Desktop.
lng lat to timezone, converting time to timezone
from tzwhere import tzwhere
time_zone_getter = tzwhere.tzwhere()
def convert_time_for_me(self, datetime_obj):
'''
getting datetime obj and convert to datetime with same timezone
'''
def time_zone(me):
lng, lat = db.session.query(ST_X(me.location)).first()[0], db.session.query(ST_Y(me.location)).first()[0]
tz = time_zone_getter.tzNameAt(lat, lng)
return pytz.timezone(tz)
def utc_to_local(utc_dt, user_local_tz):
local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(user_local_tz)
dt = user_local_tz.normalize(local_dt)
return dt.strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')
tz = time_zone(self)
return utc_to_local(datetime_obj, tz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment