<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Project><name>van.timeformat</name>
<shortdesc>Convienience functions for formatting dates/times using zope.i18n and TAL</shortdesc>
<description>.. contents::

Python Format Functions
=======================

The van.timefmt module is a support module for date/time specific operations.

    &gt;&gt;&gt; from datetime import date, datetime
    &gt;&gt;&gt; mydate = date(1975, 12, 17)
    &gt;&gt;&gt; mydatetime = datetime(1975, 12, 17, 5, 24, 36)

It provides a "timefmt" function which can take either a date or datetime object:

    &gt;&gt;&gt; from van.timeformat import ltimefmt, timefmt

Fixed formatting
----------------

Fixed formats are locale independant. They are useful in 2 situations:

* Computer parsable dates
* Projects with no localization requirement

default formatting
++++++++++++++++++

If no format argument is specified, dates and datetimes are formatted using .isoformat(" "):

    &gt;&gt;&gt; print timefmt(mydatetime)
    1975-12-17T05:24:36

    &gt;&gt;&gt; print timefmt(mydate)
    1975-12-17

The 'iso' format also triggers this:
    
    &gt;&gt;&gt; print timefmt(mydatetime, format='iso')
    1975-12-17T05:24:36

rfc2822
+++++++

The date in compliance with the RFC 2822 Internet email standard.

    &gt;&gt;&gt; print timefmt(mydate, 'rfc2822')
    Wed, 17 Dec 1975 00:00:00 +0000

    &gt;&gt;&gt; print timefmt(mydatetime, 'rfc2822')
    Wed, 17 Dec 1975 05:24:36 +0000


Extending formats
+++++++++++++++++

If we want to extend the list of formats available, we can use the
"time_format" zcml command defined in this module's meta.zcml.

An example of use is in configure.zcml where the rfc2822 format is defined.

Note: it's probably a good idea to use namespaces for registrations. The
van.timeformat module promises to not use "." in any of it's default
registrations.

Unicode
+++++++

The return type is a unicode string:

    &gt;&gt;&gt; timefmt(mydatetime)
    u'1975-12-17T05:24:36'

And we can have unicode in the formats:

    &gt;&gt;&gt; timefmt(mydatetime, format='unicode_test')
    u'1975-17-12 Extended Arabic-Indic Digit Seven: \u06f7:'

Locale dependant translations
-----------------------------

    &gt;&gt;&gt; from zope.i18n.locales import locales
    &gt;&gt;&gt; german = locales.getLocale('de', 'de')
    &gt;&gt;&gt; us = locales.getLocale('en', 'us')
    &gt;&gt;&gt; britain = locales.getLocale('en', 'gb')

Returns unicode:

    &gt;&gt;&gt; ltimefmt(mydate, us)
    u'Dec 17, 1975'

Defaults correctly chosen for date and datetime:

    &gt;&gt;&gt; print ltimefmt(mydate, us)
    Dec 17, 1975
    &gt;&gt;&gt; print ltimefmt(mydatetime, us)
    Dec 17, 1975 5:24:36 AM

But we can force format one as the other:

    &gt;&gt;&gt; print ltimefmt(mydate, us, category="dateTime")
    Dec 17, 1975 12:00:00 AM
    &gt;&gt;&gt; print ltimefmt(mydatetime, us, category="date")
    Dec 17, 1975

Localized:

    &gt;&gt;&gt; print ltimefmt(mydate, britain, category="date", length="long")
    17 December 1975
    &gt;&gt;&gt; print ltimefmt(mydate, german, category="date", length="long")
    17. Dezember 1975


Localized formatting examples
+++++++++++++++++++++++++++++

Short dates:

    &gt;&gt;&gt; print ltimefmt(mydate, us, category="date", length="short")
    12/17/75

Medium Dates:

    &gt;&gt;&gt; print ltimefmt(mydate, us, category="date", length="medium")
    Dec 17, 1975

Long Dates:

    &gt;&gt;&gt; print ltimefmt(mydate, us, category="date", length="long")
    December 17, 1975

Short Datetimes:

    &gt;&gt;&gt; print ltimefmt(mydatetime, us, category="dateTime", length="short")
    12/17/75 5:24 AM

Medium Datetimes:

    &gt;&gt;&gt; print ltimefmt(mydatetime, us, category="dateTime", length="medium")
    Dec 17, 1975 5:24:36 AM

Long Datetimes:

    &gt;&gt;&gt; print ltimefmt(mydatetime, us, category="dateTime", length="long")
    December 17, 1975 5:24:36 AM +000


