Skip to content

Commit 5956981

Browse files
committed
Update hashnode.py
1 parent 3863243 commit 5956981

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

scripts/hashnode.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,24 @@ def sync_posts():
171171
for tag in metadata['tags']:
172172
tags_input.append({"slug": tag.lower(), "name": tag})
173173

174-
# 提取创建时间 (ISO 格式)
174+
# 提取创建时间 (ISO 格式,强制带时区)
175175
original_publish_date = None
176176
if 'date' in metadata and 'created' in metadata['date']:
177-
# 假设格式是 2026-01-23 09:13:53
178177
try:
179178
dt = metadata['date']['created']
180-
if isinstance(dt, datetime):
181-
original_publish_date = dt.isoformat()
179+
# 如果 yaml 解析出来不是 datetime 对象,先转换
180+
if not isinstance(dt, datetime):
181+
dt = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S")
182+
183+
# 关键修复:处理 ISO 格式
184+
# 1. isoformat() 生成 "2026-01-23T09:13:53"
185+
# 2. 如果没有时区信息 (tzinfo 为空),手动添加 "Z" 表示 UTC
186+
if dt.tzinfo is None:
187+
original_publish_date = dt.isoformat() + "Z"
182188
else:
183-
original_publish_date = datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S").isoformat()
184-
except:
185-
pass
189+
original_publish_date = dt.isoformat()
190+
except Exception as e:
191+
print(f"时间解析错误 {filename}: {e}")
186192

187193
# 3. 构建比对指纹
188194
local_hash = calculate_content_hash(title, description, body, tags_input)

0 commit comments

Comments
 (0)