Skip to content

Conversation

@marcosppca
Copy link

Summary

This pull request refactors the date and time formatting logic to use Python’s structural pattern matching (match/case, PEP 634) instead of multiple if/elif statements.
The new implementation improves readability and maintainability while preserving all existing behavior and localization rules.

Motivation

The original function handled several format types (shortdatetime, longdatetime, date, time, datetime, year, etc.) using a long chain of conditional statements.
By switching to match/case, the control flow becomes clearer and easier to extend, making each format explicitly defined and simplifying future maintenance.

Changes

  • Replaced the if/elif chain with a match format: statement.
  • Preserved all existing cases (shortdatetime, shortdate, longdatetime, date, time, datetime, year).
  • Maintained support for “today” detection when format is shortdatetime or shortdate.
  • Kept localization (locale) and timezone (tzinfo) behavior identical to the previous implementation.
  • Raised DateTimeFormatError for unsupported formats, same as before.

Example

match format:
    case "shortdatetime" | "shortdate":
        today = datetime.datetime.now(tz=convert_tzinfo).toordinal()
        kwargs = {"format": "short", "tzinfo": convert_tzinfo, "locale": locale}
        if convert_value.toordinal() == today:
            formatted = _lazy("Today at %s") % format_time(convert_value, **kwargs)
        elif format == "shortdatetime":
            formatted = format_datetime(convert_value, **kwargs)
        else:
            del kwargs["tzinfo"]
            formatted = format_date(convert_value, **kwargs)

    case "longdatetime":
        formatted = format_datetime(
            convert_value, format="long", tzinfo=convert_tzinfo, locale=locale
        )

    case "date":
        formatted = format_date(convert_value, locale=locale)

    case "time":
        formatted = format_time(convert_value, tzinfo=convert_tzinfo, locale=locale)

    case "datetime":
        formatted = format_datetime(convert_value, tzinfo=convert_tzinfo, locale=locale)

    case "year":
        formatted = format_datetime(
            convert_value, format="yyyy", tzinfo=convert_tzinfo, locale=locale
        )

    case _:
        raise DateTimeFormatError

@akatsoulas akatsoulas requested a review from Copilot November 7, 2025 10:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the datetimeformat function in jinja_helpers.py to use Python 3.10+ match-case syntax instead of traditional if-elif-else statements, improving code readability and maintainability.

Key Changes

  • Replaced if-elif-else chain with match-case statement for format string handling
  • Maintained functional equivalence with the original implementation
  • Added pattern matching with the | operator for "shortdatetime" and "shortdate" cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 315 to 316


Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

Extra blank line added. While not a functional issue, this adds unnecessary whitespace. Consider removing this blank line to maintain consistency with the other case branches (lines 298, 301, 304, 307) which don't have trailing blank lines.

Suggested change

Copilot uses AI. Check for mistakes.
return v.replace(tzinfo=tz).date()
return v.astimezone(tz).date()

value_date = _as_date_in_tz(convert_value, convert_tzinfo)
Copy link
Collaborator

@akatsoulas akatsoulas Nov 12, 2025

Choose a reason for hiding this comment

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

Thanks for this PR! Regarding this helper method wouldn't be simpler if this call was

value_date = convert_value.date()

The edge cases that the as_date_in_tz function checks for have been handled in earlier steps. For example the check for Date objects is defined in line 248

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.

2 participants