Skip to content

Commit b3e2ac1

Browse files
committed
修复format_time时间提取不准确的问题
1 parent 58619a0 commit b3e2ac1

2 files changed

Lines changed: 7 additions & 19 deletions

File tree

feapder/utils/tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ def format_date(date, old_format="", new_format="%Y-%m-%d %H:%M:%S"):
14881488
%S 秒(00-59)
14891489
@param new_format: 输出的日期格式
14901490
---------
1491-
@result: 格式化后的日期,类型为字符串 如2017-4-17 3:27:12
1491+
@result: 格式化后的日期,类型为字符串 如2017-4-17 03:27:12
14921492
"""
14931493
if not date:
14941494
return ""
@@ -1629,7 +1629,8 @@ def format_time(release_time, date_format="%Y-%m-%d %H:%M:%S"):
16291629
else:
16301630
release_time = str(int(get_current_date("%Y")) - 1) + "-" + release_time
16311631

1632-
template = re.compile("(\d{4}-\d{1,2}-\d{1,2}) ?(\d.*)")
1632+
# 把日和小时粘在一起的拆开
1633+
template = re.compile("(\d{4}-\d{1,2}-\d{2})(\d{1,2})")
16331634
release_time = re.sub(template, r"\1 \2", release_time)
16341635
release_time = format_date(release_time, new_format=date_format)
16351636

tests/test_tools.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
from feapder.utils import tools
22

3-
times = [
4-
'十',
5-
'十一',
6-
'二十一',
7-
"二零一五年十一月三日十三点五十四分十1秒",
8-
]
9-
for i in times:
10-
date = tools.transform_lower_num(i)
11-
print(i,date)
12-
13-
143

154
date = tools.format_time("昨天3:10")
165
print(date)
17-
# assert date == "2021-03-15 00:00:00"
18-
196

7+
print(tools.format_date("2017年4月17日 3时27分12秒"))
208

21-
date = tools.format_time("昨天 3:10")
9+
date = tools.format_time("昨天")
2210
print(date)
2311

24-
date = tools.format_time("2021-11-5 14:18:10")
12+
date = tools.format_time("2021-11-05 14:18:10")
2513
print(date)
2614

27-
2815
date = tools.format_time("1 年前")
29-
print(date)
16+
print(date)

0 commit comments

Comments
 (0)