| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import time, math
- def time_formatter(seconds):
- minutes, seconds = divmod(seconds, 60)
- hours, minutes = divmod(minutes, 60)
- days, hours = divmod(hours, 24)
- tmp = (
- ((str(days) + " day(s), ") if days else "")
- + ((str(hours) + " hour(s), ") if hours else "")
- + ((str(minutes) + " minute(s), ") if minutes else "")
- + ((str(seconds) + " second(s), ") if seconds else "")
- )
- return tmp[:-2]
- def time_parser(start, end):
- time_end = end - start
- month = time_end // 2678400
- days = time_end // 86400
- hours = time_end // 3600 % 24
- minutes = time_end // 60 % 60
- seconds = time_end % 60
- times = ""
- if month:
- times += "{} month, ".format(month)
- if days:
- times += "{} days, ".format(days)
- if hours:
- times += "{} hours, ".format(hours)
- if minutes:
- times += "{} minutes, ".format(minutes)
- if seconds:
- times += "{} seconds".format(seconds)
- if times == "":
- times = "{} miliseconds".format(time_end)
- return times
- def time_parser_int(time_end):
- month = time_end // 2678400
- days = time_end // 86400
- hours = time_end // 3600 % 24
- minutes = time_end // 60 % 60
- seconds = time_end % 60
- times = ""
- if month:
- times += "{} month, ".format(month)
- if days:
- times += "{} days, ".format(days)
- if hours:
- times += "{} hours, ".format(hours)
- if minutes:
- times += "{} minutes, ".format(minutes)
- if seconds:
- times += "{} seconds".format(seconds)
- if times == "":
- times = "{} miliseconds".format(time_end)
- return times[:-2] if times[-2:] == ", " else times
- def get_readable_time(seconds: int) -> str:
- count = 0
- ping_time = ""
- time_list = []
- time_suffix_list = ["s", "m", "h", "days"]
- while count < 4:
- count += 1
- remainder, result = divmod(seconds, 60) if count < 3 else divmod(seconds, 24)
- if seconds == 0 and remainder == 0:
- break
- time_list.append(int(result))
- seconds = int(remainder)
- for x in range(len(time_list)):
- time_list[x] = str(time_list[x]) + time_suffix_list[x]
- if len(time_list) == 4:
- ping_time += time_list.pop() + ", "
- time_list.reverse()
- ping_time += ":".join(time_list)
- return ping_time
- def convert_size(size_bytes):
- if size_bytes == 0:
- return "0B"
- size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
- i = int(math.floor(math.log(size_bytes, 1024)))
- p = math.pow(1024, i)
- s = round(size_bytes / p, 2)
- return "%s %s" % (s, size_name[i])
- def extract_time(time_val):
- if not any(time_val.endswith(unit) for unit in ("m", "h", "d")):
- return False
- unit = time_val[-1]
- time_num = time_val[:-1] # type: str
- if not time_num.isdigit():
- return False
- if unit == "d":
- bantime = int(time.time() + int(time_num) * 24 * 60 * 60)
- elif unit == "h":
- bantime = int(time.time() + int(time_num) * 60 * 60)
- elif unit == "m":
- bantime = int(time.time() + int(time_num) * 60)
- else:
- return False
- return bantime
- def make_time(time_val):
- if int(time_val) == 0:
- return "0"
- if int(time_val) <= 3600:
- bantime = str(int(time_val / 60)) + "m"
- elif int(time_val) >= 3600 and time_val <= 86400:
- bantime = str(int(time_val / 60 / 60)) + "h"
- elif int(time_val) >= 86400:
- bantime = str(int(time_val / 24 / 60 / 60)) + "d"
- return bantime
- def extract_time_set(time_val):
- if not any(time_val.endswith(unit) for unit in ("m", "h", "d")):
- return False
- unit = time_val[-1]
- time_num = time_val[:-1] # type: str
- if not time_num.isdigit():
- return False
- if unit == "d":
- bantime = int(int(time_num) * 24 * 60 * 60)
- elif unit == "h":
- bantime = int(int(time_num) * 60 * 60)
- elif unit == "m":
- bantime = int(int(time_num) * 60)
- else:
- return False
- return bantime
- def escape_invalid_curly_brackets(text, valids): # sourcery skip
- new_text = ""
- idx = 0
- while idx < len(text):
- if text[idx] == "{":
- if idx + 1 < len(text) and text[idx + 1] == "{":
- idx += 2
- new_text += "{{{{"
- continue
- else:
- success = False
- for v in valids:
- if text[idx:].startswith("{" + v + "}"):
- success = True
- break
- if success:
- new_text += text[idx : idx + len(v) + 2]
- idx += len(v) + 2
- continue
- else:
- new_text += "{{"
- elif text[idx] == "}":
- if idx + 1 < len(text) and text[idx + 1] == "}":
- idx += 2
- new_text += "}}}}"
- continue
- else:
- new_text += "}}"
- else:
- new_text += text[idx]
- idx += 1
- return new_text
|