Integration with ZPT
====================

If the zope.app.pagetemplate module is available, the timeformat module will integrate itself with it:

    &gt;&gt;&gt; import os
    &gt;&gt;&gt; import tempfile
    &gt;&gt;&gt; from zope.publisher.browser import TestRequest

    &gt;&gt;&gt; temp_file = tempfile.mkstemp()[1]
    &gt;&gt;&gt; open(temp_file, 'w').write("""
    ... &lt;html&gt;
    ...   &lt;body tal:define="mydatetime python:modules['datetime'].datetime(1975, 12, 17, 5, 24, 36)"&gt;
    ...       RFC 2822 date         : &lt;span tal:replace="timefmt:rfc2822:mydatetime" /&gt;
    ...       Medium Date           : &lt;span tal:replace="ltimefmt:date:medium:mydatetime" /&gt;
    ...       Medium DateTime       : &lt;span tal:replace="ltimefmt:dateTime:medium:mydatetime" /&gt;
    ...       Python Expr (ltimefmt): &lt;span tal:replace="ltimefmt:dateTime:long:python:modules['datetime'].date(1975, 12, 17)" /&gt;
    ...       Python Expr (timefmt) : &lt;span tal:replace="timefmt:rfc2822:python:modules['datetime'].date(1975, 12, 17)" /&gt;
    ...   &lt;/body&gt;
    ... &lt;/html&gt;
    ... """)

    &gt;&gt;&gt; from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
    &gt;&gt;&gt; Page = SimpleViewClass(temp_file, name='main.html')
    &gt;&gt;&gt; request = TestRequest()
    &gt;&gt;&gt; print Page(None, request)().strip() # doctest: +NORMALIZE_WHITESPACE
    &lt;html&gt;
      &lt;body&gt;
          RFC 2822 date         : Wed, 17 Dec 1975 05:24:36 +0000
          Medium Date           : 1975 12 17
          Medium DateTime       : 1975 12 17  05:24:36
          Python Expr (ltimefmt): 1975 12 17  00:00:00 +000
          Python Expr (timefmt) : Wed, 17 Dec 1975 00:00:00 +0000
      &lt;/body&gt;
    &lt;/html&gt;


Let's see if it works with spaces after the colon (at various places):

    &gt;&gt;&gt; open(temp_file, 'w').write("""
    ... &lt;html&gt;
    ...   &lt;body tal:define="mydatetime python:modules['datetime'].datetime(1975, 12, 17, 5, 24, 36)"&gt;
    ...       RFC 2822 date         : &lt;span tal:replace="timefmt: rfc2822:mydatetime" /&gt;
    ...       Medium Date           : &lt;span tal:replace="ltimefmt: date: medium:mydatetime" /&gt;
    ...       Medium DateTime       : &lt;span tal:replace="ltimefmt: dateTime:medium: mydatetime" /&gt;
    ...       Python Expr (ltimefmt): &lt;span tal:replace="ltimefmt: dateTime:long: python:modules['datetime'].date(1975, 12, 17)" /&gt;
    ...       Python Expr (timefmt) : &lt;span tal:replace="timefmt: rfc2822: python:modules['datetime'].date(1975, 12, 17)" /&gt;
    ...   &lt;/body&gt;
    ... &lt;/html&gt;
    ... """)

    &gt;&gt;&gt; Page = SimpleViewClass(temp_file, name='main_with_spaces.html')
    &gt;&gt;&gt; request = TestRequest()
    &gt;&gt;&gt; print Page(None, request)().strip() # doctest: +NORMALIZE_WHITESPACE
    &lt;html&gt;
      &lt;body&gt;
          RFC 2822 date         : Wed, 17 Dec 1975 05:24:36 +0000
          Medium Date           : 1975 12 17
          Medium DateTime       : 1975 12 17  05:24:36
          Python Expr (ltimefmt): 1975 12 17  00:00:00 +000
          Python Expr (timefmt) : Wed, 17 Dec 1975 00:00:00 +0000
      &lt;/body&gt;
    &lt;/html&gt;

CleanUp:

    &gt;&gt;&gt; os.remove(temp_file)


Changes
=======

1.0.0 (2008-11-21)
------------------

- Initial Release</description>
<homepage rdf:resource="http://pypi.python.org/pypi/van.timeformat" />
<maintainer><foaf:Person><foaf:name>Vanguardistas LLC</foaf:name>
</foaf:Person></maintainer>
<release><Version><revision>1.0.0</revision></Version></release>
</Project></rdf:RDF>