《python语言程序设计》2018年版第6章31题调用time.time()返回从1970年1月1日0点开始显示当前日期和时间

在这里插入图片描述

我没按要求显示结果。但是内容差不都,关键。每个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()
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

电饭叔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值