fix: handle month overflow in duration_in_seconds for multi-month durations (#23099)

When value > 1 (e.g., "2mo") and current_month + value > 12,
target_month exceeds valid range (1-12), causing ValueError in
datetime constructor. For example, calling duration_in_seconds("2mo")
in November produces target_month=13.

Use modular arithmetic to correctly wrap months and increment year.

Signed-off-by: JiangNan <1394485448@qq.com>
This commit is contained in:
JiangNan 2026-03-10 10:46:26 +08:00 • committed by GitHub
parent 9b15f639a4
commit 0d3735f9c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -64,12 +64,10 @@ def duration_in_seconds(duration: str) -> int:
now = time.time()
current_time = datetime.fromtimestamp(now)
if current_time.month == 12:
target_year = current_time.year + 1
target_month = 1
else:
target_year = current_time.year
target_month = current_time.month + value
# Calculate target month and year, handling overflow past December
total_months = current_time.month - 1 + value # 0-indexed months
target_year = current_time.year + total_months // 12
target_month = total_months % 12 + 1 # back to 1-indexed
# Determine the day to set for next month
target_day = current_time.day