10 examples of 'python get timezone' in Python

Every line of 'python get 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

134def tzname(self, _):
135 return self.name
25def tzname(self, dt):
26 '''
27 Return the time zone name corresponding to the datetime object dt,
28 as a string.
29 '''
30 return "UTC"
34def _timezone(utc_offset):
35 '''
36 Return a string representing the timezone offset.
37
38 >>> _timezone(0)
39 '+00:00'
40 >>> _timezone(3600)
41 '+01:00'
42 >>> _timezone(-28800)
43 '-08:00'
44 >>> _timezone(-1800)
45 '-00:30'
46 '''
47 # Python's division uses floor(), not round() like in other languages:
48 # -1 / 2 == -1 and not -1 / 2 == 0
49 # That's why we use abs(utc_offset).
50 hours = abs(utc_offset) // 3600
51 minutes = abs(utc_offset) % 3600 // 60
52 sign = (utc_offset < 0 and '-') or '+'
53 return '%c%02d:%02d' % (sign, hours, minutes)
38def parse_timezone(tzspec):
39 if tzspec[0] in "+-":
40 offset = int(tzspec[1:3], 10) * 60 + int(tzspec[3:5], 10)
41 offset *= (-1 if tzspec[0] == "-" else 1)
42 return FixedOffset(offset, tzspec)
43 elif tzspec[0] == ":":
44 print("ignoring unimp", tzspec)
45 return UTC()
46 else:
47 return UTC()
60def set_datetime_timezone_utc(dt):
61 return pytz.utc.localize(dt)
274def get_tz(df):
275 index = df.index
276 try:
277 tz = index.tz
278 except AttributeError:
279 tz = None
280 return tz
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'])
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)
247def astimezone(self, tz=None):
248 d = _underlying_datetime_type.astimezone(self, tz)
249 return _original_datetime_type.__new__(type(self), d)

Related snippets