From bb0e256a9892f5b70bb6465a27a7919f8851b60b Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 20 Apr 2016 16:51:01 -0400 Subject: [PATCH] Convert datetime objects to ISO8601 on the way out If a plugin iterates a datetime object, decode to ISO-8601 string on the way out. This allows plugins to work directly with datetime objects and allow the messaging layer to normalize it to ISO-8601 --- confluent_client/confluent/tlvdata.py | 3 +++ confluent_server/confluent/messages.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/confluent_client/confluent/tlvdata.py b/confluent_client/confluent/tlvdata.py index 9676a35a..93332a48 100644 --- a/confluent_client/confluent/tlvdata.py +++ b/confluent_client/confluent/tlvdata.py @@ -16,6 +16,7 @@ # limitations under the License. import confluent.tlv as tlv +from datetime import datetime import json import struct @@ -34,6 +35,8 @@ def unicode_dictvalues(dictdata): for key in dictdata: if isinstance(dictdata[key], str): dictdata[key] = decodestr(dictdata[key]) + elif isinstance(dictdata[key], datetime): + dictdata[key] = dictdata[key].strftime('%Y-%m-%dT%H:%M:%S') elif isinstance(dictdata[key], list): for i in xrange(len(dictdata[key])): if isinstance(dictdata[key][i], str): diff --git a/confluent_server/confluent/messages.py b/confluent_server/confluent/messages.py index 895c9782..27a684a3 100644 --- a/confluent_server/confluent/messages.py +++ b/confluent_server/confluent/messages.py @@ -19,6 +19,7 @@ # Things are defined here to 'encourage' developers to coordinate information # format. This is also how different data formats are supported import confluent.exceptions as exc +from datetime import datetime import json valid_health_values = set([ @@ -36,6 +37,8 @@ def _htmlify_structure(indict): ret += "
  • {0}: ".format(key) if type(indict[key]) in (str, unicode, float, int): ret += str(indict[key]) + elif isinstance(indict[key], datetime): + ret += indict[key].strftime('%Y-%m-%dT%H:%M:%S') else: ret += _htmlify_structure(indict[key]) elif isinstance(indict, list):