#2-7 original code
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, ":", currentMinutes, ":", currentSecond, "GMT")
2-7的代码可以很好的计算剩余时间然后得出当前几点几分。
但是本题要求我们求得当前日期和当前时间
我之前的思路是通过换算得出总共天数,然后一点点往后减
怎么进行相减
总天数减谁?
因为包含这365和366两组天数
通过闰年判断
2024.7.28
Continue code design
But I don‘t now yesterday will do code
and today I want use to subtraction number integer
old code is design
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, "GMT")
24.07.28
Ok I think find right way
but less right way
Is come go next design
right code
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)
totalHours = totalSeconds // 3600
currentTotalDays = totalHours // 24
# 差二天。我按加2补充1970.1.1但是不知道这样能不能算是准的呢。
return currentTotalDays + 2
# 通过剩余的天数和已知的年数来计算当前的月和日。但是差一天。
def monthToDays(years_n, num):
monthsToName = 0
while num >= 28:
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
monthsToName += 1
return f"{monthsToName}月 {num}日"
FIRST_OF_YEARS = 1970
year_num = FIRST_OF_YEARS
currentDays = currentDay_num()
while currentDays >= 365 or currentDays >= 366:
a = numberOfDaysInAYear(year_num)
currentDays -= a
year_num += 1
# 测试结果和实际日期差了一天。
print(f"{year_num} 年 {monthToDays(year_num, currentDays)}")
currentTime_output()