Mercurial > babel > old > mirror
comparison babel/dates.py @ 396:45a5b2266001
Doc improvements for the new `format_timedelta` function.
author | cmlenz |
---|---|
date | Tue, 15 Jul 2008 12:53:52 +0000 |
parents | dd0df1242ae3 |
children | ff9a6a37eb72 |
comparison
equal
deleted
inserted
replaced
395:98f06b9c44a7 | 396:45a5b2266001 |
---|---|
19 * ``LC_TIME``, | 19 * ``LC_TIME``, |
20 * ``LC_ALL``, and | 20 * ``LC_ALL``, and |
21 * ``LANG`` | 21 * ``LANG`` |
22 """ | 22 """ |
23 | 23 |
24 from __future__ import division | |
24 from datetime import date, datetime, time, timedelta, tzinfo | 25 from datetime import date, datetime, time, timedelta, tzinfo |
25 import re | 26 import re |
26 | 27 |
27 from babel.core import default_locale, get_global, Locale | 28 from babel.core import default_locale, get_global, Locale |
28 from babel.util import UTC | 29 from babel.util import UTC |
29 | 30 |
30 __all__ = ['format_date', 'format_datetime', 'format_time', | 31 __all__ = ['format_date', 'format_datetime', 'format_time', 'format_timedelta', |
31 'get_timezone_name', 'parse_date', 'parse_datetime', 'parse_time'] | 32 'get_timezone_name', 'parse_date', 'parse_datetime', 'parse_time'] |
32 __docformat__ = 'restructuredtext en' | 33 __docformat__ = 'restructuredtext en' |
33 | 34 |
34 LC_TIME = default_locale('LC_TIME') | 35 LC_TIME = default_locale('LC_TIME') |
35 | 36 |
597 ('hour', 3600), | 598 ('hour', 3600), |
598 ('minute', 60), | 599 ('minute', 60), |
599 ('second', 1) | 600 ('second', 1) |
600 ) | 601 ) |
601 | 602 |
602 def format_timedelta(delta, granularity='second', threshold=.9, locale=LC_TIME): | 603 def format_timedelta(delta, granularity='second', threshold=.85, locale=LC_TIME): |
603 """Return a time delta according to the rules of the given locale. | 604 """Return a time delta according to the rules of the given locale. |
604 | 605 |
605 >>> format_timedelta(timedelta(weeks=12), locale='en_US') | 606 >>> format_timedelta(timedelta(weeks=12), locale='en_US') |
606 u'3 months' | 607 u'3 months' |
607 >>> format_timedelta(timedelta(seconds=1), locale='es') | 608 >>> format_timedelta(timedelta(seconds=1), locale='es') |
608 u'1 segundo' | 609 u'1 segundo' |
609 >>> format_timedelta(timedelta(seconds=1), locale='en_US') | 610 |
610 u'1 second' | 611 The granularity parameter can be provided to alter the lowest unit |
611 | 612 presented, which defaults to a second. |
613 | |
614 >>> format_timedelta(timedelta(hours=3), granularity='day', | |
615 ... locale='en_US') | |
616 u'0 days' | |
617 | |
618 The threshold parameter can be used to determine at which value the | |
619 presentation switches to the next higher unit. A higher threshold factor | |
620 means the presentation will switch later. For example: | |
621 | |
622 >>> format_timedelta(timedelta(hours=23), threshold=0.9, locale='en_US') | |
623 u'1 day' | |
624 >>> format_timedelta(timedelta(hours=23), threshold=1.1, locale='en_US') | |
625 u'23 hours' | |
626 | |
612 :param delta: a ``timedelta`` object representing the time difference to | 627 :param delta: a ``timedelta`` object representing the time difference to |
613 format | 628 format, or the delta in seconds as an `int` value |
614 | 629 :param granularity: determines the smallest unit that should be displayed, |
615 """ | 630 the value can be one of "year", "month", "week", "day", |
631 "hour", "minute" or "second" | |
632 :param threshold: factor that determines at which point the presentation | |
633 switches to the next higher unit | |
634 :param locale: a `Locale` object or a locale identifier | |
635 :rtype: `unicode` | |
636 """ | |
637 if isinstance(delta, timedelta): | |
638 seconds = int((delta.days * 86400) + delta.seconds) | |
639 else: | |
640 seconds = delta | |
616 locale = Locale.parse(locale) | 641 locale = Locale.parse(locale) |
617 seconds = int((delta.days * 86400) + delta.seconds) | 642 |
618 | 643 for unit, secs_per_unit in TIMEDELTA_UNITS: |
619 for unit, limit in TIMEDELTA_UNITS: | 644 value = abs(seconds) / secs_per_unit |
620 r = float(abs(seconds)) / float(limit) | 645 if value >= threshold or unit == granularity: |
621 if r >= threshold or unit == granularity: | 646 value = int(round(value)) |
622 r = int(round(r)) | 647 plural_form = locale.plural_form(value) |
623 plural_form = locale.plural_form(r) | |
624 pattern = locale._data['unit_patterns'][unit][plural_form] | 648 pattern = locale._data['unit_patterns'][unit][plural_form] |
625 return pattern.replace('{0}', str(r)) | 649 return pattern.replace('{0}', str(value)) |
626 return '' | 650 |
651 return u'' | |
627 | 652 |
628 def parse_date(string, locale=LC_TIME): | 653 def parse_date(string, locale=LC_TIME): |
629 """Parse a date from a string. | 654 """Parse a date from a string. |
630 | 655 |
631 This function uses the date format for the locale as a hint to determine | 656 This function uses the date format for the locale as a hint to determine |
851 | 876 |
852 def format_day_of_year(self, num): | 877 def format_day_of_year(self, num): |
853 return self.format(self.get_day_of_year(), num) | 878 return self.format(self.get_day_of_year(), num) |
854 | 879 |
855 def format_day_of_week_in_month(self): | 880 def format_day_of_week_in_month(self): |
856 return '%d' % ((self.value.day - 1) / 7 + 1) | 881 return '%d' % ((self.value.day - 1) // 7 + 1) |
857 | 882 |
858 def format_period(self, char): | 883 def format_period(self, char): |
859 period = {0: 'am', 1: 'pm'}[int(self.value.hour >= 12)] | 884 period = {0: 'am', 1: 'pm'}[int(self.value.hour >= 12)] |
860 return get_period_names(locale=self.locale)[period] | 885 return get_period_names(locale=self.locale)[period] |
861 | 886 |
916 day_of_week = self.value.weekday() | 941 day_of_week = self.value.weekday() |
917 first_day = (day_of_week - self.locale.first_week_day - | 942 first_day = (day_of_week - self.locale.first_week_day - |
918 day_of_period + 1) % 7 | 943 day_of_period + 1) % 7 |
919 if first_day < 0: | 944 if first_day < 0: |
920 first_day += 7 | 945 first_day += 7 |
921 week_number = (day_of_period + first_day - 1) / 7 | 946 week_number = (day_of_period + first_day - 1) // 7 |
922 if 7 - first_day >= self.locale.min_week_days: | 947 if 7 - first_day >= self.locale.min_week_days: |
923 week_number += 1 | 948 week_number += 1 |
924 return week_number | 949 return week_number |
925 | 950 |
926 | 951 |