Mercurial > p > roundup > code
comparison roundup/date.py @ 5400:2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Tool-assisted patch. Those divisions that I thought must be integer
floor divisions and rely on Python 2 integer floor division semantics
are changed to use // (if any are actually meant to be floating-point
divisions, that would break things). One __div__ method is changed to
__truediv__ (with __div__ = __truediv__ for Python 2 compatibility).
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Tue, 24 Jul 2018 23:16:09 +0000 |
| parents | c26d88ec071e |
| children | 3fa026621f69 |
comparison
equal
deleted
inserted
replaced
| 5399:dccae35caa59 | 5400:2120f77554d5 |
|---|---|
| 562 sign = 1 | 562 sign = 1 |
| 563 else: | 563 else: |
| 564 sign = -1 | 564 sign = -1 |
| 565 diff = -diff | 565 diff = -diff |
| 566 S = diff%60 | 566 S = diff%60 |
| 567 M = (diff/60)%60 | 567 M = (diff//60)%60 |
| 568 H = (diff/(60*60))%24 | 568 H = (diff//(60*60))%24 |
| 569 d = diff/(24*60*60) | 569 d = diff//(24*60*60) |
| 570 return Interval((0, 0, d, H, M, S), sign=sign, | 570 return Interval((0, 0, d, H, M, S), sign=sign, |
| 571 translator=self.translator) | 571 translator=self.translator) |
| 572 | 572 |
| 573 def __cmp__(self, other, int_seconds=0): | 573 def __cmp__(self, other, int_seconds=0): |
| 574 """Compare this date to another date.""" | 574 """Compare this date to another date.""" |
| 893 i = fixTimeOverflow(i) | 893 i = fixTimeOverflow(i) |
| 894 return Interval(i, translator=self.translator) | 894 return Interval(i, translator=self.translator) |
| 895 # nope, no idea what to do with this other... | 895 # nope, no idea what to do with this other... |
| 896 raise TypeError("Can't add %r"%other) | 896 raise TypeError("Can't add %r"%other) |
| 897 | 897 |
| 898 def __div__(self, other): | 898 def __truediv__(self, other): |
| 899 """ Divide this interval by an int value. | 899 """ Divide this interval by an int value. |
| 900 | 900 |
| 901 Can't divide years and months sensibly in the _same_ | 901 Can't divide years and months sensibly in the _same_ |
| 902 calculation as days/time, so raise an error in that situation. | 902 calculation as days/time, so raise an error in that situation. |
| 903 """ | 903 """ |
| 916 | 916 |
| 917 months = int(months/other) | 917 months = int(months/other) |
| 918 | 918 |
| 919 sign = months<0 and -1 or 1 | 919 sign = months<0 and -1 or 1 |
| 920 m = months%12 | 920 m = months%12 |
| 921 y = months / 12 | 921 y = months // 12 |
| 922 return Interval((sign, y, m, 0, 0, 0, 0), | 922 return Interval((sign, y, m, 0, 0, 0, 0), |
| 923 translator=self.translator) | 923 translator=self.translator) |
| 924 | 924 |
| 925 else: | 925 else: |
| 926 # handle a day/time division | 926 # handle a day/time division |
| 930 seconds = int(seconds/other) | 930 seconds = int(seconds/other) |
| 931 | 931 |
| 932 sign = seconds<0 and -1 or 1 | 932 sign = seconds<0 and -1 or 1 |
| 933 seconds *= sign | 933 seconds *= sign |
| 934 S = seconds%60 | 934 S = seconds%60 |
| 935 seconds /= 60 | 935 seconds //= 60 |
| 936 M = seconds%60 | 936 M = seconds%60 |
| 937 seconds /= 60 | 937 seconds //= 60 |
| 938 H = seconds%24 | 938 H = seconds%24 |
| 939 d = seconds / 24 | 939 d = seconds // 24 |
| 940 return Interval((sign, 0, 0, d, H, M, S), | 940 return Interval((sign, 0, 0, d, H, M, S), |
| 941 translator=self.translator) | 941 translator=self.translator) |
| 942 # Python 2 compatibility: | |
| 943 __div__ = __truediv__ | |
| 942 | 944 |
| 943 def __repr__(self): | 945 def __repr__(self): |
| 944 return '<Interval %s>'%self.__str__() | 946 return '<Interval %s>'%self.__str__() |
| 945 | 947 |
| 946 def pretty(self): | 948 def pretty(self): |
| 947 ''' print up the date date using one of these nice formats.. | 949 ''' print up the date date using one of these nice formats.. |
| 948 ''' | 950 ''' |
| 949 _quarters = self.minute / 15 | 951 _quarters = self.minute // 15 |
| 950 if self.year: | 952 if self.year: |
| 951 s = self.ngettext("%(number)s year", "%(number)s years", | 953 s = self.ngettext("%(number)s year", "%(number)s years", |
| 952 self.year) % {'number': self.year} | 954 self.year) % {'number': self.year} |
| 953 elif self.month or self.day > 28: | 955 elif self.month or self.day > 28: |
| 954 _months = max(1, int(((self.month * 30) + self.day) / 30)) | 956 _months = max(1, int(((self.month * 30) + self.day) / 30)) |
| 1038 self.sign = -1 | 1040 self.sign = -1 |
| 1039 val = -val | 1041 val = -val |
| 1040 else: | 1042 else: |
| 1041 self.sign = 1 | 1043 self.sign = 1 |
| 1042 self.second = val % 60 | 1044 self.second = val % 60 |
| 1043 val = val / 60 | 1045 val = val // 60 |
| 1044 self.minute = val % 60 | 1046 self.minute = val % 60 |
| 1045 val = val / 60 | 1047 val = val // 60 |
| 1046 self.hour = val % 24 | 1048 self.hour = val % 24 |
| 1047 val = val / 24 | 1049 val = val // 24 |
| 1048 self.day = val | 1050 self.day = val |
| 1049 self.month = self.year = 0 | 1051 self.month = self.year = 0 |
| 1050 | 1052 |
| 1051 def setTranslator(self, translator): | 1053 def setTranslator(self, translator): |
| 1052 """Replace the translation engine | 1054 """Replace the translation engine |
| 1077 seconds = sign * (S + M*60 + H*60*60 + d*60*60*24) | 1079 seconds = sign * (S + M*60 + H*60*60 + d*60*60*24) |
| 1078 if seconds: | 1080 if seconds: |
| 1079 sign = seconds<0 and -1 or 1 | 1081 sign = seconds<0 and -1 or 1 |
| 1080 seconds *= sign | 1082 seconds *= sign |
| 1081 S = seconds%60 | 1083 S = seconds%60 |
| 1082 seconds /= 60 | 1084 seconds //= 60 |
| 1083 M = seconds%60 | 1085 M = seconds%60 |
| 1084 seconds /= 60 | 1086 seconds //= 60 |
| 1085 H = seconds%24 | 1087 H = seconds%24 |
| 1086 d = seconds / 24 | 1088 d = seconds // 24 |
| 1087 else: | 1089 else: |
| 1088 months = y*12 + m | 1090 months = y*12 + m |
| 1089 sign = months<0 and -1 or 1 | 1091 sign = months<0 and -1 or 1 |
| 1090 months *= sign | 1092 months *= sign |
| 1091 m = months%12 | 1093 m = months%12 |
| 1092 y = months/12 | 1094 y = months//12 |
| 1093 | 1095 |
| 1094 return (sign, y, m, d, H, M, S) | 1096 return (sign, y, m, d, H, M, S) |
| 1095 | 1097 |
| 1096 class Range: | 1098 class Range: |
| 1097 """Represents range between two values | 1099 """Represents range between two values |
