From 1041766d81ccdd53200f206860d17e6a64d4e65b Mon Sep 17 00:00:00 2001 From: squidfunk Date: Tue, 2 Apr 2024 12:01:50 +0800 Subject: [PATCH] Fixed time sneaking into default post format string --- material/plugins/blog/plugin.py | 12 +++++++++--- src/plugins/blog/plugin.py | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/material/plugins/blog/plugin.py b/material/plugins/blog/plugin.py index 05f76e3ab..da64432de 100644 --- a/material/plugins/blog/plugin.py +++ b/material/plugins/blog/plugin.py @@ -25,7 +25,7 @@ import os import posixpath import yaml -from babel.dates import format_datetime +from babel.dates import format_date, format_datetime from datetime import datetime from jinja2 import pass_context from jinja2.runtime import Context @@ -780,10 +780,16 @@ class BlogPlugin(BasePlugin[BlogConfig]): # ------------------------------------------------------------------------- - # Format date + # Format date - if the given format string refers to a predefined format, + # we format the date without a time component in order to keep sane default + # behavior, since authors will not expect time to be relevant for most posts + # as by our assumptions - see https://t.ly/Yi7ZC def _format_date(self, date: datetime, format: str, config: MkDocsConfig): locale: str = config.theme["language"].replace("-", "_") - return format_datetime(date, format = format, locale = locale) + if format in ["full", "long", "medium", "short"]: + return format_date(date, format = format, locale = locale) + else: + return format_datetime(date, format = format, locale = locale) # Format date for post def _format_date_for_post(self, date: datetime, config: MkDocsConfig): diff --git a/src/plugins/blog/plugin.py b/src/plugins/blog/plugin.py index 05f76e3ab..da64432de 100644 --- a/src/plugins/blog/plugin.py +++ b/src/plugins/blog/plugin.py @@ -25,7 +25,7 @@ import os import posixpath import yaml -from babel.dates import format_datetime +from babel.dates import format_date, format_datetime from datetime import datetime from jinja2 import pass_context from jinja2.runtime import Context @@ -780,10 +780,16 @@ class BlogPlugin(BasePlugin[BlogConfig]): # ------------------------------------------------------------------------- - # Format date + # Format date - if the given format string refers to a predefined format, + # we format the date without a time component in order to keep sane default + # behavior, since authors will not expect time to be relevant for most posts + # as by our assumptions - see https://t.ly/Yi7ZC def _format_date(self, date: datetime, format: str, config: MkDocsConfig): locale: str = config.theme["language"].replace("-", "_") - return format_datetime(date, format = format, locale = locale) + if format in ["full", "long", "medium", "short"]: + return format_date(date, format = format, locale = locale) + else: + return format_datetime(date, format = format, locale = locale) # Format date for post def _format_date_for_post(self, date: datetime, config: MkDocsConfig):