Skip to content

Timezone.from_tzinfo(): Use RRULE rather than RDATE in VTIMEZONE? #2074

Open

Description

@tobixen

Short human-written summary:

We discovered in python-caldav/caldav#852 that the size of the VTIMEZONE wasn't constant. RDATE is used in the VTIMEZONE to indicate the DST, and the VTIMEZONE covers the range of the calendar.

It's being claimed that year 9999 is a common "no end"-sentinel. While I've never seen that myself, I suppose such systems exist. If something starts in the current decade and ends in year 9999, the VTIMEZONE will be hundreds of kilobytes big.

Wouldn't it be better to use RRULE?

Longer analysis by Claude Opus 5.5 under the line.


Timezone.from_tzinfo() / Timezone.from_tzid() describe every transition between first_date and last_date as an RDATE, and nothing after last_date. That gives two problems for a caller that has to cover dates far in the future:

  1. The size grows linearly with the range. For Europe/Berlin on current main (7.3.1.dev188):

    last_date to_ical() size
    2038-01-01 (default) 2343 bytes
    2100-01-01 4405 bytes
    2200-01-01 7737 bytes
    2500-01-01 17727 bytes
    9998-01-01 267389 bytes

    Year 9999 is a common "no end" sentinel, so a caller that stretches the range to the dates in its calendar ends up with very large VTIMEZONEs.

  2. Times after last_date get the last offset. A client that reads the VTIMEZONE rather than resolving the TZID shows summer-time dates past last_date an hour off:

    import datetime
    from zoneinfo import ZoneInfo
    import icalendar
    
    tz = icalendar.Timezone.from_tzinfo(
        ZoneInfo("Europe/Berlin"), tzid="Custom-Berlin", last_date=datetime.date(2100, 1, 1)
    )
    d = datetime.datetime(2150, 7, 1, 12)
    print(d.replace(tzinfo=tz.to_tz()).utcoffset())                # 1:00:00
    print(d.replace(tzinfo=ZoneInfo("Europe/Berlin")).utcoffset())  # 2:00:00

    (A custom TZID is used only so that to_tz() reads the VTIMEZONE instead of looking Europe/Berlin up.)

Suggestion: after the last irregular transition, emit one STANDARD and one DAYLIGHT observance with an open-ended RRULE for the rule that keeps repeating. That is what most VTIMEZONEs in the wild do, e.g. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU. The rule is available: TZif files end with a POSIX TZ string (e.g. CET-1CEST,M3.5.0,M10.5.0/3) for exactly this purpose. Inferring it from two consecutive years of transitions would also work for pytz and other tzinfo objects. The VTIMEZONE would then be small and correct for any future date. last_date would only matter for zones without a recurring rule.

Context: python-caldav adds missing VTIMEZONEs to the objects it creates (python-caldav/caldav#852). It now stretches first_date/last_date to the dates in the object, but has to cap last_date at the end of 2200 because of the size, so summer-time dates after 2200 are an hour off in clients that read the VTIMEZONE.

Activity

  1. github-actions commented on Nov 8, 2026

    @github-actions
    Contributor

    Thanks for the issue, @tobixen! A maintainer will review it shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions