Fixing HtmlLayoutTest when timezone has minutes
@@ -39,6 +39,7 @@ import org.apache.logging.log4j.core.test.BasicConfigurationFactory;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.time.Instant;
import org.apache.logging.log4j.core.time.MutableInstant;
+import org.apache.logging.log4j.core.util.datetime.FixedDateFormat;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.junit.UsingAnyThreadContext;
@@ -238,7 +239,7 @@ public class HtmlLayoutTest {
@Test
public void testLayoutWithDatePatternFixedFormat() {
- for (final String timeZone : new String[] {"GMT+8", "UTC", null}) {
+ for (final String timeZone : new String[] {"GMT+8", "GMT+0530" ,"UTC", null}) {
for (final FixedFormat format : FixedFormat.values()) {
testLayoutWithDatePatternFixedFormat(format, timeZone);
}
@@ -278,6 +279,15 @@ public class HtmlLayoutTest {
DateTimeFormatter.ofPattern(format.getPattern().replace('n', 'S').replace('X', 'x'), locale);
String expected = zonedDateTime.format(dateTimeFormatter);
+ String offset = zonedDateTime.getOffset().toString();
+
+ //Truncate minutes if timeZone format is HH and timeZone has minutes. This is required because according to DateTimeFormatter,
+ //One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'
+ //ref : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
+ if(FixedDateFormat.FixedTimeZoneFormat.HH.equals(format.getFixedTimeZoneFormat()) && offset.contains(":") && !"00".equals(offset.split(":")[1])) {
+ expected = expected.substring(0, expected.length() - 2);
+ }
+
assertEquals("<td>" + expected + "</td>", actual,
MessageFormat.format("Incorrect date={0}, format={1}, timezone={2}", actual, format.name(), timezone));
}
