Commits

faassen  committed 7968613

Support custom datetime format.

  • Participants
  • Parent commits 877e7fb

Comments (0)

Files changed (1)

 from docutils.core import publish_parts
 from hgapi import hgapi
 import PyRSS2Gen
-from ConfigParser import SafeConfigParser, NoSectionError
+from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
 import codecs
 
 def get_configuration(path):
     t = int(mktime(datetime(int(year), int(month), int(day)).timetuple()))
     return {'created': t, 'modified': t}
 
-def get_timestamp(created, modified, created_tpl, modified_tpl):
+def format_timestamp(ts, template, dt_format):
+    dt = datetime.fromtimestamp(ts)
+    return dt.strftime(dt_format)
+    
+def get_timestamp(created, modified, created_tpl, modified_tpl, dt_format):
     """Return string representing timestamp in post"""
-    cr, mod = [datetime.fromtimestamp(ts) for ts in created, modified]
-    ts_str = created_tpl.substitute(timestamp=cr.strftime("%y%m%d %H:%M"))
+    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 mod - cr > timedelta(minutes=10):
-        ts_str += modified_tpl.substitute(timestamp=mod.strftime("%y%m%d %H:%M"))
-    return ts_str
+    if modified_dt - created_dt > timedelta(minutes=10):
+        result += modified_tpl.substitute(
+            timestamp=modified_dt.strftime(dt_format))
+    return result
 
 class Templates(object):
     def __init__(self, templates_path):
         use_filename_for_creation_time = config.getboolean(
             'datetime',
             'use_filename_for_creation_time')
-    except NoSectionError:
+    except (NoSectionError, NoOptionError):
         use_filename_for_creation_time = False
         
     data = get_data_entries(
         return base_url + get_relative_url(parts)
     
     #Get timestamps from Mercurial
+    try:
+        dt_format = config.get('datetime', 'datetime_format')
+    except (NoSectionError, NoOptionError):
+        dt_format = '%y%m%d %H:%M'
     for parts in data:
         timestamp = timestamp_tpl.substitute(
             timestamps=get_timestamp(parts['created'], parts['modified'],
-                                     created_tpl, modified_tpl))
+                                     created_tpl, modified_tpl, dt_format))
         parts.update({
                 'timestamp': timestamp,
                 'identifier': identifier(parts)