我没按要求显示结果。但是内容差不都,关键。每个31日或者月底就时间出现偏差
# 之前已经做好的当前的小时、分、秒。
def currentTime_output():
currentTime = time.time()
totalSeconds = int(currentTime)
currentSecond = totalSeconds % 60
totalMinutes = totalSeconds // 60
currentMinutes = totalMinutes % 60
totalHours = totalMinutes // 60
currentHour = totalHours % 24
print("Current time is", currentHour + 8, ":", currentMinutes, ":", currentSecond, "北京时间")
# 区分是闰年还是非闰年
def numberOfDaysInAYear(year_num_t):
isLeapYear = (year_num_t % 4 == 0 and year_num % 100 != 0) or (year_num % 400 == 0)
if isLeapYear is True:
return 366
else:
return 365
def numberOfYearToMonth(year_num_t):
isLeapYear = (year_num_t % 4 == 0 and year_num % 100 != 0) or (year_num % 400 == 0)
if isLeapYear is True:
return True
else:
return False
# 计算从1970.1.1开始到现在一共的天数
def currentDay_num():
# total seconds number
currentTime = time.time()
totalSeconds = int(currentTime)
currentSecond = totalSeconds % 60
totalMinutes = totalSeconds // 60
currentMinutes = totalMinutes % 60
totalHours = totalMinutes // 60
currentTotalDays = totalHours // 24
# 差二天。我按加2补充1970.1.1但是不知道这样能不能算是准的呢。
return currentTotalDays + 1
# 通过剩余的天数和已知的年数来计算当前的月和日。但是差一天。
def monthToDays(num, years_n):
monthsToName = 0
while num >= 29 or num >= 28 or num >= 31 or num >= 30:
monthsToName += 1
if monthsToName == 1:
num - 31
elif monthsToName == 2:
if numberOfYearToMonth(years_n) is True:
num -= 29
else:
num -= 28
elif monthsToName == 3:
num -= 31
elif monthsToName == 4:
num -= 30
elif monthsToName == 5:
num -= 31
elif monthsToName == 6:
num -= 30
elif monthsToName == 7:
num -= 31
elif monthsToName == 8:
num -= 31
elif monthsToName == 9:
num -= 30
elif monthsToName == 10:
num -= 31
elif monthsToName == 11:
num -= 30
elif monthsToName == 12:
num -= 31
return f"{monthsToName}月 {num}日"
FIRST_OF_YEARS = 1970
year_num = FIRST_OF_YEARS
currentDays = currentDay_num()
while currentDays >= 365 or currentDays >= 366:
year_num += 1
a = numberOfDaysInAYear(year_num)
currentDays -= a
# 测试结果和实际日期差了一天。
print(f"{year_num} 年 {monthToDays(currentDays, year_num)}")
currentTime_output()