当 minutes/seconds 达到 0 时,向数字时钟添加额外的 0

Adding additional 0 to a digital clock when minutes/seconds reach 0

I've tried adding multiple if statements but running 3 separate sections of the same if statements to account for needing to add an additional 0 to minutes or seconds seems cumbersome. Is there a better option?

current_time = time.time()

def process_time(current_time):
    days = int(current_time // 8640)
    hours = 24 * ((current_time / 8640) - days)
    whole_hours = int(hours)
    minutes = 60 * (hours - whole_hours)
    whole_minutes = int(minutes)
    seconds = 60 * (minutes - whole_minutes)
    whole_seconds = int(seconds)
    def select_time():
        if whole_hours >= 12:
            hr = str(whole_hours - 12)
            min = str(whole_minutes)
            sec = str(whole_seconds)
            print("It is", hr+ ":" +min+ ":" +sec, "PM.")
            print("It has been", days, "days since the epoch.")
        elif 0 < whole_hours < 12:
            hr = str(whole_hours)
            min = str(whole_minutes)
            sec = str(whole_seconds)
            print("It is", hr+ ":" +min+ ":" +sec, "AM.")
            print("It has been", days, "days since the epoch.")
        elif whole_hours == 0:
            hr = str(whole_hours + 12)
            min = str(whole_minutes)
            sec = str(whole_seconds)
            print("It is", hr+ ":" +min+ ":" +sec, "AM.")
            print("It has been", days, "days since the epoch.")

process_time(8640) # 8640 because of # of seconds in a day so that it's midnight

Output: It is 12:0:0 AM.
        It has been 1 days since the epoch.

@Abel 的评论有效,或者您可以创建一个小函数来添加 0:

#!/usr/bin/python3

def two_digits(number):
    if 0 <= number < 10:
        outnumber = '0' + str(number)
    else:
        outnumber = number
    return outnumber

print(two_digits(3))
print(two_digits(10))
print(two_digits(-3))
print(two_digits(0))

Returns:

03
10
-3
00

您可以使用 f 字符串来格式化带有前导零的整数,如下所示。您可能还对计算除法运算的商和余数的 divmod 感兴趣:

def process_time(secs):
    # divmod works on floats as well, so use int() to ensure integer result
    days,secs = divmod(int(secs), 24*60*60)
    half,secs = divmod(secs, 12*60*60) # half days to compute AM/PM
    hours,secs = divmod(secs, 60*60)
    mins,secs = divmod(secs,60)
    # f-strings can do computations for hours and AM/PM
    # 02 format means two digits with leading zeros
    print(f"It is {hours if hours else 12:02}:{mins:02}:{secs:02} {'PM' if half else 'AM'}.")
    print(f"It has been {days} days since the epoch.")

trials = 0,1,60,60*60,12*60*60-1,12*60*60,24*60*60-1,24*60*60
for s in trials:
    process_time(s)

输出:

It is 12:00:00 AM.
It has been 0 days since the epoch.
It is 12:00:01 AM.
It has been 0 days since the epoch.
It is 12:01:00 AM.
It has been 0 days since the epoch.
It is 01:00:00 AM.
It has been 0 days since the epoch.
It is 11:59:59 AM.
It has been 0 days since the epoch.
It is 12:00:00 PM.
It has been 0 days since the epoch.
It is 11:59:59 PM.
It has been 0 days since the epoch.
It is 12:00:00 AM.
It has been 1 days since the epoch.

但是,有更简单的方法可以使用 datetime library and Format Codes:

import time
from datetime import datetime, timezone, timedelta

EPOCH = datetime.fromtimestamp(0, timezone.utc)
print(f"The epoch is {EPOCH:%B %d, %Y %I:%M:%S %p %Z}.")

dt = datetime.now(timezone.utc)
diff = dt - EPOCH
print(f"It is {dt:%I:%M:%S %p %Z}.")
print(f"It has been {diff.days} days since the epoch.")

输出:

The epoch is January 01, 1970 12:00:00 AM UTC.
It is 03:27:38 AM UTC.
It has been 18879 days since the epoch.