datetime_util.py
· 3.1 KiB · Python
Sin formato
import math
import time
from datetime import datetime
# 轉換時間格式到字串(天)
def human_date(date=None):
if date:
assert isinstance(date, datetime)
else:
date = datetime.now()
return date.strftime("%Y-%m-%d")
# 轉換時間格式到字串
def human_datetime(date=None):
if date:
assert isinstance(date, datetime)
else:
date = datetime.now()
return date.strftime("%Y-%m-%d %H:%M:%S")
def human_time(date=None):
if date:
assert isinstance(date, datetime)
else:
date = datetime.now()
return date.strftime("%H:%M:%S")
# 解析時間類型的資料
def parse_time(value):
if isinstance(value, datetime):
return value
if isinstance(value, str):
if len(value) == 10:
return datetime.strptime(value, "%Y-%m-%d")
elif len(value) == 19:
return datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
raise TypeError("Expect a datetime.datetime value")
# 傳兩個時間得到一個時間差
def human_seconds_time(seconds):
text = ""
if seconds >= 3600:
text += "%d小時" % (seconds / 3600)
seconds = seconds % 3600
if seconds >= 60:
text += "%d分" % (seconds / 60)
seconds = seconds % 60
if seconds > 0:
if text or isinstance(seconds, int):
text += "%.d秒" % seconds
else:
text += "%.1f秒" % seconds
return text
def seconds_for_human(seconds):
second = 1
minute = second * 60
hour = minute * 60
day = hour * 24
lst = []
if seconds >= day:
days, seconds = divmod(seconds, day)
lst.append(str(days) + "d")
if seconds >= hour:
hours, seconds = divmod(seconds, hour)
lst.append(str(hours) + "h")
if seconds >= minute:
minutes, seconds = divmod(seconds, minute)
lst.append(str(minutes) + "m")
if seconds > 0:
lst.append(str(seconds) + "s")
return " ".join(lst)
def time_for_human(time_val):
second = 1
minute = second * 60
hour = minute * 60
day = hour * 24
days_8 = day * 8
if isinstance(time_val, datetime):
time_val = time_val.timestamp()
if isinstance(time_val, str):
time_val = time.mktime(time.strptime(time_val, "%Y-%m-%d %H:%M:%S"))
now = time.time()
duration = now - time_val
if duration < minute:
return "剛剛"
elif duration < hour:
return str(math.floor(duration / minute)) + "分鐘前"
elif duration < day:
return str(math.floor(duration / hour)) + "小時前"
elif duration < days_8:
return str(math.floor(duration / day)) + "天前"
else:
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_val))
if __name__ == "__main__":
print(human_datetime())
print(human_date())
print(human_time())
print(human_seconds_time(10))
print(seconds_for_human(86400 + 3600))
print(parse_time("2022-10-17 13:33:11"))
print(time_for_human(1665381270))
print(time_for_human(time.time() - 100))
print(time_for_human("2022-10-17 13:33:11"))
| 1 | import math |
| 2 | import time |
| 3 | from datetime import datetime |
| 4 | |
| 5 | |
| 6 | # 轉換時間格式到字串(天) |
| 7 | def human_date(date=None): |
| 8 | if date: |
| 9 | assert isinstance(date, datetime) |
| 10 | else: |
| 11 | date = datetime.now() |
| 12 | return date.strftime("%Y-%m-%d") |
| 13 | |
| 14 | |
| 15 | # 轉換時間格式到字串 |
| 16 | def human_datetime(date=None): |
| 17 | if date: |
| 18 | assert isinstance(date, datetime) |
| 19 | else: |
| 20 | date = datetime.now() |
| 21 | return date.strftime("%Y-%m-%d %H:%M:%S") |
| 22 | |
| 23 | |
| 24 | def human_time(date=None): |
| 25 | if date: |
| 26 | assert isinstance(date, datetime) |
| 27 | else: |
| 28 | date = datetime.now() |
| 29 | return date.strftime("%H:%M:%S") |
| 30 | |
| 31 | |
| 32 | # 解析時間類型的資料 |
| 33 | def parse_time(value): |
| 34 | if isinstance(value, datetime): |
| 35 | return value |
| 36 | if isinstance(value, str): |
| 37 | if len(value) == 10: |
| 38 | return datetime.strptime(value, "%Y-%m-%d") |
| 39 | elif len(value) == 19: |
| 40 | return datetime.strptime(value, "%Y-%m-%d %H:%M:%S") |
| 41 | raise TypeError("Expect a datetime.datetime value") |
| 42 | |
| 43 | |
| 44 | # 傳兩個時間得到一個時間差 |
| 45 | def human_seconds_time(seconds): |
| 46 | text = "" |
| 47 | if seconds >= 3600: |
| 48 | text += "%d小時" % (seconds / 3600) |
| 49 | seconds = seconds % 3600 |
| 50 | if seconds >= 60: |
| 51 | text += "%d分" % (seconds / 60) |
| 52 | seconds = seconds % 60 |
| 53 | if seconds > 0: |
| 54 | if text or isinstance(seconds, int): |
| 55 | text += "%.d秒" % seconds |
| 56 | else: |
| 57 | text += "%.1f秒" % seconds |
| 58 | return text |
| 59 | |
| 60 | |
| 61 | def seconds_for_human(seconds): |
| 62 | |
| 63 | second = 1 |
| 64 | minute = second * 60 |
| 65 | hour = minute * 60 |
| 66 | day = hour * 24 |
| 67 | lst = [] |
| 68 | |
| 69 | if seconds >= day: |
| 70 | days, seconds = divmod(seconds, day) |
| 71 | lst.append(str(days) + "d") |
| 72 | |
| 73 | if seconds >= hour: |
| 74 | hours, seconds = divmod(seconds, hour) |
| 75 | lst.append(str(hours) + "h") |
| 76 | |
| 77 | if seconds >= minute: |
| 78 | minutes, seconds = divmod(seconds, minute) |
| 79 | lst.append(str(minutes) + "m") |
| 80 | |
| 81 | if seconds > 0: |
| 82 | lst.append(str(seconds) + "s") |
| 83 | |
| 84 | return " ".join(lst) |
| 85 | |
| 86 | |
| 87 | def time_for_human(time_val): |
| 88 | second = 1 |
| 89 | minute = second * 60 |
| 90 | hour = minute * 60 |
| 91 | day = hour * 24 |
| 92 | days_8 = day * 8 |
| 93 | |
| 94 | if isinstance(time_val, datetime): |
| 95 | time_val = time_val.timestamp() |
| 96 | |
| 97 | if isinstance(time_val, str): |
| 98 | time_val = time.mktime(time.strptime(time_val, "%Y-%m-%d %H:%M:%S")) |
| 99 | |
| 100 | now = time.time() |
| 101 | duration = now - time_val |
| 102 | |
| 103 | if duration < minute: |
| 104 | return "剛剛" |
| 105 | elif duration < hour: |
| 106 | return str(math.floor(duration / minute)) + "分鐘前" |
| 107 | elif duration < day: |
| 108 | return str(math.floor(duration / hour)) + "小時前" |
| 109 | elif duration < days_8: |
| 110 | return str(math.floor(duration / day)) + "天前" |
| 111 | else: |
| 112 | return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_val)) |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | print(human_datetime()) |
| 117 | print(human_date()) |
| 118 | print(human_time()) |
| 119 | print(human_seconds_time(10)) |
| 120 | print(seconds_for_human(86400 + 3600)) |
| 121 | print(parse_time("2022-10-17 13:33:11")) |
| 122 | print(time_for_human(1665381270)) |
| 123 | print(time_for_human(time.time() - 100)) |
| 124 | print(time_for_human("2022-10-17 13:33:11")) |
| 125 |