This is an automated email from the ASF dual-hosted git repository.
jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/develop by this push:
new 0e15125 GEODE-8447: add seconds to the localized pattern (#5891)
0e15125 is described below
commit 0e15125f8090e79fa8c6a1e05a28d535b9eeadd4
Author: Jinmei Liao <[email protected]>
AuthorDate: Tue Jan 12 10:26:30 2021 -0800
GEODE-8447: add seconds to the localized pattern (#5891)
---
.../DistributedSystemMBeanIntegrationTest.java | 5 +--
.../geode/internal/logging/DateFormatter.java | 43 ++++++++++++++++++++++
.../internal/json/QueryResultFormatter.java | 7 ++--
.../geode/internal/logging/DateFormatterTest.java | 35 ++++++++++++++++++
.../internal/json/QueryResultFormatterTest.java | 13 +++++--
5 files changed, 92 insertions(+), 11 deletions(-)
diff --git
a/geode-core/src/integrationTest/java/org/apache/geode/management/internal/beans/DistributedSystemMBeanIntegrationTest.java
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/beans/DistributedSystemMBeanIntegrationTest.java
index 2c662f0..45f5371 100644
---
a/geode-core/src/integrationTest/java/org/apache/geode/management/internal/beans/DistributedSystemMBeanIntegrationTest.java
+++
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/beans/DistributedSystemMBeanIntegrationTest.java
@@ -29,8 +29,8 @@ import org.junit.Test;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionShortcut;
+import org.apache.geode.internal.logging.DateFormatter;
import org.apache.geode.management.DistributedSystemMXBean;
-import org.apache.geode.management.internal.json.QueryResultFormatter;
import org.apache.geode.management.model.Employee;
import org.apache.geode.test.junit.assertions.TabularResultModelAssert;
import org.apache.geode.test.junit.rules.GfshCommandRule;
@@ -87,8 +87,7 @@ public class DistributedSystemMBeanIntegrationTest {
// this is to make sure dates are formatted correctly and it does not honor
the json annotations
@Test
public void queryAllUsingMBean() throws Exception {
- SimpleDateFormat formater =
- new SimpleDateFormat(QueryResultFormatter.DATE_FORMAT_PATTERN);
+ SimpleDateFormat formater = DateFormatter.createLocalizedDateFormat();
String dateString = formater.format(date);
String result = bean.queryData(SELECT_ALL, "server", 100);
System.out.println(result);
diff --git
a/geode-core/src/main/java/org/apache/geode/internal/logging/DateFormatter.java
b/geode-core/src/main/java/org/apache/geode/internal/logging/DateFormatter.java
index a8277ea..0524e21 100644
---
a/geode-core/src/main/java/org/apache/geode/internal/logging/DateFormatter.java
+++
b/geode-core/src/main/java/org/apache/geode/internal/logging/DateFormatter.java
@@ -19,6 +19,7 @@ package org.apache.geode.internal.logging;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.Locale;
/**
@@ -32,6 +33,11 @@ public class DateFormatter {
public static final String FORMAT_STRING = "yyyy/MM/dd HH:mm:ss.SSS z";
/**
+ * the format string used to format the date/time when localized pattern is
not ideal
+ */
+ public static final String LOCALIZED_FORMAT_STRING = "EEE yyyy/MM/dd
HH:mm:ss zzz";
+
+ /**
* Creates a SimpleDateFormat using {@link #FORMAT_STRING}.
*
* Thread Safety Issue: (From SimpleDateFormat) Date formats are not
synchronized. It is
@@ -42,6 +48,43 @@ public class DateFormatter {
return new SimpleDateFormat(FORMAT_STRING);
}
+ public static SimpleDateFormat createLocalizedDateFormat() {
+ String pattern = new SimpleDateFormat().toLocalizedPattern();
+ return new SimpleDateFormat(getModifiedLocalizedPattern(pattern),
Locale.getDefault());
+ }
+
+ static String getModifiedLocalizedPattern(String pattern) {
+ String pattern_to_use;
+ // will always try to use the localized pattern if the pattern displays
the minutes
+ if (pattern.contains("mm")) {
+ // if the localized pattern does not display seconds, add it in the
pattern
+ if (!pattern.contains("ss")) {
+ int mm = pattern.indexOf("mm");
+ pattern_to_use = pattern.substring(0, mm + 2) + ":ss" +
pattern.substring(mm + 2);
+ }
+ // if the localized pattern already contains seconds, the pattern should
be enough
+ else {
+ pattern_to_use = pattern;
+ }
+
+ // add the days of week to the pattern if not there yet
+ if (!pattern_to_use.contains("EEE")) {
+ pattern_to_use = "EEE " + pattern_to_use;
+ }
+
+ if (!pattern_to_use.contains("zzz")) {
+ pattern_to_use = pattern_to_use + " zzz";
+ }
+ }
+ // if the localized pattern doesn't even contain the minutes, then giving
up using the
+ // localized pattern since we have no idea what it is
+ else {
+ pattern_to_use = LOCALIZED_FORMAT_STRING;
+ }
+
+ return pattern_to_use;
+ }
+
/**
* Creates a SimpleDateFormat using specified formatString.
*/
diff --git
a/geode-core/src/main/java/org/apache/geode/management/internal/json/QueryResultFormatter.java
b/geode-core/src/main/java/org/apache/geode/management/internal/json/QueryResultFormatter.java
index bbd5523..6eefcba 100644
---
a/geode-core/src/main/java/org/apache/geode/management/internal/json/QueryResultFormatter.java
+++
b/geode-core/src/main/java/org/apache/geode/management/internal/json/QueryResultFormatter.java
@@ -41,10 +41,9 @@ import
com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
import com.fasterxml.jackson.databind.type.ArrayType;
-public class QueryResultFormatter extends AbstractJSONFormatter {
+import org.apache.geode.internal.logging.DateFormatter;
- public static final String DATE_FORMAT_PATTERN =
- "EEE " + new SimpleDateFormat().toLocalizedPattern() + " zzz";
+public class QueryResultFormatter extends AbstractJSONFormatter {
/**
* map contains the named objects to be serialized
*/
@@ -80,7 +79,7 @@ public class QueryResultFormatter extends
AbstractJSONFormatter {
// Consistency: use the same date format java.sql.Date as well as
java.util.Date.
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
+ SimpleDateFormat sdf = DateFormatter.createLocalizedDateFormat();
mapper.setDateFormat(sdf);
typeModule.addSerializer(java.sql.Date.class, new
SqlDateSerializer(mapper.getDateFormat()));
diff --git
a/geode-core/src/test/java/org/apache/geode/internal/logging/DateFormatterTest.java
b/geode-core/src/test/java/org/apache/geode/internal/logging/DateFormatterTest.java
new file mode 100644
index 0000000..a09c19d
--- /dev/null
+++
b/geode-core/src/test/java/org/apache/geode/internal/logging/DateFormatterTest.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache
License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
License. You may obtain a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express
+ * or implied. See the License for the specific language governing permissions
and limitations under
+ * the License.
+ */
+
+package org.apache.geode.internal.logging;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Test;
+
+public class DateFormatterTest {
+
+ @Test
+ public void getModifiedLocalizedPattern() throws Exception {
+ assertThat(DateFormatter.getModifiedLocalizedPattern("M/d/yy h:mm a"))
+ .isEqualTo("EEE M/d/yy h:mm:ss a zzz");
+ assertThat(DateFormatter.getModifiedLocalizedPattern("M/d/yy h:mm:ss a"))
+ .isEqualTo("EEE M/d/yy h:mm:ss a zzz");
+ assertThat(DateFormatter.getModifiedLocalizedPattern("M/d/yy h a"))
+ .isEqualTo(DateFormatter.LOCALIZED_FORMAT_STRING);
+ assertThat(DateFormatter.getModifiedLocalizedPattern("EEE M/d/yy h:mm a"))
+ .isEqualTo("EEE M/d/yy h:mm:ss a zzz");
+ }
+}
diff --git
a/geode-core/src/test/java/org/apache/geode/management/internal/json/QueryResultFormatterTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/json/QueryResultFormatterTest.java
index 1defccb..30e7e03 100644
---
a/geode-core/src/test/java/org/apache/geode/management/internal/json/QueryResultFormatterTest.java
+++
b/geode-core/src/test/java/org/apache/geode/management/internal/json/QueryResultFormatterTest.java
@@ -34,6 +34,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.apache.geode.cache.query.data.CollectionHolder;
+import org.apache.geode.internal.logging.DateFormatter;
import org.apache.geode.management.model.Employee;
import org.apache.geode.management.model.Item;
import org.apache.geode.management.model.Order;
@@ -119,16 +120,20 @@ public class QueryResultFormatterTest {
QueryResultFormatter stringResult = new
QueryResultFormatter(100).add(RESULT, "String");
checkResult(stringResult,
"{\"result\":[[\"java.lang.String\",\"String\"]]}");
+ }
- Date date = new Date(0);
- String expectedString =
- new
SimpleDateFormat(QueryResultFormatter.DATE_FORMAT_PATTERN).format(date);
+ @Test
+ public void testDateTimes() throws Exception {
+ long time = System.currentTimeMillis();
+ Date date = new Date(time);
+ SimpleDateFormat format = DateFormatter.createLocalizedDateFormat();
+ String expectedString = format.format(date);
QueryResultFormatter javaDateResult =
new QueryResultFormatter(100).add(RESULT, date);
checkResult(javaDateResult,
"{\"result\":[[\"java.util.Date\",\"" + expectedString + "\"]]}");
- java.sql.Date sqlDate = new java.sql.Date(0);
+ java.sql.Date sqlDate = new java.sql.Date(time);
QueryResultFormatter sqlDateResult =
new QueryResultFormatter(100).add(RESULT, sqlDate);
checkResult(sqlDateResult,