7 examples of 'python get current timezone' in Python

Every line of 'python get current timezone' 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
this disclaimer
7def get_current_time_zone():
8 return time.altzone if time.localtime().tm_isdst else time.timezone
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

223def current_tz_string():
224 """ returns padded string UTC offset for current server
225 +/-xxx
226 """
227 global _tz_string
228 if _tz_string is not None: return _tz_string
229 offset = time.timezone if (time.localtime().tm_isdst==0) else time.altzone
230 offset = -1*offset
231 ohour = abs(int(offset/3600))
232 omin = int(((abs(offset))-ohour*3600)%60)
233 if offset>0:
234 _tz_string = "+%s:%s" % ('{0:>02d}'.format(ohour),
235 '{0:>02d}'.format(omin))
236 else:
237 _tz_string = "-%s:%s" % ('{0:>02d}'.format(ohour),
238 '{0:>02d}'.format(omin))
239 return _tz_string
117@cached_property
118def timezone(self):
119 """
120 Return a tzinfo of the database connection time zone.
121
122 This is only used when time zone support is enabled. When a datetime is
123 read from the database, it is always returned in this time zone.
124
125 When the database backend supports time zones, it doesn't matter which
126 time zone Django uses, as long as aware datetimes are used everywhere.
127 Other users connecting to the database can choose their own time zone.
128
129 When the database backend doesn't support time zones, the time zone
130 Django uses may be constrained by the requirements of other users of
131 the database.
132 """
133 if not settings.USE_TZ:
134 return None
135 elif self.settings_dict['TIME_ZONE'] is None:
136 return timezone.utc
137 else:
138 return pytz.timezone(self.settings_dict['TIME_ZONE'])
69def print_timezone():
70 print(get_timezone())
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)
32def timezone(request):
33 current_timezone = None
34 detected_timezone = request.session.get("detected_timezone", None)
35
36 if request.session.get("timezone", None):
37 current_timezone = request.session['timezone']
38
39 if request.user.is_authenticated() and request.user.get_profile().timezone:
40 if current_timezone != request.user.get_profile().timezone:
41 request.user.get_profile().timezone = current_timezone
42 request.user.get_profile().save()
43
44 if request.GET.get("tzoffset", None):
45 offset = 0 - int(request.GET['tzoffset'])
46 tz = None
47
48 for name, code in tznames.items():
49 zoneoffset = pytzone(code).utcoffset(datetime.now()).seconds
50 if zoneoffset > 43200: # over 12 hours: need to wrap arond
51 zoneoffset = zoneoffset - 86400 # 24 hours * 60 minutes * 60 seconds
52 zoneoffset = zoneoffset / 60 # seconds to minutes
53 if zoneoffset == offset:
54 tz = name
55 break
56
57 if not tz:
58 offset = int(offset) / -60
59 if offset > 0:
60 tz = 'Etc/GMT+%s' % offset
61 elif offset < 0:
62 tz = 'Etc/GMT%s' % offset
63 else:
64 tz = 'UTC'
65
66 request.session['detected_timezone'] = tz
67 if not current_timezone:
68 request.session['timezone'] = tz
69 current_timezone = tz
70
71 return {'current_timezone': current_timezone,
72 'detected_timezone': detected_timezone,
73 'timezones': timezones}
31def _try_tz_from_env():
32 tzenv = os.environ.get('TZ')
33 if tzenv:
34 try:
35 return _tz_from_env(tzenv)
36 except pytz.UnknownTimeZoneError:
37 pass

Related snippets