def tostr(dt): date = iso8601.parse(dt) return iso8601.tostring(date)
def as_text(self, include_date=0, include_note=0): """Convert instance into plain text, returning a string. If 'include_date' is true, the date will be included on the attribution line. If 'include_note' is true, any notes will also be formatted and returned as part of the string. """ # If there's more than one paragraph, each paragraph # will be indented by 4 spaces. Single paragraphs # aren't indented at all. if len(self.text) > 1: indent = 4 * " " else: indent = "" output = "" for i in range(len(self.text)): paragraph = self.text[i] start = 0 for j in range(len(paragraph)): t = paragraph[j] if (t.is_break() or t.is_preformatted()): output += format_paragraph_as_text(paragraph[start:j], indent) output += t.as_text() if t.is_preformatted(): output += '\n' start = j + 1 output += format_paragraph_as_text(paragraph[start:], indent) para = "" for i in ['author', 'source']: value = getattr(self, i) if value is not None: v = value.as_text() if para: para += ', ' v = v[:1].lower() + v[1:] para += v # Add the date if include_date and self.date: date = iso8601.parse(self.date) para += ' ' + time.strftime('(%Y)', time.gmtime(date)) if para: para = "-- " + para lines = textwrap.wrap(para, 75, initial_indent = 6*' ', subsequent_indent = 9*' ') para = '\n'.join(lines) + '\n' output += para if include_note and self.note: output += '\n' for paragraph in self.note: para = "" for t in paragraph: para += t.as_text() lines = textwrap.wrap(para, 75) output += '\n'.join(lines) + '\n' return output
def test_tostring(self): dt = iso8601.parse('1998-06-13T14:12Z') self.assertEqual(iso8601.tostring(dt), '1998-06-13T14:12Z') self.assertEqual(iso8601.tostring(dt, 'Z'), '1998-06-13T14:12Z') self.assertEqual(iso8601.tostring(dt, '-05:00'), '1998-06-13T09:12-05:00')