Mercurial > babel > old > mirror
comparison babel/dates.py @ 244:cfb4cb07cbc7
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
author | cmlenz |
---|---|
date | Tue, 07 Aug 2007 21:41:35 +0000 |
parents | 2ed73d964211 |
children | d42e85b23272 |
comparison
equal
deleted
inserted
replaced
243:2ed73d964211 | 244:cfb4cb07cbc7 |
---|---|
728 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[max(3, num)] | 728 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[max(3, num)] |
729 era = int(self.value.year >= 0) | 729 era = int(self.value.year >= 0) |
730 return get_era_names(width, self.locale)[era] | 730 return get_era_names(width, self.locale)[era] |
731 | 731 |
732 def format_year(self, char, num): | 732 def format_year(self, char, num): |
733 if char.islower(): | 733 value = self.value.year |
734 value = self.value.year | 734 if char.isupper(): |
735 else: | 735 week = self.get_week_number(self.get_day_of_year()) |
736 value = self.value.isocalendar()[0] | 736 if week == 0: |
737 value -= 1 | |
737 year = self.format(value, num) | 738 year = self.format(value, num) |
738 if num == 2: | 739 if num == 2: |
739 year = year[-2:] | 740 year = year[-2:] |
740 return year | 741 return year |
741 | 742 |
746 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] | 747 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] |
747 return get_month_names(width, context, self.locale)[self.value.month] | 748 return get_month_names(width, context, self.locale)[self.value.month] |
748 | 749 |
749 def format_week(self, char, num): | 750 def format_week(self, char, num): |
750 if char.islower(): # week of year | 751 if char.islower(): # week of year |
751 week = self.get_week_number(self.get_day_of_year()) | 752 day_of_year = self.get_day_of_year() |
753 week = self.get_week_number(day_of_year) | |
752 if week == 0: | 754 if week == 0: |
753 # FIXME: I suppose this should return the last week number of | 755 date = self.value - timedelta(days=day_of_year) |
754 # the previous year | 756 week = self.get_week_number(self.get_day_of_year(date), |
755 pass | 757 date.weekday()) |
756 return self.format(week, num) | 758 return self.format(week, num) |
757 else: # week of month | 759 else: # week of month |
758 week = self.get_week_number(self.value.day) | 760 week = self.get_week_number(self.value.day) |
759 if week == 0: | 761 if week == 0: |
760 # FIXME: I suppose this should return the last week number of | 762 date = self.value - timedelta(days=self.value.day) |
761 # the previous month | 763 week = self.get_week_number(date.day, date.weekday()) |
762 pass | 764 pass |
763 return '%d' % week | 765 return '%d' % week |
764 | 766 |
765 def format_weekday(self, char, num): | 767 def format_weekday(self, char, num): |
766 if num < 3: | 768 if num < 3: |
808 return get_timezone_location(self.value.tzinfo, locale=self.locale) | 810 return get_timezone_location(self.value.tzinfo, locale=self.locale) |
809 | 811 |
810 def format(self, value, length): | 812 def format(self, value, length): |
811 return ('%%0%dd' % length) % value | 813 return ('%%0%dd' % length) % value |
812 | 814 |
813 def get_day_of_year(self): | 815 def get_day_of_year(self, date=None): |
814 return (self.value - date(self.value.year, 1, 1)).days + 1 | 816 if date is None: |
815 | 817 date = self.value |
816 def get_week_number(self, day_of_period): | 818 return (date - date_(date.year, 1, 1)).days + 1 |
819 | |
820 def get_week_number(self, day_of_period, day_of_week=None): | |
817 """Return the number of the week of a day within a period. This may be | 821 """Return the number of the week of a day within a period. This may be |
818 the week number in a year or the week number in a month. | 822 the week number in a year or the week number in a month. |
819 | 823 |
820 Usually this will return a value equal to or greater than 1, but if the | 824 Usually this will return a value equal to or greater than 1, but if the |
821 first week of the period is so short that it actually counts as the last | 825 first week of the period is so short that it actually counts as the last |
829 >>> format.get_week_number(6) | 833 >>> format.get_week_number(6) |
830 2 | 834 2 |
831 | 835 |
832 :param day_of_period: the number of the day in the period (usually | 836 :param day_of_period: the number of the day in the period (usually |
833 either the day of month or the day of year) | 837 either the day of month or the day of year) |
838 :param day_of_week: the week day; if ommitted, the week day of the | |
839 current date is assumed | |
834 """ | 840 """ |
835 first_day = (self.value.weekday() - self.locale.first_week_day - | 841 if day_of_week is None: |
842 day_of_week = self.value.weekday() | |
843 first_day = (day_of_week - self.locale.first_week_day - | |
836 day_of_period + 1) % 7 | 844 day_of_period + 1) % 7 |
837 if first_day < 0: | 845 if first_day < 0: |
838 first_day += 7 | 846 first_day += 7 |
839 week_number = (day_of_period + first_day - 1) / 7 | 847 week_number = (day_of_period + first_day - 1) / 7 |
840 if 7 - first_day >= self.locale.min_week_days: | 848 if 7 - first_day >= self.locale.min_week_days: |