import os
import re
from datetime import datetime
import pytz
pattern = re.compile(r"pubDatetime:\s*[\d\-T:]+Z")
utc_time = datetime.utcnow()
utc_timezone = pytz.timezone('UTC')
beijing_timezone = pytz.timezone('Asia/Shanghai')
beijing_time = utc_time.replace(tzinfo=utc_timezone).astimezone(beijing_timezone)
new_datetime = beijing_time.strftime("%Y-%m-%dT%H:%M:%SZ")
def traverse_and_update(path):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".md"):
filepath = os.path.join(root, file)
modFile = os.popen(f"git status --porcelain").read().strip()
if os.path.exists(filepath) and modFile:
fileName = filepath.split('/')[-1]
if fileName in modFile:
with open(filepath, "r+") as f:
content = f.read()
f.seek(0)
f.write(re.sub(pattern, f"pubDatetime: {new_datetime}", content))
f.truncate()
print(f"File {filepath} has been updated.")
continue
if os.path.isdir(".git"):
os.chdir("src/content/blog")
traverse_and_update(".")