Created
December 7, 2020 22:17
-
-
Save John-Paul-R/bd0d428e90977e1ae54b628ce123785c to your computer and use it in GitHub Desktop.
Time String Parser (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def time_str_to_time_delta(stime: str): | |
match_days = re.search('([0-9]+)d', stime) | |
match_hrs = re.search('([0-9]+)h', stime) | |
match_mins = re.search('([0-9]+)m', stime) | |
match_secs = re.search('([0-9]+)s', stime) | |
def int_from_match(match) -> int: | |
out = 0 | |
if (match != None): | |
out = int(match.group(1)) | |
return out | |
idays = int_from_match(match_days) | |
ihrs = int_from_match(match_hrs) | |
imins = int_from_match(match_mins) | |
isecs = int_from_match(match_secs) | |
totalsecs = (idays * (60 * 60 * 24)) + (ihrs * (60 * 60)) + (imins * 60) + isecs | |
if (totalsecs == 0): | |
print("ERR: No time specified for tempmute command.") | |
else: | |
print ("d/h/m/s") | |
print ('%i/%i/%i/%i' % (idays, ihrs, imins, isecs), end=" - ") | |
print('(%s secs)' % totalsecs) | |
if __name__ == '__main__': | |
while(True): | |
i = input("Enter a time string (ex: 2d 10h 4m 3s) to start. (q to quit)\n") | |
if (i == 'q'): | |
break | |
time_str_to_time_delta(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment