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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
"""
Parsing of a POSIX zone spec as described in the TZ part of section 8.3 in
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html.
"""
import re
from typing import Optional
from pendulum.constants import MONTHS_OFFSETS
from pendulum.constants import SECS_PER_DAY
from .exceptions import InvalidPosixSpec
_spec = re.compile(
"^"
r"(?P<std_abbr><.*?>|[^-+,\d]{3,})"
r"(?P<std_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)"
r"(?P<dst_info>"
r" (?P<dst_abbr><.*?>|[^-+,\d]{3,})"
r" (?P<dst_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)?"
r")?"
r"(?:,(?P<rules>"
r" (?P<dst_start>"
r" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
r" (?:/(?P<dst_start_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
" )"
" ,"
r" (?P<dst_end>"
r" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
r" (?:/(?P<dst_end_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
" )"
"))?"
"$",
re.VERBOSE,
)
def posix_spec(spec): # type: (str) -> PosixTimezone
try:
return _posix_spec(spec)
except ValueError:
raise InvalidPosixSpec(spec)
def _posix_spec(spec): # type: (str) -> PosixTimezone
m = _spec.match(spec)
if not m:
raise ValueError("Invalid posix spec")
std_abbr = _parse_abbr(m.group("std_abbr"))
std_offset = _parse_offset(m.group("std_offset"))
dst_abbr = None
dst_offset = None
if m.group("dst_info"):
dst_abbr = _parse_abbr(m.group("dst_abbr"))
if m.group("dst_offset"):
dst_offset = _parse_offset(m.group("dst_offset"))
else:
dst_offset = std_offset + 3600
dst_start = None
dst_end = None
if m.group("rules"):
dst_start = _parse_rule(m.group("dst_start"))
dst_end = _parse_rule(m.group("dst_end"))
return PosixTimezone(std_abbr, std_offset, dst_abbr, dst_offset, dst_start, dst_end)
def _parse_abbr(text): # type: (str) -> str
return text.lstrip("<").rstrip(">")
def _parse_offset(text, sign=-1): # type: (str, int) -> int
if text.startswith(("+", "-")):
if text.startswith("-"):
sign *= -1
text = text[1:]
minutes = 0
seconds = 0
parts = text.split(":")
hours = int(parts[0])
if len(parts) > 1:
minutes = int(parts[1])
if len(parts) > 2:
seconds = int(parts[2])
return sign * ((((hours * 60) + minutes) * 60) + seconds)
def _parse_rule(rule): # type: (str) -> PosixTransition
klass = NPosixTransition
args = ()
if rule.startswith("M"):
rule = rule[1:]
parts = rule.split(".")
month = int(parts[0])
week = int(parts[1])
day = int(parts[2].split("/")[0])
args += (month, week, day)
klass = MPosixTransition
elif rule.startswith("J"):
rule = rule[1:]
args += (int(rule.split("/")[0]),)
klass = JPosixTransition
else:
args += (int(rule.split("/")[0]),)
# Checking offset
parts = rule.split("/")
if len(parts) > 1:
offset = _parse_offset(parts[-1], sign=1)
else:
offset = 7200
args += (offset,)
return klass(*args)
class PosixTransition(object):
def __init__(self, offset): # type: (int) -> None
self._offset = offset
@property
def offset(self): # type: () -> int
return self._offset
def trans_offset(self, is_leap, jan1_weekday): # type: (bool, int) -> int
raise NotImplementedError()
class JPosixTransition(PosixTransition):
def __init__(self, day, offset): # type: (int, int) -> None
self._day = day
super(JPosixTransition, self).__init__(offset)
@property
def day(self): # type: () -> int
"""
day of non-leap year [1:365]
"""
return self._day
def trans_offset(self, is_leap, jan1_weekday): # type: (bool, int) -> int
days = self._day
if not is_leap or days < MONTHS_OFFSETS[1][3]:
days -= 1
return (days * SECS_PER_DAY) + self._offset
class NPosixTransition(PosixTransition):
def __init__(self, day, offset): # type: (int, int) -> None
self._day = day
super(NPosixTransition, self).__init__(offset)
@property
def day(self): # type: () -> int
"""
day of year [0:365]
"""
return self._day
def trans_offset(self, is_leap, jan1_weekday): # type: (bool, int) -> int
days = self._day
return (days * SECS_PER_DAY) + self._offset
class MPosixTransition(PosixTransition):
def __init__(self, month, week, weekday, offset):
# type: (int, int, int, int) -> None
self._month = month
self._week = week
self._weekday = weekday
super(MPosixTransition, self).__init__(offset)
@property
def month(self): # type: () -> int
"""
month of year [1:12]
"""
return self._month
@property
def week(self): # type: () -> int
"""
week of month [1:5] (5==last)
"""
return self._week
@property
def weekday(self): # type: () -> int
"""
0==Sun, ..., 6=Sat
"""
return self._weekday
def trans_offset(self, is_leap, jan1_weekday): # type: (bool, int) -> int
last_week = self._week == 5
days = MONTHS_OFFSETS[is_leap][self._month + int(last_week)]
weekday = (jan1_weekday + days) % 7
if last_week:
days -= (weekday + 7 - 1 - self._weekday) % 7 + 1
else:
days += (self._weekday + 7 - weekday) % 7
days += (self._week - 1) * 7
return (days * SECS_PER_DAY) + self._offset
class PosixTimezone:
"""
The entirety of a POSIX-string specified time-zone rule.
The standard abbreviation and offset are always given.
"""
def __init__(
self,
std_abbr, # type: str
std_offset, # type: int
dst_abbr, # type: Optional[str]
dst_offset, # type: Optional[int]
dst_start=None, # type: Optional[PosixTransition]
dst_end=None, # type: Optional[PosixTransition]
):
self._std_abbr = std_abbr
self._std_offset = std_offset
self._dst_abbr = dst_abbr
self._dst_offset = dst_offset
self._dst_start = dst_start
self._dst_end = dst_end
@property
def std_abbr(self): # type: () -> str
return self._std_abbr
@property
def std_offset(self): # type: () -> int
return self._std_offset
@property
def dst_abbr(self): # type: () -> Optional[str]
return self._dst_abbr
@property
def dst_offset(self): # type: () -> Optional[int]
return self._dst_offset
@property
def dst_start(self): # type: () -> Optional[PosixTransition]
return self._dst_start
@property
def dst_end(self): # type: () -> Optional[PosixTransition]
return self._dst_end
|