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.

## Answer

You can use f-strings to format integers with leading zeros as shown below. You may also be interested in `divmod`

which computes the quotient and remainder of a division operation:

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)

Output:

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.

But, there are easier ways to go about this using the `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.")

Output:

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.