From 7af5cc215a5d19641939eca9529e9a8c82583ca4 Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:25:13 -0300 Subject: [PATCH] fix(flags): clamp relative month and year lookbacks after every step Month and year lookbacks jumped straight to the target and clamped once. The SDK spec requires one unit at a time with a clamp after every step, so 2m on March 31 is January 28, and 4y on February 29 is February 28. --- .../relative-date-clamp-every-step.md | 5 +++ posthog/feature_flags.py | 16 ++++++--- posthog/test/test_feature_flags.py | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 .sampo/changesets/relative-date-clamp-every-step.md diff --git a/.sampo/changesets/relative-date-clamp-every-step.md b/.sampo/changesets/relative-date-clamp-every-step.md new file mode 100644 index 000000000..e6e89f7a1 --- /dev/null +++ b/.sampo/changesets/relative-date-clamp-every-step.md @@ -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. diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 6a16ee319..bf10cf946 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -1609,14 +1609,20 @@ 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) @@ -1624,7 +1630,9 @@ def _subtract_years(dt: datetime.datetime, years: int) -> Optional[datetime.date 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) diff --git a/posthog/test/test_feature_flags.py b/posthog/test/test_feature_flags.py index ff1d56593..a18592db4 100644 --- a/posthog/test/test_feature_flags.py +++ b/posthog/test/test_feature_flags.py @@ -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(