guides

Date and Time Processing

Fast date arithmetic, parsing, and timezone handling.

Published May 30, 2026

Fast Date Parsing

def parse_dates(dates: list[str]) -> list[tuple[int, int, int]]:
    result = []
    for d in dates:
        year = int(d[0:4])
        month = int(d[5:7])
        day = int(d[8:10])
        result.append((year, month, day))
    return result

Day of Week Calculation

def day_of_week(year: int, month: int, day: int) -> int:
    if month < 3:
        month += 12
        year -= 1
    k = year % 100
    j = year // 100
    f = day + 13 * (month + 1) // 5 + k + k // 4 + j // 4 + 5 * j
    return f % 7

Avoid datetime in Hot Loops

The datetime module creates objects with significant overhead. For bulk date arithmetic, use integer tuples or epoch seconds.