Naposledy aktivní 8 months ago

常用的 datetime 工具函式:轉格式、計算秒數差、解析時間字串、顯示幾分鐘前/剛剛/幾天前等人類友善時間字串,寫後台超實用 ✅

timmy revidoval tento gist 8 months ago. Přejít na revizi

Žádné změny

timmy revidoval tento gist 9 months ago. Přejít na revizi

1 file changed, 124 insertions

datetime_util.py(vytvořil soubor)

@@ -0,0 +1,124 @@
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"))
Novější Starší