Repository navigation
Add the Julian day ('g') date pattern - #1445
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| assert dates.format_date(d, 'w', locale='en_US') == '14' | ||
|
|
||
|
|
||
| def test_format_julian_day(timezone_getter): |
There was a problem hiding this comment.
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
|
@akx do you think you could have a look when you're free? :) |
There was a problem hiding this comment.
🟢 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
gdate 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
|
Thanks for the super quick review! ❤️ |
While looking into improving skeleton matching (#1084) I noticed we don't support the
gdate pattern (Julian day), or at least only partially.parse_patternwill accept it, butformat_dateraises:I thought it could be nice to support this pattern?