Skip to content

Commit

Permalink
GEODE-8447: add seconds to the localized pattern (#5891)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0e15125)
  • Loading branch information
jinmeiliao committed Jan 12, 2021
1 parent 0a6be20 commit 9987851
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

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;
Expand Down Expand Up @@ -87,8 +87,7 @@ public void setup() throws Exception {
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
Expand All @@ -31,6 +32,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}.
*
Expand All @@ -42,6 +48,43 @@ public static DateFormat createDateFormat() {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
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
*/
Expand Down Expand Up @@ -80,7 +79,7 @@ void postCreateMapper() {

// 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()));

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -119,16 +120,20 @@ public void testPrimitives() throws Exception {

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,
Expand Down

0 comments on commit 9987851

Please sign in to comment.