# HG changeset patch # User cmlenz # Date 1216388282 0 # Node ID 7826d8e9998f0499706fb040b9af80b9b1598686 # Parent 3510257d57d3b0ed9d1ad99825d3a6f2a1f9fc9c Add `timedelta` function to the `Format` support class. diff --git a/babel/support.py b/babel/support.py --- a/babel/support.py +++ b/babel/support.py @@ -17,7 +17,7 @@ .. note: the code in this module is not used by Babel itself """ -from datetime import date, datetime, time +from datetime import date, datetime, time, timedelta import gettext try: @@ -26,7 +26,8 @@ from sets import set from babel.core import Locale -from babel.dates import format_date, format_datetime, format_time, LC_TIME +from babel.dates import format_date, format_datetime, format_time, \ + format_timedelta, LC_TIME from babel.numbers import format_number, format_decimal, format_currency, \ format_percent, format_scientific, LC_NUMERIC from babel.util import UTC @@ -91,6 +92,18 @@ """ return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale) + def timedelta(self, delta, granularity='second', threshold=.85): + """Return a time delta according to the rules of the given locale. + + >>> fmt = Format('en_US') + >>> fmt.timedelta(timedelta(weeks=11)) + u'3 months' + + :see: `babel.dates.format_timedelta` + """ + return format_timedelta(delta, granularity=granularity, + threshold=threshold, locale=self.locale) + def number(self, number): """Return an integer number formatted for the locale.