/
pytz_fixer.py
59 lines (49 loc) · 2.11 KB
/
pytz_fixer.py
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
# -*- coding: utf-8 -*-
"""
Original: https://gist.github.com/gyst/c3739160a18cf52c93e0132da511b2ee
see https://stackoverflow.com/questions/4008960/pytz-and-etc-gmt-5
for GMT+ and GMT- bug
"""
from collective.transmogrifier.interfaces import ISection
from collective.transmogrifier.interfaces import ISectionBlueprint
from collective.transmogrifier.utils import Condition
from collective.transmogrifier.utils import Matcher
from zope.interface import implementer
from zope.interface import provider
import logging
logger = logging.getLogger(__name__)
@provider(ISectionBlueprint)
@implementer(ISection)
class TimezoneFixerSection(object):
"""
Fixes UnknownTimeZoneError: (UnknownTimeZoneError('GMT+1',),
by replacing the (unknown) GMT+x timezone with Etc/GMT+x
"""
def __init__(self, transmogrifier, name, options, previous):
keys = options.get("keys") or "regexp:(.*[Dd]ate)$"
self.keys = Matcher(*keys.splitlines())
self.condition = Condition(
options.get("condition", "python:True"), transmogrifier, name, options
)
self.previous = previous
def __iter__(self):
for item in self.previous:
if self.condition(item):
for key in item.keys():
match = self.keys(key)[1]
if match:
oldvalue = item[key]
if "GMT+" in item[key]:
item[key] = item[key].replace(" GMT+", " Etc/GMT-") # noqa
elif "GMT-" in item[key]:
item[key] = item[key].replace(" GMT-", " Etc/GMT+") # noqa
elif "GMT" in item[key]:
item[key] = item[key].replace(" GMT", " Etc/GMT")
newvalue = item[key]
if oldvalue != newvalue:
logger.info(
"pytz_fixer: field: {}, oldvalue {} newvalue {}".format( # noqa
key, oldvalue, newvalue
)
)
yield item