Kaydet (Commit) 8bd9a2f7 authored tarafından Andrew M. Kuchling's avatar Andrew M. Kuchling

#687648 from Robert Schuppenies: use classic division. From me: don't use…

#687648 from Robert Schuppenies: use classic division.  From me: don't use string exception; add __main__ section
üst c2dc2692
...@@ -68,7 +68,7 @@ def _days_in_year(year): # number of days in year ...@@ -68,7 +68,7 @@ def _days_in_year(year): # number of days in year
return 365 + _is_leap(year) return 365 + _is_leap(year)
def _days_before_year(year): # number of days before year def _days_before_year(year): # number of days before year
return year*365L + (year+3)/4 - (year+99)/100 + (year+399)/400 return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400
def _days_in_month(month, year): # number of days in month of year def _days_in_month(month, year): # number of days in month of year
if month == 2 and _is_leap(year): return 29 if month == 2 and _is_leap(year): return 29
...@@ -92,9 +92,9 @@ def _num2date(n): # return date with ordinal n ...@@ -92,9 +92,9 @@ def _num2date(n): # return date with ordinal n
del ans.ord, ans.month, ans.day, ans.year # un-initialize it del ans.ord, ans.month, ans.day, ans.year # un-initialize it
ans.ord = n ans.ord = n
n400 = (n-1)/_DI400Y # # of 400-year blocks preceding n400 = (n-1)//_DI400Y # # of 400-year blocks preceding
year, n = 400 * n400, n - _DI400Y * n400 year, n = 400 * n400, n - _DI400Y * n400
more = n / 365 more = n // 365
dby = _days_before_year(more) dby = _days_before_year(more)
if dby >= n: if dby >= n:
more = more - 1 more = more - 1
...@@ -104,7 +104,7 @@ def _num2date(n): # return date with ordinal n ...@@ -104,7 +104,7 @@ def _num2date(n): # return date with ordinal n
try: year = int(year) # chop to int, if it fits try: year = int(year) # chop to int, if it fits
except (ValueError, OverflowError): pass except (ValueError, OverflowError): pass
month = min(n/29 + 1, 12) month = min(n//29 + 1, 12)
dbm = _days_before_month(month, year) dbm = _days_before_month(month, year)
if dbm >= n: if dbm >= n:
month = month - 1 month = month - 1
...@@ -174,7 +174,9 @@ def today(): ...@@ -174,7 +174,9 @@ def today():
local = time.localtime(time.time()) local = time.localtime(time.time())
return Date(local[1], local[2], local[0]) return Date(local[1], local[2], local[0])
DateTestError = 'DateTestError' class DateTestError(Exception):
pass
def test(firstyear, lastyear): def test(firstyear, lastyear):
a = Date(9,30,1913) a = Date(9,30,1913)
b = Date(9,30,1914) b = Date(9,30,1914)
...@@ -220,3 +222,6 @@ def test(firstyear, lastyear): ...@@ -220,3 +222,6 @@ def test(firstyear, lastyear):
(fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):
raise DateTestError, ('num->date failed', y) raise DateTestError, ('num->date failed', y)
y = y + 1 y = y + 1
if __name__ == '__main__':
test(1850, 2150)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment