diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index af9da29..abff42e 100755 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -33,7 +33,7 @@ from IPython.config import Config from IPython.nbformat import current as nbformat from IPython.utils.traitlets import MetaHasTraits, DottedObjectName, Unicode, List, Dict, Any from IPython.utils.importstring import import_item -from IPython.utils.text import indent +from IPython.utils.text import indent, format_date from IPython.utils import py3compat from IPython.nbconvert import preprocessors as nbpreprocessors @@ -282,7 +282,7 @@ class Exporter(LoggingConfigurable): resources['metadata']['name'] = notebook_name modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) - resources['metadata']['modified_date'] = modified_date.strftime("%B %d, %Y") + resources['metadata']['modified_date'] = format_date(modified_date) with io.open(filename) as f: return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources,**kw) diff --git a/IPython/nbconvert/preprocessors/sphinx.py b/IPython/nbconvert/preprocessors/sphinx.py index 453a9c0..90cd063 100755 --- a/IPython/nbconvert/preprocessors/sphinx.py +++ b/IPython/nbconvert/preprocessors/sphinx.py @@ -30,6 +30,7 @@ from pygments.formatters import LatexFormatter # Our own imports # Configurable traitlets from IPython.utils.traitlets import Unicode, Bool +from IPython.utils.text import format_date # Needed to override preprocessor from .base import (Preprocessor) @@ -158,10 +159,7 @@ class SphinxPreprocessor(Preprocessor): if self.publish_date: resources["sphinx"]["date"] = self.publish_date elif len(resources['metadata']['modified_date'].strip()) == 0: - if sys.platform == 'win32': - resources["sphinx"]["date"] = date.today().strftime("%B %d, %Y") - else: - resources["sphinx"]["date"] = date.today().strftime("%B %-d, %Y") + resources["sphinx"]["date"] = format_date(date.today()) else: resources["sphinx"]["date"] = resources['metadata']['modified_date'] @@ -223,10 +221,7 @@ class SphinxPreprocessor(Preprocessor): if resources['metadata']['modified_date']: default_date = resources['metadata']['modified_date'] else: - if sys.platform == 'win32': - default_date = date.today().strftime("%B %d, %Y") - else: - default_date = date.today().strftime("%B %-d, %Y") + default_date = format_date(date.today()) user_date = console.input("Date (deafults to \"" + default_date + "\"): ") if len(user_date.strip()) == 0: diff --git a/IPython/utils/text.py b/IPython/utils/text.py index 55b8861..1a3546a 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -21,6 +21,7 @@ Inheritance diagram: import os import re +import sys import textwrap from string import Formatter @@ -711,3 +712,21 @@ def columnize(items, separator=' ', displaywidth=80): fmatrix = [filter(None, x) for x in matrix] sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])]) return '\n'.join(map(sjoin, fmatrix))+'\n' + + +def format_date(date): + """ Format a datetime object as a string. + + Parameters + ---------- + date : datetime object + Date to be formatted. + + Returns + ------- + The formatted date string (ie "July 1, 1990") + """ + if sys.platform == 'win32': + return date.strftime("%B {0}, %Y").format(date.day) + else: + return date.strftime("%B %-d, %Y") \ No newline at end of file