Fixes ISSUE-44640: Sets correct DateTime in window grid exported CSV file
authorCristian Berner <cristian.berner@openbravo.com>
Fri, 24 Jul 2020 13:25:16 +0200
changeset 37502 387029a95a3e
parent 37501 cfb8b02695d2
child 37503 8e534be16660
Fixes ISSUE 44640: Sets correct DateTime in window grid exported CSV file

DateTime in export to CSV was wrong, applying local time zone several times instead of once.

It has been fixed by using a function that doesn't try to transform to
UTC in DateTimeUIDefinition, convertToClassicStringInLocalTime. This
function formats the date as is, using the timezone received without
prior conversion to UTC.
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/DateTimeUIDefinition.java
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/DateTimeUIDefinition.java	Wed Jul 22 11:07:37 2020 +0200
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/DateTimeUIDefinition.java	Fri Jul 24 13:25:16 2020 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2010-2019 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2020 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -59,22 +59,46 @@
     if (value instanceof String) {
       return (String) value;
     }
+    Date date = (Date) value;
+    date = convertLocalDateTimeToUTC(date);
 
-    StringBuffer convertedValue = convertLocalDateTimeToUTC((Date) value);
-    return convertedValue.toString();
+    return formatDateTime(date);
   }
 
-  private StringBuffer convertLocalDateTimeToUTC(Date date) {
+  /**
+   * Creates a classic string which is used by callouts from an object value.
+   * Date is formatted as is, using local timezone
+   *
+   * @param value Object to be converted
+   * @return converted of formatted date string
+   */
+  public String convertToClassicStringInLocalTime(Object value) {
+    if (value == null || value == "") {
+      return "";
+    }
+
+    if (value instanceof String) {
+      return (String) value;
+    }
+    Date date = (Date) value;
+
+    return formatDateTime(date);
+  }
+
+  private Date convertLocalDateTimeToUTC(Date date) {
     Calendar calendar = Calendar.getInstance();
     calendar.setTime(date);
 
     int gmtMillisecondOffset = (calendar.get(Calendar.ZONE_OFFSET)
         + calendar.get(Calendar.DST_OFFSET));
     calendar.add(Calendar.MILLISECOND, -gmtMillisecondOffset);
+    return calendar.getTime();
+  }
+
+  private String formatDateTime(Date date) {
     SimpleDateFormat dateTimeFormat = getClassicFormat();
     synchronized (dateTimeFormat) {
-      return getClassicFormat().format(calendar.getTime(), new StringBuffer(),
-          new FieldPosition(0));
+      return getClassicFormat().format(date, new StringBuffer(), new FieldPosition(0)).toString();
     }
   }
 
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Wed Jul 22 11:07:37 2020 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Fri Jul 24 13:25:16 2020 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2019 Openbravo SLU
+ * All portions are Copyright (C) 2009-2020 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -66,6 +66,7 @@
 import org.openbravo.client.kernel.BaseKernelServlet;
 import org.openbravo.client.kernel.KernelUtils;
 import org.openbravo.client.kernel.OBUserException;
+import org.openbravo.client.kernel.reference.DateTimeUIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinitionController;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
@@ -652,8 +653,8 @@
             Date localDate = JsonUtils.createDateTimeFormat().parse(repairedString);
             Date clientTimezoneDate = null;
             clientTimezoneDate = convertFromLocalToClientTimezone(localDate);
-            keyValue = UIDefinitionController.DATETIME_UI_DEFINITION
-                .convertToClassicString(clientTimezoneDate);
+            keyValue = ((DateTimeUIDefinition) UIDefinitionController.DATETIME_UI_DEFINITION)
+                .convertToClassicStringInLocalTime(clientTimezoneDate);
           } else if (timeCols.contains(key) && keyValue != null
               && !keyValue.toString().equals("null")) {
             Date UTCdate = JsonUtils.createTimeFormatWithoutGMTOffset().parse(keyValue.toString());