Skip to content
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
5 changes: 5 additions & 0 deletions .sampo/changesets/relative-date-clamp-every-step.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Local evaluation of relative date filters now clamps the day after every month or year subtracted, as the SDK spec requires. A `2m` filter evaluated on March 31 resolved to January 31 and now resolves to January 28, and `4y` on February 29 resolves to February 28 of the target year.
16 changes: 12 additions & 4 deletions posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,22 +1609,30 @@ def parse_datetime(value: str) -> datetime.datetime:


# Python's stdlib doesn't provide a calendar-aware month/year delta.
# Clamp the day to the target month's end to match dateutil.relativedelta behavior.
# Subtract one unit at a time and clamp the day after every step, so March 31
# minus two months is January 28 (through February 28), as the SDK spec requires.
def _subtract_months(dt: datetime.datetime, months: int) -> Optional[datetime.datetime]:
month_index = dt.year * 12 + dt.month - 1 - months
current_index = dt.year * 12 + dt.month - 1
month_index = current_index - months
year = month_index // 12
month = month_index % 12 + 1
if not 1 <= year <= 9999:
return None
day = min(dt.day, calendar.monthrange(year, month)[1])
day = dt.day
for index in range(current_index - 1, month_index - 1, -1):
if day <= 28: # Every month has at least 28 days, so nothing clamps further.
break
day = min(day, calendar.monthrange(index // 12, index % 12 + 1)[1])
return dt.replace(year=year, month=month, day=day)


def _subtract_years(dt: datetime.datetime, years: int) -> Optional[datetime.datetime]:
year = dt.year - years
if not 1 <= year <= 9999:
return None
day = min(dt.day, calendar.monthrange(year, dt.month)[1])
# The year before a leap year is never a leap year, so February 29 clamps on
# the first step and stays on the 28th.
day = 28 if years > 0 and (dt.month, dt.day) == (2, 29) else dt.day
return dt.replace(year=year, day=day)


Expand Down
34 changes: 34 additions & 0 deletions posthog/test/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7247,6 +7247,40 @@ def test_year_parsing_clamps_to_target_month_end(self):
"1y"
) == now - relativedelta(years=1)

def test_month_parsing_clamps_after_every_step(self):
cases = [
("2025-03-31T12:00:00Z", "2m", (2025, 1, 28)),
("2024-03-31T12:00:00Z", "2m", (2024, 1, 29)),
("2025-05-31T12:00:00Z", "2m", (2025, 3, 30)),
("2025-05-31T12:00:00Z", "4m", (2025, 1, 28)),
("2025-01-31T12:00:00Z", "11m", (2024, 2, 29)),
("2025-03-30T12:00:00Z", "13m", (2024, 2, 28)),
]
for now, value, expected in cases:
with self.subTest(now=now, value=value), freeze_time(now):
assert relative_date_parse_for_feature_flag_matching(
value
) == datetime.datetime(*expected, 12, tzinfo=datetime.timezone.utc)

def test_year_parsing_clamps_after_every_step(self):
cases = [
("2024-02-29T12:00:00Z", "4y", (2020, 2, 28)),
("2024-02-29T12:00:00Z", "2y", (2022, 2, 28)),
("2024-03-31T12:00:00Z", "4y", (2020, 3, 31)),
("2024-02-28T12:00:00Z", "4y", (2020, 2, 28)),
]
for now, value, expected in cases:
with self.subTest(now=now, value=value), freeze_time(now):
assert relative_date_parse_for_feature_flag_matching(
value
) == datetime.datetime(*expected, 12, tzinfo=datetime.timezone.utc)

def test_month_and_year_parsing_with_zero_keeps_the_date(self):
with freeze_time("2024-02-29T12:00:00Z"):
now = datetime.datetime.now(datetime.timezone.utc)
assert relative_date_parse_for_feature_flag_matching("0m") == now
assert relative_date_parse_for_feature_flag_matching("0y") == now

def test_year_parsing(self):
with freeze_time("2020-01-01T12:01:20.1340Z"):
assert relative_date_parse_for_feature_flag_matching(
Expand Down
Loading