Skip to content
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

[BUG] TypeError: strptime() argument 1 must be str, not datetime.date #48

Closed
jgraichen opened this issue Apr 12, 2023 · 3 comments · Fixed by #49
Closed

[BUG] TypeError: strptime() argument 1 must be str, not datetime.date #48

jgraichen opened this issue Apr 12, 2023 · 3 comments · Fixed by #49
Labels
bug Something isn't working enhancement New feature or request

Comments

@jgraichen
Copy link
Contributor

Describe the bug

The YAML parser already converts time formats in the frontmatter into datetime objects. If reading from frontmatter is enabled, building the page fails with:

TypeError: strptime() argument 1 must be str, not datetime.datetime

To Reproduce
Steps to reproduce the behavior:

  1. Configure mkdocs.yml:

    plugins:
      - blogging:
          meta_time_format: '%Y-%m-%d'
    
  2. Create an article with one of the following metadata values:

    ---
    date: 2023-04-12
    ---
    
    ---
    date: 2023-04-12 12:12
    ---
    

Expected behavior

Building the page should not fail.

Otherwise, it would be nice if an available date and datetime would be taken without configuring meta_time_format, if it already is a valid date by YAML. This would make the blogging plugin compatible with mkdocs-rss-plugin.

Maybe something like this:

    def with_timestamp(self, page, by_revision):
        timestamp = self.get_datetime_from_meta(page)
        if "time" in page.meta:
            timestamp = self._parse_time(page.meta["time"])
        if "date" in page.meta and timestamp is None:
            timestamp = self._parse_time(page.meta["date"])
        # ...

    def _parse_time(self, value):
        if isinstance(value, date):
            return datetime.combine(value, datetime.min.time()).timestamp()
        if self.meta_time_format:
            return datetime.strptime(value, self.meta_time_format).timestamp()

Environment (please complete the following information):

  • OS: Ubuntu 22.04
  • Python version 3.10.6
  • Plugin version 2.2.4

Additional context
Add any other context about the problem here.

@liang2kl liang2kl added bug Something isn't working enhancement New feature or request labels Apr 12, 2023
@liang2kl
Copy link
Owner

Thank you for reporting this! Actually, I didn't know the parser will parse things into datetime objects, and it's a great idea to make use of that.

If you want a temporary fix, wrap the date with quotes to make it recognized as a string. I will try to implement the feature you requested as well.

@jgraichen
Copy link
Contributor Author

Will it be ok to send a PR?

@liang2kl
Copy link
Owner

Will it be ok to send a PR?

Sure!

jgraichen added a commit to jgraichen/mkdocs-blogging-plugin that referenced this issue Apr 12, 2023
When valid YAML dates or times are added to the page frontmatter, the
YAML parser will already convert them to datetime.date or
datetime.datetime objects.

Example:

    ---
    date: 2023-04-12
    ---

This resulted in #strptime raising a TypeError because datetime.date
isn't a string. This commit changes the plugin code to always accept a
native datetime.date or datetime.datetime, even if meta_time_format is
not set, and handle them without string parsing.

Close liang2kl#48
jgraichen added a commit to jgraichen/mkdocs-blogging-plugin that referenced this issue Apr 12, 2023
When valid YAML dates or times are added to the page frontmatter, the
YAML parser will already convert them to datetime.date or
datetime.datetime objects.

Example:

    ---
    date: 2023-04-12
    ---

This resulted in #strptime raising a TypeError because datetime.date
isn't a string. This commit changes the plugin code to always accept a
native datetime.date or datetime.datetime, even if meta_time_format is
not set, and handle them without string parsing.

Close liang2kl#48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants