Skip to content

Commit 026a5e0

Browse files
author
Neil Grey
committed
For #35260 : Adding classmethods to SgTimezone for nested class backwards compatability, as well as docstrings to better explain the module
1 parent f60c31b commit 026a5e0

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

shotgun_api3/lib/sgtimezone.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import time as _time
1010

1111
class SgTimezone(object):
12-
12+
'''
13+
Shotgun's server infrastructure is configured for Coordinated Universal
14+
Time (UTC). In order to provide relevant local timestamps to users, we wrap
15+
the datetime module's tzinfo to provide convenient conversion methods.
16+
'''
17+
1318
ZERO = timedelta(0)
1419
STDOFFSET = timedelta(seconds = -_time.timezone)
1520
if _time.daylight:
@@ -21,8 +26,28 @@ class SgTimezone(object):
2126
def __init__(self):
2227
self.utc = UTC()
2328
self.local = LocalTimezone()
29+
30+
@classmethod
31+
def UTC(cls):
32+
'''
33+
For backwards compatibility, from when UTC was a nested class,
34+
we allow instantiation via SgTimezone
35+
'''
36+
return UTC()
37+
38+
@classmethod
39+
def LocalTimezone(cls):
40+
'''
41+
For backwards compatibility, from when LocalTimezone was a nested
42+
class, we allow instantiation via SgTimezone
43+
'''
44+
return LocalTimezone()
2445

2546
class UTC(tzinfo):
47+
'''
48+
Implementation of datetime's tzinfo to provide consistent calculated
49+
offsets against Coordinated Universal Time (UTC)
50+
'''
2651

2752
def utcoffset(self, dt):
2853
return SgTimezone.ZERO
@@ -34,25 +59,41 @@ def dst(self, dt):
3459
return SgTimezone.ZERO
3560

3661
class LocalTimezone(tzinfo):
62+
'''
63+
Implementation of datetime's tzinfo to provide convenient conversion
64+
between Shotgun server time and local user time
65+
'''
3766

3867
def utcoffset(self, dt):
68+
'''
69+
Difference between the user's local timezone and UTC timezone in seconds
70+
'''
3971
if self._isdst(dt):
4072
return SgTimezone.DSTOFFSET
4173
else:
4274
return SgTimezone.STDOFFSET
4375

4476
def dst(self, dt):
77+
'''
78+
Daylight savings time (dst) offset in seconds
79+
'''
4580
if self._isdst(dt):
4681
return SgTimezone.DSTDIFF
4782
else:
4883
return SgTimezone.ZERO
4984

5085
def tzname(self, dt):
86+
'''
87+
Name of the user's local timezone, including a reference
88+
to daylight savings time (dst) if applicable
89+
'''
5190
return _time.tzname[self._isdst(dt)]
5291

5392
def _isdst(self, dt):
93+
'''
94+
Calculate whether the timestamp in question was in daylight savings
95+
'''
5496
tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1)
55-
import time as _time
5697
stamp = _time.mktime(tt)
5798
tt = _time.localtime(stamp)
5899
return tt.tm_isdst > 0

0 commit comments

Comments
 (0)