Skip to content

Add the Julian day ('g') date pattern - #1445

Merged
akx merged 1 commit into
python-babel:masterfrom
tomasr8:g-date
Nov 9, 2026
Merged

akx merged 1 commit into
python-babel:masterfrom
tomasr8:g-date

Conversation

@tomasr8

@tomasr8 tomasr8 commented Nov 8, 2026

Copy link
Copy Markdown
Member

While looking into improving skeleton matching (#1084) I noticed we don't support the g date pattern (Julian day), or at least only partially. parse_pattern will accept it, but format_date raises:

>>> from babel.dates import parse_pattern, format_date
>>> from datetime import date
>>> parse_pattern('g')
<DateTimePattern 'g'>
>>> format_date(date(2000, 1, 1), 'g', locale='en')
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    format_date(date(2000, 1, 1), 'g', locale='en')
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/dev/babel/babel/dates.py", line 792, in format_date
    return pattern.apply(date, locale)
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "~/dev/babel/babel/dates.py", line 1551, in apply
    return self % DateTimeFormat(datetime, locale, reference_date)
           ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  File "~/dev/babel/babel/dates.py", line 1543, in __mod__
    return self.format % other
           ~~~~~~~~~~~~^~~~~~~
  File "~/dev/babel/babel/dates.py", line 1616, in __getitem__
    raise KeyError(f"Unsupported date/time field {char!r}")
KeyError: "Unsupported date/time field 'g'"
>>> 

I thought it could be nice to support this pattern?

@codspeed

codspeed Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing tomasr8:g-date (c8510a9) with master (a18709c)

Open in CodSpeed

@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.63%. Comparing base (a18709c) to head (c8510a9).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1445   +/-   ##
=======================================
  Coverage   93.62%   93.63%           
=======================================
  Files          27       27           
  Lines        4973     4978    +5     
=======================================
+ Hits         4656     4661    +5     
  Misses        317      317           
Flag Coverage Δ
macos-15-3.10 92.70% <100.00%> (?)
macos-15-3.11 92.64% <100.00%> (?)
macos-15-3.12 92.90% <100.00%> (?)
macos-15-3.13 92.90% <100.00%> (+<0.01%) ⬆️
macos-15-3.14 92.88% <100.00%> (?)
macos-15-3.15 92.88% <100.00%> (+<0.01%) ⬆️
macos-15-3.9 92.64% <100.00%> (?)
macos-15-pypy3.10 92.70% <100.00%> (?)
ubuntu-24.04-3.10 92.72% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 92.66% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 92.92% <100.00%> (?)
ubuntu-24.04-3.13 92.92% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 92.90% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 92.90% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 92.66% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.10 92.72% <100.00%> (+<0.01%) ⬆️
windows-2022-3.10 92.72% <100.00%> (+<0.01%) ⬆️
windows-2022-3.11 92.66% <100.00%> (?)
windows-2022-3.12 92.92% <100.00%> (?)
windows-2022-3.13 92.92% <100.00%> (?)
windows-2022-3.14 92.90% <100.00%> (?)
windows-2022-3.15 92.90% <100.00%> (+<0.01%) ⬆️
windows-2022-3.9 92.65% <100.00%> (+<0.01%) ⬆️
windows-2022-pypy3.10 92.72% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread tests/test_dates.py
assert dates.format_date(d, 'w', locale='en_US') == '14'


def test_format_julian_day(timezone_getter):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just to be sure, I checked these test cases against ICU using this script:

from datetime import datetime, timezone
from icu import SimpleDateFormat, Locale, TimeZone


def format_date(pattern, tz, dt, locale='en'):
    fmt = SimpleDateFormat(pattern, Locale(locale))
    fmt.setTimeZone(TimeZone.createTimeZone(tz))
    return fmt.format(dt.timestamp())  


U = timezone.utc
print(format_date('g', 'UTC', datetime(476, 9, 4, tzinfo=U)))
print(format_date('g', 'UTC', datetime(1970, 1, 1, 12, tzinfo=U)))
print(format_date('g', 'UTC', datetime(1970, 1, 1, 12, 30, tzinfo=U)))
print(format_date('ggggggggg', 'UTC', datetime(2026, 11, 7, tzinfo=U)))

dt = datetime(2007, 3, 31, 23, 30, tzinfo=U)
print(format_date('g', 'UTC', dt))
print(format_date('g', 'Europe/Zurich', dt))

You can run it with uv:

uv run --with PyICU python

@tomasr8

tomasr8 commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

@akx do you think you could have a look when you're free? :)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the change adding g date-pattern formatting support.

No open issues 🎉

What changed in this PR

Adds formatting support for the previously accepted but unsupported g date pattern in Babel’s date and datetime formatters.

Changes:

  • Calculates Julian day values from date ordinals, with zero padding based on pattern length.
  • Adds tests for historical dates, ignored time components, padding, and local-time-zone behavior.
  • Documents the g date field.
File Description
tests/​test_dates.py Tests Julian day formatting, padding, and timezone handling.
docs/​dates.rst Replaces the g placeholder with a field description.
babel/​dates.py Dispatches g patterns to the new Julian day formatter.

Review effort: Balanced

@akx akx 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.

Well julienned! 😄

@akx
akx enabled auto-merge (squash) November 9, 2026 09:44
@akx
akx merged commit c210d07 into python-babel:master Nov 9, 2026
43 of 61 checks passed
@tomasr8
tomasr8 deleted the g-date branch November 9, 2026 11:15
@tomasr8

tomasr8 commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Thanks for the super quick review! ❤️

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants