Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle platform-specific issue with strftime/strptime #35

Merged
merged 2 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions deidentify/surrogates/generators/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def minimum_season_offsets(dates):
class DateSurrogates(SurrogateGenerator):

def __init__(self, annotations, year_shift_base=65, year_shift_fuzz=20, random_data=None):
super(DateSurrogates, self).__init__(annotations=annotations, random_data=random_data)
super().__init__(annotations=annotations, random_data=random_data)

self.dates = self._parse_dates(self.annotations)
self.year_shift = year_shift_base + self.random_data.randint(
Expand Down Expand Up @@ -191,14 +191,20 @@ def replace_all(self):
most_recent = self.dates[0]

for date in self.dates:
# TODO: Add an annotation object that encapsules automatic replacement errors
if isinstance(date, NullDate):
# TODO: Add an annotation object that encapsules automatic replacement errors
replaced.append(None)
continue

adjusted = adjust_long_date_span(date, most_recent=most_recent, max_delta=89)
delta = relativedelta(days=self.day_shift, years=self.year_shift)
shifted_date = shift_date(adjusted, delta)
replaced.append(shifted_date.date_string)

try:
shifted_date = shift_date(adjusted, delta)
replaced.append(shifted_date.date_string)
except ValueError:
# shift_date may fail in platform-specific implementations of strftime and strptime
# when they are not symmetric. See: https://bugs.python.org/issue32195
replaced.append(None)

return replaced
1 change: 1 addition & 0 deletions tests/surrogates/generators/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_infer_format():
assert infer_format('23 feb.').format == '%d %b.'
assert infer_format('01 01 2015').format == '%d %m %Y'
assert infer_format('22/05/13').format == '%d/%m/%y'
assert infer_format('0411').format == '%Y'



Expand Down