>>> format_time(d, "EEE, MMM d, ''yy", locale='en')
u"Sun, Apr 1, '07"
- If the pattern contains time fields, an `AttributeError` will be raised
- when trying to apply the formatting:
-
- >>> format_date(d, "yyyy-MM-dd HH:mm", locale='en_US')
- Traceback (most recent call last):
- ...
- AttributeError: 'datetime.date' object has no attribute 'hour'
-
- This is also true if the value of ``date`` parameter is a ``datetime``
- object, as this function automatically converts it to a ``date``::
-
- >>> dt = datetime(2007, 04, 01, 15, 30)
- >>> format_date(dt, "yyyy-MM-dd HH:mm", locale='en_US')
- Traceback (most recent call last):
- ...
- AttributeError: 'datetime.date' object has no attribute 'hour'
-
- :param date: the ``date`` object
+ :param date: the ``date`` or ``datetime`` object
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
- :param locale: a `Locale` object or a locale string
+ :param locale: a `Locale` object or a locale identifier
:rtype: `unicode`
+
+ :note: If the pattern contains time fields, an `AttributeError` will be
+ raised when trying to apply the formatting. This is also true if
+ the value of ``date`` parameter is actually a ``datetime`` object,
+ as this function automatically converts that to a ``date``.
"""
if isinstance(date, datetime):
date = date.date()
:param datetime: the ``date`` object
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
- :param locale: a `Locale` object or a locale string
+ :param locale: a `Locale` object or a locale identifier
:rtype: `unicode`
"""
locale = Locale.parse(locale)
>>> format_time(t, "hh 'o''clock' a", locale='en')
u"03 o'clock PM"
- If the pattern contains date fields, an `AttributeError` will be raised
- when trying to apply the formatting:
-
- >>> format_time(t, "yyyy-MM-dd HH:mm", locale='en_US')
- Traceback (most recent call last):
- ...
- AttributeError: 'datetime.time' object has no attribute 'year'
-
- This is also true if the value of ``time`` parameter is a ``datetime``
- object, as this function automatically converts it to a ``time``::
-
- >>> dt = datetime(2007, 04, 01, 15, 30)
- >>> format_time(dt, "yyyy-MM-dd HH:mm", locale='en_US')
- Traceback (most recent call last):
- ...
- AttributeError: 'datetime.time' object has no attribute 'year'
-
- :param time: the ``time`` object
+ :param time: the ``time`` or ``datetime`` object
:param format: one of "full", "long", "medium", or "short", or a custom
date/time pattern
- :param locale: a `Locale` object or a locale string
+ :param locale: a `Locale` object or a locale identifier
:rtype: `unicode`
+
+ :note: If the pattern contains date fields, an `AttributeError` will be
+ raised when trying to apply the formatting. This is also true if
+ the value of ``time`` parameter is actually a ``datetime`` object,
+ as this function automatically converts that to a ``time``.
"""
if isinstance(time, (int, long)):
time = datetime.fromtimestamp(time).time()
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
-from datetime import date, datetime
+from datetime import date, datetime, time
import doctest
import unittest
self.assertEqual('4', fmt['c']) # friday is first day of week
+class FormatDateTestCase(unittest.TestCase):
+
+ def test_with_time_fields_in_pattern(self):
+ self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+ def test_with_time_fields_in_pattern_and_datetime_param(self):
+ self.assertRaises(AttributeError, dates.format_date,
+ datetime(2007, 04, 01, 15, 30),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+
+class FormatTimeTestCase(unittest.TestCase):
+
+ def test_with_date_fields_in_pattern(self):
+ self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+ def test_with_date_fields_in_pattern_and_datetime_param(self):
+ self.assertRaises(AttributeError, dates.format_time,
+ datetime(2007, 04, 01, 15, 30),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(dates))
suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
+ suite.addTest(unittest.makeSuite(FormatDateTestCase))
+ suite.addTest(unittest.makeSuite(FormatTimeTestCase))
return suite
if __name__ == '__main__':