《python语言程序设计》2018版第6章第22题显示当前日期和时间利用程序清单2-7和6-13的内容修改。但日期差一天。

#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()

在这里插入图片描述

今天是31日,但是变成8月1日。哈哈,看看明天,大家试一下

  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

谢谢各位兄弟们的关注,谢谢破费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值