Mercurial > babel > old > mirror
comparison babel/dates.py @ 239:4c5fc8879d03
Properly implement week-in-year (#46).
author | cmlenz |
---|---|
date | Tue, 07 Aug 2007 17:13:45 +0000 |
parents | d0cd235ede46 |
children | e37046445638 |
comparison
equal
deleted
inserted
replaced
238:d75cfd01f218 | 239:4c5fc8879d03 |
---|---|
743 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] | 743 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] |
744 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] | 744 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] |
745 return get_month_names(width, context, self.locale)[self.value.month] | 745 return get_month_names(width, context, self.locale)[self.value.month] |
746 | 746 |
747 def format_week(self, char, num): | 747 def format_week(self, char, num): |
748 # FIXME: this should really be based on the first_week_day and | 748 if char.islower(): # week of year |
749 # min_week_days locale data | 749 return self.format(self.get_week_number(self.get_day_of_year()), |
750 if char.islower(): | 750 num) |
751 return self.value.strftime('%W') | 751 else: # week of month |
752 else: | 752 # FIXME: this should really be based on the first_week_day and |
753 # min_week_days locale data | |
753 return '%d' % ((self.value.day + 6 - self.value.weekday()) / 7 + 1) | 754 return '%d' % ((self.value.day + 6 - self.value.weekday()) / 7 + 1) |
754 | 755 |
755 def format_weekday(self, char, num): | 756 def format_weekday(self, char, num): |
756 if num < 3: | 757 if num < 3: |
757 if char.islower(): | 758 if char.islower(): |
762 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] | 763 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] |
763 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] | 764 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] |
764 return get_day_names(width, context, self.locale)[weekday] | 765 return get_day_names(width, context, self.locale)[weekday] |
765 | 766 |
766 def format_day_of_year(self, num): | 767 def format_day_of_year(self, num): |
767 delta = self.value - date(self.value.year, 1, 1) | 768 return self.format(self.get_day_of_year(), num) |
768 return self.format(delta.days + 1, num) | |
769 | 769 |
770 def format_period(self, char): | 770 def format_period(self, char): |
771 period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)] | 771 period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)] |
772 return get_period_names(locale=self.locale)[period] | 772 return get_period_names(locale=self.locale)[period] |
773 | 773 |
795 uncommon=True, locale=self.locale) | 795 uncommon=True, locale=self.locale) |
796 return get_timezone_location(self.value.tzinfo, locale=self.locale) | 796 return get_timezone_location(self.value.tzinfo, locale=self.locale) |
797 | 797 |
798 def format(self, value, length): | 798 def format(self, value, length): |
799 return ('%%0%dd' % length) % value | 799 return ('%%0%dd' % length) % value |
800 | |
801 def get_day_of_year(self): | |
802 return (self.value - date(self.value.year, 1, 1)).days + 1 | |
803 | |
804 def get_week_number(self, day_of_period): | |
805 """Return the number of the week of a day within a period. This may be | |
806 the week number in a year or the week number in a month. | |
807 | |
808 Usually this will return a value equal to or greater than 1, but if the | |
809 first week of the period is so short that it actually counts as the last | |
810 week of the previous period, this function will return 0. | |
811 | |
812 >>> format = DateTimeFormat(date(2006, 1, 8), Locale.parse('de_DE')) | |
813 >>> format.get_week_number(6) | |
814 1 | |
815 | |
816 >>> format = DateTimeFormat(date(2006, 1, 8), Locale.parse('en_US')) | |
817 >>> format.get_week_number(6) | |
818 2 | |
819 | |
820 :param day_of_period: the number of the day in the period (usually | |
821 either the day of month or the day of year) | |
822 """ | |
823 first_day = (self.value.weekday() - self.locale.first_week_day - | |
824 day_of_period + 1) % 7 | |
825 if first_day < 0: | |
826 first_day += 7 | |
827 week_number = (day_of_period + first_day - 1) / 7 | |
828 if 7 - first_day >= self.locale.min_week_days: | |
829 week_number += 1 | |
830 return week_number | |
800 | 831 |
801 | 832 |
802 PATTERN_CHARS = { | 833 PATTERN_CHARS = { |
803 'G': [1, 2, 3, 4, 5], # era | 834 'G': [1, 2, 3, 4, 5], # era |
804 'y': None, 'Y': None, 'u': None, # year | 835 'y': None, 'Y': None, 'u': None, # year |