Переглянути джерело

Fixed "CRITICAL: can't compare offset-naive and offset-aware datetimes" bug

This is similar to another issue in a different plugin:
https://github.com/getpelican/pelican-plugins/issues/264
Kyle Barlow 9 роки тому
батько
коміт
698dd8706e
1 змінених файлів з 14 додано та 10 видалено
  1. 14 10
      filetime_from_git/filetime_from_git.py

+ 14 - 10
filetime_from_git/filetime_from_git.py

@@ -6,7 +6,7 @@ from git import Git, Repo, InvalidGitRepositoryError
 from pelican import signals, contents
 from datetime import datetime
 from time import mktime, altzone
-from pelican.utils import  strftime
+from pelican.utils import strftime, set_date_tzinfo
 
 try:
     repo = Repo(os.path.abspath('.'))
@@ -14,6 +14,14 @@ try:
 except InvalidGitRepositoryError as e:
     repo = None
 
+def datetime_from_timestamp(timestamp, content):
+    """
+    Helper function to add timezone information to datetime,
+    so that datetime is comparable to other datetime objects in recent versions
+    that now also have timezone information.
+    """
+    return set_date_tzinfo(datetime.fromtimestamp(timestamp), tz_name=content.settings.get('TIMEZONE', None))
+
 def filetime_from_git(content):
     if isinstance(content, contents.Static) or repo is None:
         return
@@ -34,26 +42,26 @@ def filetime_from_git(content):
             with_extended_output=True, with_exceptions=False)
     if status != 0:
         # file is not managed by git
-        content.date = datetime.fromtimestamp(os.stat(path).st_ctime)
+        content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
     else:
         # file is managed by git
         commits = repo.commits(path=path)
         if len(commits) == 0:
             # never commited, but staged
-            content.date = datetime.fromtimestamp(os.stat(path).st_ctime)
+            content.date = datetime_from_timestamp(os.stat(path).st_ctime, content)
         else:
             # has commited
-            content.date = datetime.fromtimestamp(mktime(commits[-1].committed_date) - altzone)
+            content.date = datetime_from_timestamp(mktime(commits[-1].committed_date) - altzone, content)
 
             status, stdout, stderr = git.execute(['git', 'diff', '--quiet', 'HEAD', path],
                     with_extended_output=True, with_exceptions=False)
             if status != 0:
                 # file has changed
-                content.modified = datetime.fromtimestamp(os.stat(path).st_ctime)
+                content.modified = datetime_from_timestamp(os.stat(path).st_ctime, content)
             else:
                 # file is not changed
                 if len(commits) > 1:
-                    content.modified = datetime.fromtimestamp(mktime(commits[0].committed_date) - altzone)
+                    content.modified = datetime_from_timestamp(mktime(commits[0].committed_date) - altzone, content)
     if not hasattr(content, 'modified'):
         content.modified = content.date
     if hasattr(content, 'date'):
@@ -61,9 +69,5 @@ def filetime_from_git(content):
     if hasattr(content, 'modified'):
         content.locale_modified = strftime(content.modified, content.date_format)
 
-
-
-
-
 def register():
     signals.content_object_init.connect(filetime_from_git)