-2

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.
emfastic
  • 31
  • 6
  • try the % on a format string. supports things that would be handled in printf/sprintf in c. https://stackoverflow.com/questions/517355/string-formatting-in-python – Abel Sep 09 '21 at 01:33
  • Please format your code to produce the output claimed. As show this outputs nothing. – Mark Tolonen Sep 09 '21 at 02:35

2 Answers2

2

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.
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Thanks, I really appreciate it. It was for a beginning exercise that we were instructed to only use what we had been working on in previous chapters (if statements, floor division, and modulus). Will definitely use datetime in future though. – emfastic Sep 09 '21 at 22:09
  • @emfastic Yes, it is hard to answer a question when the restrictions aren't known in advance. It's better to indicate it is a homework problem and what the restrictions are. In my opinion print formatting should be an early chapter. – Mark Tolonen Sep 09 '21 at 23:36
0

@Abel's comment is valid, or you could create a small function to add the 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
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Nic3500
  • 8,144
  • 10
  • 29
  • 40