-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtzutils.py
More file actions
58 lines (40 loc) · 1.51 KB
/
tzutils.py
File metadata and controls
58 lines (40 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Time zones from UTC offsets
Copyright (c) 2019 The University of Texas at Austin. All rights reserved.
Use and redistribution of this file is governed by the license terms in
the LICENSE file found in the project's top-level directory.
"""
import datetime
ZERO = datetime.timedelta(0)
class FixedOffsetTZInfo(datetime.tzinfo):
"""Time zone that is a fixed offset in minutes from UTC."""
def __init__(self, offset_string):
self.__offset = datetime.timedelta(
hours=int(offset_string[0:3]),
minutes=int(offset_string[0] + offset_string[3:5]))
def __repr__(self):
return 'FixedOffsetTZInfo(\'' + self.offsetstring() + '\')'
def __str__(self):
return self.offsetstring()
def __getinitargs__(self):
return (self.offsetstring(),)
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return None
def dst(self, dt):
return ZERO
def offsetstring(self):
offset_min = int(
self.__offset.seconds / 60.0 + self.__offset.days * 24.0 * 60.0)
offset_hr = int(offset_min / 60.0)
offset_min = abs(offset_min) % 60
return '{:+03}{:02}'.format(offset_hr, offset_min)
tz_cache = {}
def get_tz_for_offset(offset_string):
""""Get a tzinfo for an offset, either a cached instance or new"""
if offset_string in tz_cache:
return tz_cache[offset_string]
else:
tz = FixedOffsetTZInfo(offset_string)
tz_cache[offset_string] = tz
return tz