如何在 Python 中将 google.protobuf.timestamp_pb2.Timestamp 转换为 datetime

假设 timestamp 是你的 google.protobuf.timestamp_pb2.Timestamp 类型的 timestamp,使用此代码片段转换为 datetime:

timestamp-to-datetime.py
from datetime import datetime

timestamp_dt = datetime.fromtimestamp(timestamp.seconds + timestamp.nanos/1e9)

Check out similar posts by category: Python