from datetime import datetime, timezone, timedelta # 确保导入了 timedelta
# ... 其余代码 ...
def format_timestamp(timestamp_str):
"""
将时间戳字符串转换为日本标准时间(JST)格式
"""
if timestamp_str:
try:
# 解析时间戳,包含时区信息
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%S.%f%z')
# 创建日本时区(UTC+9)
jst_timezone = timezone(timedelta(hours=9))
# 转换为日本时区
jst_time = timestamp.astimezone(jst_timezone)
return jst_time.strftime('%Y-%m-%d %H:%M:%S %Z')
except ValueError as e:
print(f"时间戳解析错误: {e}")
return 'N/A'
else:
return 'N/A'
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme