23 lines
659 B
Python
23 lines
659 B
Python
from datetime import datetime
|
|
|
|
|
|
def format_time_string(input_time_str):
|
|
try:
|
|
# 解析时区时间字符串为datetime对象
|
|
dt_object = datetime.strptime(input_time_str, "%a %b %d %H:%M:%S %Z %Y")
|
|
|
|
# 将datetime对象重新格式化为所需的字符串格式
|
|
formatted_time_str = dt_object.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
return formatted_time_str
|
|
except ValueError as e:
|
|
print("Error: Invalid time format.")
|
|
return None
|
|
|
|
|
|
# 测试函数
|
|
input_time_str = "Mon Aug 7 09:33:39 BST 2023"
|
|
formatted_time = format_time_string(input_time_str)
|
|
if formatted_time is not None:
|
|
print(formatted_time)
|