Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import regex, sys
- ### Script that turns 24 hr clock string into 12 hr clock string with AM/PM ###
- time = sys.argv[1]
- pattern = "(\d{1,2}):(\d{1,2})"
- #~~~~~~~~~~~~~~~~~~~#
- #Error check no.1
- try:
- result = list(regex.findall(pattern, time)[0])
- #Error check no.2 (nested!)
- if len(result) != 2:
- print("Input cannot be parsed. Please format like this: `##:##`.")
- sys.exit(-1)
- except:
- print("Input cannot be parsed. Please format like this: `##:##`.")
- sys.exit(-1)
- result = [int(i) for i in result]
- #~~~~~~~~~~~~~~~~~~~#
- #Failure detection system (Error check no.3)
- failureReasons = {
- "Hours too high.": (result[0] > 23),
- "Minutes too high.": (result[1] > 59)
- }
- Failures = [i for i in failureReasons if failureReasons[i]]
- if Failures:
- #Lists any errors like this:
- # [!] Reason 1
- # [!] Reason 2
- print("Unable to run because of the following:\n{}".format("\n".join(["\t[!] " + i for i in Failures])))
- sys.exit(-1)
- #~~~~~~~~~~~~~~~~~~~#
- def main():
- if result[0] > 12:
- result[0] -= 12
- result.append("PM")
- else:
- result.append("AM")
- #Formats `result` array into a string
- #Prepends "0" to `m` if it doesn't have one, so that it appears as "2:04" for example, instead of "2:4".
- timeformatter = lambda h, m ,suf: "{}:{} {}".format(h, "0"+str(m) if len(str(m)) == 1 else m, suf)
- print("Time (12hr): {output}".format(output=timeformatter(*result)))
- #~~~~~~~~~~~~~~~~~~~#
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement