-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use babel.format_datetime instead of a str concatenation #23
Conversation
Thanks for the PR! This will change the output, both date (shorter version) and time (adding AM/PM time in certain locales): from babel.dates import format_datetime, format_date
from datetime import datetime
dt = datetime(2007, 4, 1, 15, 30)
format_datetime(dt, format="medium", locale='en_US')
# 'Apr 1, 2007, 3:30:00 PM'
format_datetime(dt, format="medium", locale='nl')
# '1 apr. 2007 15:30:00'
format_date(dt, format="long", locale='nl')
# '1 april 2007' To maintain consistency while removing the string concat, you can specify a custom date/time pattern to |
Hi @Guts, I see you added some commits, but issue remains: output is not backwards compatible. Would you like to address this, or close this PR? Tim |
Codecov Report
@@ Coverage Diff @@
## master #23 +/- ##
=======================================
Coverage 86.45% 86.45%
=======================================
Files 2 2
Lines 96 96
=======================================
Hits 83 83
Misses 13 13
Continue to review full report at Codecov.
|
Hi @timvink . I just merged from your master branch. Would you be interested in my pre-commit config? It's a practical tool to ensure consistency between commiters to a repo. Let me know :) Oh and spoiler alert: I use your https://github.com/timvink/mkdocs-git-authors-plugin too and I've got some changes to propose too. |
Sure! You can reach me at [email protected]. I agree blackening the code is an improvement.
Awesome, great to hear that! I've been trying to find time to release a new version of git-authors (my job is busy despite going remote). I expect to finish next Monday. So I advise to wait before proposing changes. Check the roadmap issue as well (timvink/mkdocs-git-authors-plugin#16)
Well, the output is still different. Depending on the locale, it shows the AM/PM time. In my locale >>> format_datetime(dt, format="long", locale='nl')
'1 april 2007 om 15:30:00 UTC' Probably this is just a personal preference, but I like Do you have a use case for this format or was it meant as an optimization to get rid of the 'ugly' string concat? I would be open to adding another output type, f.e. |
No pressure. I'll wait :)
Okay, I didn't understand!
It was meant to get rid of the 'ugly' str concat and get more consistent using the same lib to format dates and later use skeleton method. If you prefer the actual result, you can close. Below, I just put my test script for memory: """Test different behaviors in datetime formatting between babel and standard lib
"""
from datetime import date, datetime
from babel.dates import format_date, format_datetime
# globals
t = datetime.utcnow()
loc = "nl"
# date
print("Date with babel: %s" % format_date(date=t, format="medium", locale=loc))
print("Date with PSL: %s" % t.strftime("%d %B %Y"))
# datetime
print(
"\nDatetime with babel only: %s"
% format_datetime(datetime=t, format="long", locale=loc)
)
print(
"Datetime with babel and concat: %s %s"
% (format_date(date=t, format="long", locale=loc), t.strftime("%H:%M:%S"),)
)
# iso date
print("\niso_date custom: %s" % t.strftime("%Y-%m-%d"))
print("iso_date with babel: %s" % format_date(t, format="short", locale=loc))
print("iso_date with PSL isoformat: %s" % t.date().isoformat())
# iso datetime
print("\niso_datetime custom: %s" % t.strftime("%Y-%m-%d %H:%M:%S"))
print("iso_datetime babel: %s" % format_datetime(t, "medium", locale=loc))
print("iso_datetime PSL: %s" % t.isoformat()) |
Thanks for understanding! |
In
mkdocs-git-revision-date-localized-plugin/mkdocs_git_revision_date_localized_plugin/util.py
Lines 52 to 58 in 4e36cbe
Prefer babel as for
format_date
: http://babel.pocoo.org/en/latest/api/dates.html#babel.dates.format_datetime