浏览代码

Merge pull request #372 from kylebarlow/master

filetime_from_git plugin: add timezone information to datetime objects
Justin Mayer 10 年之前
父节点
当前提交
dc8dd502d2
共有 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 pelican import signals, contents
 from datetime import datetime
 from datetime import datetime
 from time import mktime, altzone
 from time import mktime, altzone
-from pelican.utils import  strftime
+from pelican.utils import strftime, set_date_tzinfo
 
 
 try:
 try:
     repo = Repo(os.path.abspath('.'))
     repo = Repo(os.path.abspath('.'))
@@ -14,6 +14,14 @@ try:
 except InvalidGitRepositoryError as e:
 except InvalidGitRepositoryError as e:
     repo = None
     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):
 def filetime_from_git(content):
     if isinstance(content, contents.Static) or repo is None:
     if isinstance(content, contents.Static) or repo is None:
         return
         return
@@ -34,26 +42,26 @@ def filetime_from_git(content):
             with_extended_output=True, with_exceptions=False)
             with_extended_output=True, with_exceptions=False)
     if status != 0:
     if status != 0:
         # file is not managed by git
         # 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:
     else:
         # file is managed by git
         # file is managed by git
         commits = repo.commits(path=path)
         commits = repo.commits(path=path)
         if len(commits) == 0:
         if len(commits) == 0:
             # never commited, but staged
             # 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:
         else:
             # has commited
             # 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],
             status, stdout, stderr = git.execute(['git', 'diff', '--quiet', 'HEAD', path],
                     with_extended_output=True, with_exceptions=False)
                     with_extended_output=True, with_exceptions=False)
             if status != 0:
             if status != 0:
                 # file has changed
                 # file has changed
-                content.modified = datetime.fromtimestamp(os.stat(path).st_ctime)
+                content.modified = datetime_from_timestamp(os.stat(path).st_ctime, content)
             else:
             else:
                 # file is not changed
                 # file is not changed
                 if len(commits) > 1:
                 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'):
     if not hasattr(content, 'modified'):
         content.modified = content.date
         content.modified = content.date
     if hasattr(content, 'date'):
     if hasattr(content, 'date'):
@@ -61,9 +69,5 @@ def filetime_from_git(content):
     if hasattr(content, 'modified'):
     if hasattr(content, 'modified'):
         content.locale_modified = strftime(content.modified, content.date_format)
         content.locale_modified = strftime(content.modified, content.date_format)
 
 
-
-
-
-
 def register():
 def register():
     signals.content_object_init.connect(filetime_from_git)
     signals.content_object_init.connect(filetime_from_git)