9
9
import time as _time
10
10
11
11
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
+
13
18
ZERO = timedelta (0 )
14
19
STDOFFSET = timedelta (seconds = - _time .timezone )
15
20
if _time .daylight :
@@ -21,8 +26,28 @@ class SgTimezone(object):
21
26
def __init__ (self ):
22
27
self .utc = UTC ()
23
28
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 ()
24
45
25
46
class UTC (tzinfo ):
47
+ '''
48
+ Implementation of datetime's tzinfo to provide consistent calculated
49
+ offsets against Coordinated Universal Time (UTC)
50
+ '''
26
51
27
52
def utcoffset (self , dt ):
28
53
return SgTimezone .ZERO
@@ -34,25 +59,41 @@ def dst(self, dt):
34
59
return SgTimezone .ZERO
35
60
36
61
class LocalTimezone (tzinfo ):
62
+ '''
63
+ Implementation of datetime's tzinfo to provide convenient conversion
64
+ between Shotgun server time and local user time
65
+ '''
37
66
38
67
def utcoffset (self , dt ):
68
+ '''
69
+ Difference between the user's local timezone and UTC timezone in seconds
70
+ '''
39
71
if self ._isdst (dt ):
40
72
return SgTimezone .DSTOFFSET
41
73
else :
42
74
return SgTimezone .STDOFFSET
43
75
44
76
def dst (self , dt ):
77
+ '''
78
+ Daylight savings time (dst) offset in seconds
79
+ '''
45
80
if self ._isdst (dt ):
46
81
return SgTimezone .DSTDIFF
47
82
else :
48
83
return SgTimezone .ZERO
49
84
50
85
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
+ '''
51
90
return _time .tzname [self ._isdst (dt )]
52
91
53
92
def _isdst (self , dt ):
93
+ '''
94
+ Calculate whether the timestamp in question was in daylight savings
95
+ '''
54
96
tt = (dt .year , dt .month , dt .day , dt .hour , dt .minute , dt .second , dt .weekday (), 0 , - 1 )
55
- import time as _time
56
97
stamp = _time .mktime (tt )
57
98
tt = _time .localtime (stamp )
58
99
return tt .tm_isdst > 0
0 commit comments