Skip to content

Fix timezone placement in add_missing_timezones()#878

Merged
niccokunzmann merged 11 commits into
collective:mainfrom
SashankBhamidi:fix-timezone-placement-issue-844
Jul 17, 2025
Merged

Fix timezone placement in add_missing_timezones()#878
niccokunzmann merged 11 commits into
collective:mainfrom
SashankBhamidi:fix-timezone-placement-issue-844

Conversation

@SashankBhamidi

@SashankBhamidi SashankBhamidi commented Jul 16, 2025

Copy link
Copy Markdown
Member

Fixes #844 by modifying Calendar.add_missing_timezones() to insert VTIMEZONE components at the beginning of the calendar instead of appending them to the end.

Problem

The add_missing_timezones() method was placing VTIMEZONE components after VEVENT and other components that reference them. This violates standard iCalendar practices where VTIMEZONE components should appear before components that reference them.

Solution

  • Modified the insertion logic to place new VTIMEZONE components after any existing VTIMEZONE components but before other component types
  • Preserves existing timezone components in their current positions
  • Maintains backward compatibility with all existing functionality

Changes

  • Modified: src/icalendar/cal/calendar.py - Updated add_missing_timezones() method
  • Added: src/icalendar/tests/test_issue_844_timezone_placement.py - Comprehensive test coverage
  • Updated: CHANGES.rst and docs/credits.rst per contributing guidelines

Test Coverage

  • Single and multiple timezone scenarios
  • Existing timezone preservation
  • Edge cases (empty calendar, no missing timezones)
  • Serialized output validation
  • Forward reference compatibility
  • All existing tests continue to pass (755 tests total)

📚 Documentation preview 📚: https://icalendar--878.org.readthedocs.build/

Modify Calendar.add_missing_timezones() to insert VTIMEZONE components
at the beginning of the calendar instead of appending them to the end.
This ensures VTIMEZONE components appear before VEVENT and other
components that reference them, following standard iCalendar practices.

The fix preserves existing timezone components and inserts new ones
immediately after any existing VTIMEZONE components.

Fixes #844
Add test_issue_844_timezone_placement.py with comprehensive test
coverage for the timezone placement fix, including:

- Single and multiple timezone scenarios
- Existing timezone preservation
- Edge cases (empty calendar, no missing timezones)
- Serialized output validation
- Forward reference compatibility

These tests ensure the fix works correctly and doesn't break
existing functionality.
- Add entry to CHANGES.rst for issue #844 fix
- Add contributor to docs/credits.rst
Test timezone placement functionality in environments where pytz is not available by temporarily mocking pytz module to raise ImportError.
Use try/except import to handle zoneinfo vs backports.zoneinfo for Python < 3.9 compatibility.
Replace custom try/except import with the official icalendar.compatibility module for consistent Python version compatibility across the codebase.
@coveralls

coveralls commented Jul 16, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 16340146346

Details

  • 139 of 140 (99.29%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 97.368%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/icalendar/tests/test_issue_844_timezone_placement.py 132 133 99.25%
Totals Coverage Status
Change from base Build 16246736182: 0.04%
Covered Lines: 6780
Relevant Lines: 6953

💛 - Coveralls

@stevepiercy stevepiercy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting for change log. Still needs a technical review.

I'd suggest running tox -e ruff to lint the Python files.

https://github.com/collective/icalendar/blob/main/tox.ini#L44

We should also add that to the Contributing guide and as a CI check. @niccokunzmann what do you think?

Comment thread CHANGES.rst Outdated
- Format method name add_missing_timezones() with backticks
- Format component names VTIMEZONE, VEVENT with backticks
- Run tox -e ruff as requested for code quality

@stevepiercy stevepiercy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs a technical review. docstrings and news look good.

@niccokunzmann niccokunzmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this thorough work!

Comment thread src/icalendar/tests/test_issue_844_timezone_placement.py
Comment thread src/icalendar/tests/test_issue_844_timezone_placement.py
@niccokunzmann

Copy link
Copy Markdown
Member

@stevepiercy, I think this is good to discuss in the issue #672.
At present, tox -e ruff will format everything, with 364 places to fix.
Installing the pre-commit is the way to go at present, I feel.

@niccokunzmann niccokunzmann merged commit e443764 into collective:main Jul 17, 2025
18 checks passed
@SashankBhamidi SashankBhamidi deleted the fix-timezone-placement-issue-844 branch August 19, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Missing timezones are added to the end of the file but should be at the start

4 participants