Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ def get_restriction_time(string: str) -> typing.Optional[int]:
if number.isdigit() and letter in suffix: # if number contains only digits and letter can be converted to seconds
return int(number) * suffix[letter] # convert number to seconds
return None # if string is not correct we come here and return None

def restriction_time_to_human_readable(number: int) -> str:
"""
Convert number of seconds to human readable date
:number: seconds
:return: string with human readable data one or two nonzero values d,h,m,s or d h, h m, m s
"""
result=['навсегда'] # permanent ban by default
if number>=30 and number<=31622400: # if <30s or > 366d - permanent ban
day_order = [ 'd', 'h', 'm', 's'] # type of human readable date
day_div = [ 86400, 3600, 60, 1] # divider to convert to this type from seconds
day_last = len(day_order)-1
for i in range(day_last): # convert number to human readable from larger scale to smaller, except last unit
x, number = divmod(number, day_div[i]) # get current units and the rest seconds to number
if x: # if current units is not zero
result = [str(x) + day_order[i]] # save value and units to answer
y = number // day_div[i+1] # get next unit value
if y: # if next unit value is not zero
result.append(str(y) + day_order[i+1]) # append next unit and value to answer
break # break on first non zero unit found
else: # if no item in for fits
result = [str(number//day_div[day_last]) + day_order[day_last]] # use last units
return ' '.join(result) # unite items with space and return