|
8 | 8 | from dateutil.relativedelta import relativedelta |
9 | 9 |
|
10 | 10 | from .period import Period |
| 11 | +from .formatting.difference_formatter import DifferenceFormatter |
11 | 12 | from .mixins.default import TranslatableMixin, FormattableMixing, TestableMixin |
12 | 13 | from .constants import ( |
13 | 14 | DAYS_PER_WEEK, YEARS_PER_DECADE, YEARS_PER_CENTURY, |
@@ -44,6 +45,8 @@ class Date(TranslatableMixin, FormattableMixing, TestableMixin, date): |
44 | 45 |
|
45 | 46 | _MODIFIERS_VALID_UNITS = ['day', 'week', 'month', 'year', 'decade', 'century'] |
46 | 47 |
|
| 48 | + _diff_formatter = None |
| 49 | + |
47 | 50 | @classmethod |
48 | 51 | def instance(cls, dt): |
49 | 52 | """ |
@@ -609,6 +612,18 @@ def __sub__(self, other): |
609 | 612 |
|
610 | 613 | # DIFFERENCES |
611 | 614 |
|
| 615 | + @property |
| 616 | + def diff_formatter(self): |
| 617 | + """ |
| 618 | + Returns a DifferenceFormatter instance. |
| 619 | +
|
| 620 | + :rtype: DifferenceFormatter |
| 621 | + """ |
| 622 | + if not self.__class__._diff_formatter: |
| 623 | + self.__class__._diff_formatter = DifferenceFormatter(self.__class__.translator()) |
| 624 | + |
| 625 | + return self.__class__._diff_formatter |
| 626 | + |
612 | 627 | def diff(self, dt=None, abs=True): |
613 | 628 | """ |
614 | 629 | Returns the difference between two Date objects as a Period. |
@@ -655,50 +670,7 @@ def diff_for_humans(self, other=None, absolute=False, locale=None): |
655 | 670 |
|
656 | 671 | :rtype: str |
657 | 672 | """ |
658 | | - is_now = other is None |
659 | | - |
660 | | - if is_now: |
661 | | - other = self.today() |
662 | | - |
663 | | - diff = self.diff(other) |
664 | | - |
665 | | - if diff.years > 0: |
666 | | - unit = 'year' |
667 | | - count = diff.years |
668 | | - elif diff.months > 0: |
669 | | - unit = 'month' |
670 | | - count = diff.months |
671 | | - elif diff.weeks > 0: |
672 | | - unit = 'week' |
673 | | - count = diff.weeks |
674 | | - elif diff.days > 0: |
675 | | - unit = 'day' |
676 | | - count = diff.days |
677 | | - else: |
678 | | - unit = 'second' |
679 | | - count = diff.seconds |
680 | | - |
681 | | - if count == 0: |
682 | | - count = 1 |
683 | | - |
684 | | - time = self.translator().transchoice(unit, count, {'count': count}, locale=locale) |
685 | | - |
686 | | - if absolute: |
687 | | - return time |
688 | | - |
689 | | - is_future = diff.invert |
690 | | - |
691 | | - if is_now: |
692 | | - trans_id = 'from_now' if is_future else 'ago' |
693 | | - else: |
694 | | - trans_id = 'after' if is_future else 'before' |
695 | | - |
696 | | - # Some langs have special pluralization for past and future tense |
697 | | - try_key_exists = '%s_%s' % (unit, trans_id) |
698 | | - if try_key_exists != self.translator().transchoice(try_key_exists, count, locale=locale): |
699 | | - time = self.translator().transchoice(try_key_exists, count, {'count': count}, locale=locale) |
700 | | - |
701 | | - return self.translator().trans(trans_id, {'time': time}, locale=locale) |
| 673 | + return self.diff_formatter.diff_for_humans(self, other, absolute, locale) |
702 | 674 |
|
703 | 675 | # MODIFIERS |
704 | 676 |
|
|
0 commit comments