From f224cb1af1e1f7201e56e881dcba180867d69544 Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 22 Aug 2020 17:17:43 +0200 Subject: [PATCH] Show time string instead of ms in JSON structures --- .../androidaps/utils/JSONFormatter.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java index db1f6af239..d5e576b413 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java @@ -9,6 +9,9 @@ import org.json.JSONObject; import org.slf4j.Logger; import java.util.Iterator; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.text.DateFormat; import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; @@ -84,6 +87,7 @@ public class JSONFormatter { private String visit(final Object object, final int indent) throws JSONException { String ret = ""; + Long n; if (object instanceof JSONArray) { ret += visit((JSONArray) object, indent); } else if (object instanceof JSONObject) { @@ -92,7 +96,19 @@ public class JSONFormatter { if (object instanceof String) { ret += write("\"" + ((String) object).replace("<", "<").replace(">", ">") + "\"", indent); } else { - ret += write(String.valueOf(object), indent); + // try to detect Date as milliseconds + if (object instanceof Long) { + n = (Long) object; + if (n > 1580000000000L && n < 2000000000000L) { // from 2020.01.26 to 2033.05.18 it is with high probability a date object + Date date = new Date(n); + DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + ret += write(formatter.format(date), indent); + } else { + ret += write(String.valueOf(object), indent); + } + } else { + ret += write(String.valueOf(object), indent); + } } } return ret; @@ -107,4 +123,4 @@ public class JSONFormatter { return ret; } } -} \ No newline at end of file +}