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

2024-04-19 13:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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.

2条回答

您可以使用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.

但是,使用^{}库和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.

@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))

返回:

03
10
-3
00

相关问题 更多 >