我想創建一個代碼,告訴我它基於多少小時過去的日期。如何處理python中的模0
我不想導入任何我只想自己計算的庫。我希望代碼具有靈活性,可以與任何snodiy(一年中的簡單天數),nohiod(一天中的小時數)以及我將提供的月數相配合。只有絕對的時間永遠是相同的,有60分鐘。
下面的代碼工作得很好,但問題是,而不是12個月我有13(0-12),我不知道如何以最有效的方式處理0。下面的例子。我有幾個小時給了我0天0個月和正確的年份(2016年)。
dev = True
# simple number of days in year
snodiy = 365.25
# number of hours in one day
nohiod = 24
# total hours passed
# 17672256 current year
thp = 17672257
dayc = 30 # number of days in a month #
# get date
def gd():
year = (thp/snodiy)/nohiod
rest = thp % (snodiy * nohiod)
if dev:
print("dev year rest:", rest)
month = rest/(dayc * nohiod)
rest %= (dayc * nohiod)
if dev:
print("dev month rest:", rest)
day = rest/nohiod
rest %= nohiod
if dev:
print("dev hour rest:", rest)
hour = rest
if dev:
print("Year:", "%d" % year)
print("Month:", "%d" % month)
print("Day:", "%d" % day)
print("Hour:", "%d" % hour)
print("Hour:", "%d" % hour, "of", "%d" % day, "%d" % month, "%d" % year)
gd()
這是一個壞主意,計算有關日期的事情,而不使用庫。並非所有的小時都包含60分鐘(例如閏秒),有些日子有25小時(在夏令時期間),以此類推。日曆庫嘗試隱藏所有這些可怕的東西:我建議使用一個。 – tehwalrus
不能在打印時將數據增加1嗎? – Nf4r
#Nf4r這會給我1-13問題 – Hsin