(Pythonを学ぶためにこれを行うと想定しているので、コードのエラーを指摘します)。
>>> years = SecondsSinceEpoch / 31540000
いやいやいやいやいや。あなたはそれをすることはできません。31536000秒の年もあれば、31622400秒の年もあります。
>>> if calendar.isleap(yeariterator) == True:
真の値が真であるかどうかをテストする必要はありません。:-) 行う:
>>> if calendar.isleap(yeariterator):
その代わり。
また、変更します。
>>> yeariterator = 1969
>>> iterator = 0
>>> while yeariterator < yearsfordayfunction:
>>> yeariterator = yeariterator + 1
に:
範囲内のyeariteratorの場合(1970、yearsfordayfunction):
これでエラーも修正されます。2009年以降まで停止しないため、1年の残り日数が105日であるため、答えは-105になります。
また、月ごとの計算にはあまり意味がありません。年々うまくいきます。
for yeariterator in range(1970, yearsfordayfunction):
if calendar.isleap(yeariterator) == True:
days = days - 366
else:
days = days - 365
そして、8つのスペースのインデントはたくさんあります。4がより一般的です。
また、年と日を2回計算するのではなく、1つの方法で計算します。
def YearDay():
SecondsSinceEpoch = int(time.time())
days = SecondsSinceEpoch // 86400 # Double slash means floored int.
year = 1970
while True:
if calendar.isleap(year):
days -= 366
else:
days -= 365
year += 1
if calendar.isleap(year):
if days <= 366:
return year, days
else:
if days <= 365:
return year, days
def MonthDay(year, day):
if calendar.isleap(year):
monthstarts = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
else:
monthstarts = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
month = 0
for start in monthstarts:
if start > day:
return month, day - monthstarts[month-1] + 1
month += 1