from datetime import datetime, timezone, timedelta from email.utils import parsedate_to_datetime class Times: @staticmethod def _js_date_parse(date_form: str) -> datetime: if date_form == "": raise ValueError("Invalid Date") try: if date_form.endswith("Z"): return datetime.fromisoformat(date_form.replace("Z", "+00:00")) return datetime.fromisoformat(date_form) except Exception: pass try: return datetime.strptime(date_form, "%Y-%m-%d %H:%M:%S") except Exception: pass try: return datetime.strptime(date_form, "%Y/%m/%d %H:%M:%S") except Exception: pass try: return parsedate_to_datetime(date_form) except Exception: pass raise ValueError("Invalid Date") @staticmethod def bj_time(date_form: str) -> str: if date_form == "": return "" dt = Times._js_date_parse(date_form) if dt.tzinfo is None: dt = dt.replace(tzinfo=timezone.utc) bj_dt = dt + timedelta(hours=8) return bj_dt.strftime("%Y-%m-%d %H:%M:%S") @staticmethod def utc_time(date_form: str) -> str: if date_form == "": return "" dt = Times._js_date_parse(date_form) if dt.tzinfo is None: dt = dt.replace(tzinfo=timezone.utc) utc_dt = dt.astimezone(timezone.utc) return utc_dt.strftime("%Y-%m-%d %H:%M:%S") @staticmethod def times(): now = datetime.now() y = now.strftime("%Y") M = now.strftime("%m") d = now.strftime("%d") h = now.strftime("%H") m = now.strftime("%M") s = now.strftime("%S") return [ f"{y}-{M}-{d} {h}:{m}", f"{y}-{M}-{d} {h}:{m}:{s}", f"{y}-{M}-{d}", f"{m}", f"{h}", f"{d}", ]