Coverage for lino/utils/format_date.py : 85%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Copyright 2009-2015 by Luc Saffre. # License: BSD, see LICENSE for more details.
Date formatting functions -------------------------
To run the Lino test suite on this module::
$ python setup.py test -s tests.UtilsTests.test_format_date
This module provides shortcuts to `python-babel`'s `date formatting functions <http://babel.pocoo.org/docs/dates/>`_.
>>> from lino import startup >>> startup('lino.projects.min1.settings') >>> import datetime >>> d = datetime.date(2013,8,26) >>> print(fds(d)) # short 8/26/13 >>> print(fdm(d)) # medium Aug 26, 2013 >>> print(fdl(d)) # long August 26, 2013 >>> print(fdf(d)) # full Monday, August 26, 2013 >>> print(fdmy(d)) # full August 2013
The :func:`lino.core.format_date.format_date` function is a thin wrapper to the corresponding function in `babel.dates`, filling the `locale` parameter according to Django's current language (and doing the conversion).
The major advantage over using `date_format` from `django.utils.formats` is that Babel offers a "full" format:
>>> today = datetime.date(2013,01,18)
>>> print(format_date(today,'full')) Friday, January 18, 2013
>>> with translation.override('fr'): ... print(format_date(today,'full')) vendredi 18 janvier 2013
>>> with translation.override('de'): ... print(format_date(today,'full')) Freitag, 18. Januar 2013
You can use this also for languages that aren't on your site:
>>> with translation.override('et'): ... print(format_date(today,'full')) reede, 18. jaanuar 2013
>>> with translation.override('nl'): ... print(format_date(today,'full')) vrijdag 18 januari 2013
>>> print(fds('')) # empty string is tolerated <BLANKLINE> >>> print(fds('2014-10-12')) # not tolerated Traceback (most recent call last): ... Exception: Not a date: u'2014-10-12'
"""
""" Return the monthname for month # n in current language. """ d = datetime.date(2013, n, 1) return defaultfilters.date(d, 'F')
""" "format date as month and year" : return the specified date as a localized string of type 'June 2011'.""" return ''
"""Return the given date `d` formatted with `Babel's date formatting <http://babel.edgewall.org/wiki/Documentation/dates.html>`_ and using Django's current language.
""" d = d.as_date() lng = settings.SITE.languages[0].django_code
# backwards compatibility:
return format_date(d, "dd. MMMM")
|