Fixes issue 25602: Time fields behave properly when record is refreshed
authorAugusto Mauch <augusto.mauch@openbravo.com>
Fri, 31 Jan 2014 11:41:31 +0100
changeset 21991 68725a9cdf08
parent 21874 80961289f4cd
child 21992 1dc2a7cc6231
Fixes issue 25602: Time fields behave properly when record is refreshed

The value that is passed to TimeUIDefinition.convertToClassicString is taken from the database, so is stored in the server local timezone. It needs to be conv
erted to UTC before formatting it in a String
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/TimeUIDefinition.java
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/TimeUIDefinition.java	Wed Jan 22 06:52:02 2014 +0000
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/TimeUIDefinition.java	Fri Jan 31 11:41:31 2014 +0100
@@ -64,7 +64,8 @@
     }
     String timestamp = value.toString();
     timestamp = timestamp.substring(timestamp.indexOf(" ") + 1);
-    return timestamp;
+    StringBuffer convertedValue = convertLocalTimeToUTC(timestamp);
+    return convertedValue.toString();
   }
 
   private SimpleDateFormat getClassicFormat() {
@@ -109,6 +110,28 @@
     return localTimeColumnValue;
   }
 
+  private StringBuffer convertLocalTimeToUTC(String value) {
+    StringBuffer localTimeColumnValue = null;
+    try {
+      Date UTCDate = getClassicFormat().parse(value);
+      Calendar now = Calendar.getInstance();
+
+      Calendar calendar = Calendar.getInstance();
+      calendar.setTime(UTCDate);
+      calendar.set(Calendar.DATE, now.get(Calendar.DATE));
+      calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
+      calendar.set(Calendar.YEAR, now.get(Calendar.YEAR));
+
+      int gmtMillisecondOffset = (now.get(Calendar.ZONE_OFFSET) + now.get(Calendar.DST_OFFSET));
+      calendar.add(Calendar.MILLISECOND, -gmtMillisecondOffset);
+      localTimeColumnValue = getClassicFormat().format(calendar.getTime(), new StringBuffer(),
+          new FieldPosition(0));
+    } catch (ParseException e) {
+      throw new OBException("Exception when parsing date ", e);
+    }
+    return localTimeColumnValue;
+  }
+
   // getFieldProperties has to be overridden because depending on the value of getValueFromSession,
   // time fields have to be converted from localTime to UTC before sending the to the client
   @Override