10 examples of 'add timezone to datetime python' in Python

Every line of 'add timezone to datetime python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
60def set_datetime_timezone_utc(dt):
61 return pytz.utc.localize(dt)
10def convert_timezone(datetime):
11 """Converts datetime to timezone aware datetime.
12
13 Retrieving timezone:
14
15 - get `timezone` from .ini settings
16 - default to system timezone
17
18 """
19 if datetime is None:
20 return None
21
22 timezone = get_current_registry().settings.get('timezone', None)
23 if not timezone:
24 timezone = time.tzname[0]
25 return pytz.timezone(timezone).localize(datetime)
44def datetime_utc_to_datetime_local(datetime_utc, local_zone = None):
45 if local_zone is None :
46 from dateutil import tz
47 # Hardcode local zone
48 # local_zone = tz.gettz('America/Chicago')
49 # or Auto-detect local zone
50 local_zone = tz.tzlocal()
51 # Tell the datetime object that it's in local time zone since
52 # datetime objects are 'naive' by default
53 return datetime_utc.replace(tzinfo=local_zone)
196def rebase_to_timezone(datetime):
197 """Convert a datetime object to the blog timezone."""
198 if datetime.tzinfo is None:
199 datetime = datetime.replace(tzinfo=UTC)
200 tzinfo = get_timezone()
201 return tzinfo.normalize(datetime.astimezone(tzinfo))
77def datetime_utc_to_local(utc_dt):
78 """ Convert a UTC datetime to local datetime """
79 # https://stackoverflow.com/a/13287083
80 return utc_dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)
14def remove_tz_helper(datetime_object):
15 return datetime_object.replace(tzinfo=None)
66def fromutc(self, dt):
67 assert dt.tzinfo is self
68 stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
69 args = time.localtime(stamp)[:6]
70 dst_diff = DSTDIFF // SECOND
71 # Detect fold
72 fold = (args == time.localtime(stamp - dst_diff))
73 return datetime(*args, microsecond=dt.microsecond,
74 tzinfo=self, fold=fold)
51def get_local_datetime(current_datetime, timezone):
52 """apply the timezone delta to the datetime and remove the tzinfo"""
53 new_current_datetime = current_datetime
54
55 localized_current_datetime = timezone.localize(current_datetime, is_dst=False)
56 if localized_current_datetime.utcoffset():
57 new_current_datetime = current_datetime + localized_current_datetime.utcoffset()
58 new_current_datetime = new_current_datetime.replace(tzinfo=None)
59
60 return new_current_datetime
120def get_local_time(dt: datetime.datetime = None, tz_default=UTC):
121 """
122 :param dt: 为None时,返回当前时间
123 :param tz_default: dt无时区信息时的默认时区
124 :return:
125 """
126 if dt is None:
127 dt = get_utc_time()
128 return convert_zone(dt, LocalTimeZone, tz_default)
90def localize(self, dt):
91 return datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute,
92 dt.second, dt.microsecond, tzinfo=self)

Related snippets