我有一个protobuf时间戳消息,我正试图从Python字典中解析它。
目标Protobuf消息如下:
message LogStatusSnapshot {
google.protobuf.Timestamp time = 1;
LogStatus.LogStatusEnum status = 2;
LogStatusLevel.LogStatusLevelEnum level = 3;
string hostname = 4;
}
Python字典如下所示:
tl_snapshot = {u'status': u'FAILED', u'level': u'TEST_COMPUTER', u'hostname': u'mfg-line12', u'time': u'2022-10-26T13:14:07.831052'}
我是这样分析的:
from google.protobuf.json_format import ParseDict
ParseDict(tl_snapshot, log_pb2.LogStatusSnapshot())
当我删除时间字段时,所有其他字段都会进行解析。我从ParseDict内部库中得到一个错误,说:
ParseError: Failed to parse time field: time data '2022-10' does not match format '%Y-%m-%dT%H:%M:%S'.
我打印了tl_snapshot["time"]
,并验证了它是一个长度为26个字符的可接受的RFC 3339日期字符串。除非我滥用API,否则谷歌的ProtobufParseDict
功能似乎会切断我传递的日期字符串的大部分字符
软件版本:
-Python 2.7
-Protobuf 3.6.1
您的dict的time
值不是符合RFC3339的值,因为它不包括时区偏移。
时间戳需要RFC3339。
如果您将Z
(对于祖鲁语,即UTC+0(附加到time
值,即2022-10-26T13:14:07.831052Z
,它将起作用。
您需要确保为time
生成的值符合RFC3339。Python 3.x库支持这一点。