[
https://issues.apache.org/jira/browse/LOG4J2-1246?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16696119#comment-16696119
]
Gary Gregory commented on LOG4J2-1246:
--------------------------------------
In
{{org.apache.logging.log4j.core.util.datetime.FixedDateFormat.formatInstant(Instant,
char[], int)}}, there is no allowance for writing out a time zone offset like
"-0700". Am I missing something?
{noformat}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
index f58a29b..c8e4ff3 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
@@ -38,8 +38,10 @@
* Enumeration over the supported date/time format patterns.
* <p>
* Package protected for unit tests.
+ * </p>
*/
public enum FixedFormat {
+
/**
* ABSOLUTE time format: {@code "HH:mm:ss,SSS"}.
*/
@@ -107,6 +109,11 @@
ISO8601("yyyy-MM-dd'T'HH:mm:ss,SSS", "yyyy-MM-dd'T'", 2, ':', 1, ',', 1, 3),
/**
+ * ISO8601_Z time format: {@code "yyyy-MM-dd'T'HH:mm:ss,SSSZ"}.
+ */
+ ISO8601_Z("yyyy-MM-dd'T'HH:mm:ss,SSSZ", "yyyy-MM-dd'T'", 2, ':', 1, ',', 1,
3),
+
+ /**
* ISO8601 time format: {@code "yyyy-MM-dd'T'HH:mm:ss.SSS"}.
*/
ISO8601_PERIOD("yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'", 2, ':', 1, '.', 1,
3);
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/DatePatternConverterTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/DatePatternConverterTest.java
index affb5b2..aaef824 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/DatePatternConverterTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/DatePatternConverterTest.java
@@ -50,9 +50,14 @@
*/
private static final String ISO8601_FORMAT =
FixedDateFormat.FixedFormat.ISO8601.name();
+ /**
+ * ISO8601_Z string literal.
+ */
+ private static final String ISO8601_Z_FORMAT =
FixedDateFormat.FixedFormat.ISO8601_Z.name();
+
private static final String[] ISO8601_FORMAT_OPTIONS = {ISO8601_FORMAT};
- @Parameterized.Parameters
+ @Parameterized.Parameters(name = "threadLocalEnabled={0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{{Boolean.TRUE}, {Boolean.FALSE}});
}
@@ -133,6 +138,36 @@
}
@Test
+ public void testFormatLogEventStringBuilderIso8601TimezoneZ() {
+ final LogEvent event = new MyLogEvent();
+ final String[] optionsWithTimezone = {ISO8601_FORMAT, "Z"};
+ final DatePatternConverter converter =
DatePatternConverter.newInstance(optionsWithTimezone);
+ final StringBuilder sb = new StringBuilder();
+ converter.format(event, sb);
+
+ final TimeZone tz = TimeZone.getTimeZone("UTC");
+ final SimpleDateFormat sdf = new SimpleDateFormat(converter.getPattern());
+ sdf.setTimeZone(tz);
+ final long adjusted = event.getTimeMillis() + tz.getDSTSavings();
+ final String expected = sdf.format(new Date(adjusted));
+ // final String expected = "2011-12-30T17:56:35,987"; // in UTC
+ assertEquals(expected, sb.toString());
+ }
+
+ @Test
+ public void testFormatLogEventStringBuilderIso8601Timezone() {
+ final LogEvent event = new MyLogEvent();
+ final String[] optionsWithTimezone = {ISO8601_Z_FORMAT};
+ final DatePatternConverter converter =
DatePatternConverter.newInstance(optionsWithTimezone);
+ final StringBuilder sb = new StringBuilder();
+ converter.format(event, sb);
+
+ final SimpleDateFormat sdf = new SimpleDateFormat(converter.getPattern());
+ final String expected = sdf.format(new Date(event.getTimeMillis()));
+ assertEquals(expected, sb.toString());
+ }
+
+ @Test
public void testPredefinedFormatWithTimezone() {
for (final FixedDateFormat.FixedFormat format :
FixedDateFormat.FixedFormat.values()) {
final String[] options = {format.name(), "PDT"}; // Pacific Daylight
Time=UTC-8:00
{noformat}
> PatternLayout %date conversion pattern should render time zone designator for
> ISO-ISO8601
> -----------------------------------------------------------------------------------------
>
> Key: LOG4J2-1246
> URL: https://issues.apache.org/jira/browse/LOG4J2-1246
> Project: Log4j 2
> Issue Type: Bug
> Components: Pattern Converters
> Affects Versions: 2.4.1, 2.5
> Reporter: Daniel Marcotte
> Priority: Major
>
> Hi there!
> There might be a small issue with the Pattern Converters regarding Timezone
> management:
> http://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
> Right now, these patterns:
> * {{"%d\{ISO8601_BASIC\}}}
> * {{%d\{ISO8601_BASIC\}\{UTC\}}}
> Give the same results:
> * {{20160104T204256,048}}
> * {{20160104T204256,048}}
> However, with a deeper look at the ISO-8601 standards, the second one with
> {{"\{UTC\}"}} should properly add the Timezone at the end of the timestamp.
> Reference: https://en.wikipedia.org/wiki/ISO_8601#Times
> {quote}
> *UTC*
> If the time is in UTC, add a Z directly after the time without a space. Z is
> the zone designator for the zero UTC offset. "09:30 UTC" is therefore
> represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or
> "144515Z".
> UTC time is also known as 'Zulu' time, since 'Zulu' is the NATO phonetic
> alphabet word for 'Z'.
> *Examples*
> <time>Z
> <time>±hh:mm
> <time>±hhmm
> <time>±hh
> {quote}
> As of ISO-8601, the results should be one of the following (configurable?):
> * {{20160104T204256,048Z}}
> * {{20160104T204256,048+00:00}}
> * {{20160104T204256,048+0000}}
> * {{20160104T204256,048+00}}
> Another reference:
> http://stackoverflow.com/questions/833102/wheres-the-datetime-z-format-specifier
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)