Commits

faassen committed 55c38eb

Use datetime throughout instead of timestamps too.

Comments (0)

Files changed (1)

                     link = get_url(parts),
                     description = parts['html_body'],
                     guid = PyRSS2Gen.Guid(get_url(parts)),
-                    pubDate = datetime.fromtimestamp(parts['created'])))
+                    pubDate = parts['created']))
 
     return PyRSS2Gen.RSS2(
         title = title,
     """Read and parse entries, return document parts as generated by docutils"""
     data = []
     for entry in entries:
-        with open(os.path.join(entries_path, entry)) as source:
+        path = os.path.join(entries_path, entry)
+        with open(path) as source:
             parts = get_data(source.read().decode("utf-8"))
             if not use_filename:
-                entry_timeinfo = get_entry_dates(entry, entries_path)
+                created, modified = get_hg_datetimes(path)
             else:
-                entry_timeinfo = get_filename_entry_dates(entry)
-            parts.update(entry_timeinfo)
+                created, modified = get_filename_datetimes(path)
+            parts['created'] = created
+            parts['modified'] = modified
             data.append(parts)
     return data
 
     """Convert a hgdate format string to unix time UTC"""
     return sum([int(i) for i in hgdate_string.split()])
 
-def get_entry_dates(entry, entries_path):
-    """Get (creation, last modification) as unix time UTC"""
-    repo = hgapi.Repo(entries_path)
-    times = repo.hg_log(template='{date|hgdate}\n', filespec=entry).split("\n")
-   
+def get_hg_datetimes(path):
+    repo = hgapi.Repo(os.path.dirname(path))
+    times = repo.hg_log(
+        template='{date|hgdate}\n', filespec=os.path.basename(path)).split("\n")
+
     if times == ['']: #not in repo yet
         print("Warning: %s is not version controlled" % (entry,))
-        created = modified = int(time())
-    else:
+        created = modified = datetime.now()
+        return created, modified
 
-        if not times[-1]: del times[-1]
-        created = hgdate_to_utc(times[-1])
-        modified = hgdate_to_utc(times[0])
-    return {'created': created, 'modified': modified}
+    if not times[-1]:
+        del times[-1]
+    created = hgdate_to_datetime(times[-1])
+    modified = hgdate_to_datetime(times[0])
+    return created, modified
 
-def get_filename_entry_dates(entry):
-    """Get creation and last modification based on filename as unix time UTC."""
-    year, month, day, extra = entry.split('-')
-    t = int(mktime(datetime(int(year), int(month), int(day)).timetuple()))
-    return {'created': t, 'modified': t}
+def hgdate_to_datetime(hgdate_string):
+    """Convert a hgdate format string to Python datetime."""
+    t = sum([int(i) for i in hgdate_string.split()])
+    return datetime.fromtimestamp(t)
 
-def format_timestamp(ts, template, dt_format):
-    dt = datetime.fromtimestamp(ts)
-    return dt.strftime(dt_format)
+def get_filename_datetimes(path):
+    year, month, day, extra = os.path.basename(path).split('-')
+    created = datetime(int(year), int(month), int(day))
+    modified = created
+    return created, modified
     
-def get_timestamp(created, modified, created_tpl, modified_tpl, dt_format):
+def get_timestamp(created_dt, modified_dt, created_tpl, modified_tpl, dt_format):
     """Return string representing timestamp in post"""
-    created_dt = datetime.fromtimestamp(created)
-    modified_dt = datetime.fromtimestamp(modified)
-
     result = created_tpl.substitute(timestamp=created_dt.strftime(dt_format))
     #Add modification date if last change is more than 10 minutes after creation
     if modified_dt - created_dt > timedelta(minutes=10):
                 'identifier': identifier(parts)
                 })
     #Sort by creation date
-    data.sort(cmp=lambda x, y: y['created']-x['created'])
+    data.sort(key=lambda parts: parts['created'])
+    data.reverse()
+    
     #Create links
     links = "\n".join([
             menu_link_tpl.substitute(