Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/isodate/tzinfo.py: 53%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

49 statements  

1''' 

2This module provides some datetime.tzinfo implementations. 

3 

4All those classes are taken from the Python documentation. 

5''' 

6from datetime import timedelta, tzinfo 

7import time 

8 

9ZERO = timedelta(0) 

10# constant for zero time offset. 

11 

12 

13class Utc(tzinfo): 

14 '''UTC 

15 

16 Universal time coordinated time zone. 

17 ''' 

18 

19 def utcoffset(self, dt): 

20 ''' 

21 Return offset from UTC in minutes east of UTC, which is ZERO for UTC. 

22 ''' 

23 return ZERO 

24 

25 def tzname(self, dt): 

26 ''' 

27 Return the time zone name corresponding to the datetime object dt, 

28 as a string. 

29 ''' 

30 return "UTC" 

31 

32 def dst(self, dt): 

33 ''' 

34 Return the daylight saving time (DST) adjustment, in minutes east 

35 of UTC. 

36 ''' 

37 return ZERO 

38 

39 def __reduce__(self): 

40 ''' 

41 When unpickling a Utc object, return the default instance below, UTC. 

42 ''' 

43 return _Utc, () 

44 

45 

46UTC = Utc() 

47# the default instance for UTC. 

48 

49 

50def _Utc(): 

51 ''' 

52 Helper function for unpickling a Utc object. 

53 ''' 

54 return UTC 

55 

56 

57class FixedOffset(tzinfo): 

58 ''' 

59 A class building tzinfo objects for fixed-offset time zones. 

60 

61 Note that FixedOffset(0, 0, "UTC") or FixedOffset() is a different way to 

62 build a UTC tzinfo object. 

63 ''' 

64 

65 def __init__(self, offset_hours=0, offset_minutes=0, name="UTC"): 

66 ''' 

67 Initialise an instance with time offset and name. 

68 The time offset should be positive for time zones east of UTC 

69 and negate for time zones west of UTC. 

70 ''' 

71 self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes) 

72 self.__name = name 

73 

74 def utcoffset(self, dt): 

75 ''' 

76 Return offset from UTC in minutes of UTC. 

77 ''' 

78 return self.__offset 

79 

80 def tzname(self, dt): 

81 ''' 

82 Return the time zone name corresponding to the datetime object dt, as a 

83 string. 

84 ''' 

85 return self.__name 

86 

87 def dst(self, dt): 

88 ''' 

89 Return the daylight saving time (DST) adjustment, in minutes east of 

90 UTC. 

91 ''' 

92 return ZERO 

93 

94 def __repr__(self): 

95 ''' 

96 Return nicely formatted repr string. 

97 ''' 

98 return "<FixedOffset %r>" % self.__name 

99 

100 

101STDOFFSET = timedelta(seconds=-time.timezone) 

102# locale time zone offset 

103 

104# calculate local daylight saving offset if any. 

105if time.daylight: 

106 DSTOFFSET = timedelta(seconds=-time.altzone) 

107else: 

108 DSTOFFSET = STDOFFSET 

109 

110DSTDIFF = DSTOFFSET - STDOFFSET 

111# difference between local time zone and local DST time zone 

112 

113 

114class LocalTimezone(tzinfo): 

115 """ 

116 A class capturing the platform's idea of local time. 

117 """ 

118 

119 def utcoffset(self, dt): 

120 ''' 

121 Return offset from UTC in minutes of UTC. 

122 ''' 

123 if self._isdst(dt): 

124 return DSTOFFSET 

125 else: 

126 return STDOFFSET 

127 

128 def dst(self, dt): 

129 ''' 

130 Return daylight saving offset. 

131 ''' 

132 if self._isdst(dt): 

133 return DSTDIFF 

134 else: 

135 return ZERO 

136 

137 def tzname(self, dt): 

138 ''' 

139 Return the time zone name corresponding to the datetime object dt, as a 

140 string. 

141 ''' 

142 return time.tzname[self._isdst(dt)] 

143 

144 def _isdst(self, dt): 

145 ''' 

146 Returns true if DST is active for given datetime object dt. 

147 ''' 

148 tt = (dt.year, dt.month, dt.day, 

149 dt.hour, dt.minute, dt.second, 

150 dt.weekday(), 0, -1) 

151 stamp = time.mktime(tt) 

152 tt = time.localtime(stamp) 

153 return tt.tm_isdst > 0 

154 

155 

156# the default instance for local time zone. 

157LOCAL = LocalTimezone()