-
Notifications
You must be signed in to change notification settings - Fork 417
Throw IllegalArgumentException at invalid time zone textual representation instead of defaulting to GMT #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e9d4d08
851016d
dd9ab49
e5e0ba5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.util.TimeZone; | ||
|
|
||
| import net.logstash.logback.fieldnames.LogstashCommonFieldNames; | ||
| import net.logstash.logback.util.TimeZoneUtils; | ||
|
|
||
| import ch.qos.logback.core.spi.DeferredProcessingAware; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
|
|
@@ -31,8 +32,8 @@ | |
| * Writes the timestamp field as either: | ||
| * <ul> | ||
| * <li>A string value formatted by a {@link DateTimeFormatter} pattern</li> | ||
| * <li>A string value representing the number of milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_STRING})</li> | ||
| * <li>A number value of the milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_NUMBER})</li> | ||
| * <li>A string value representing the number of milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_STRING})</li> | ||
| * <li>A number value of the milliseconds since unix epoch (designated by specifying the pattern value as {@value #UNIX_TIMESTAMP_AS_NUMBER})</li> | ||
| * </ul> | ||
| */ | ||
| public abstract class FormattedTimestampJsonProvider<Event extends DeferredProcessingAware, FieldNames extends LogstashCommonFieldNames> extends AbstractFieldJsonProvider<Event> implements FieldNamesAware<FieldNames> { | ||
|
|
@@ -52,7 +53,6 @@ public abstract class FormattedTimestampJsonProvider<Event extends DeferredProce | |
| public static final String UNIX_TIMESTAMP_AS_STRING = "[UNIX_TIMESTAMP_AS_STRING]"; | ||
|
|
||
| private static final String DEFAULT_PATTERN = "[ISO_OFFSET_DATE_TIME]"; | ||
| private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getDefault(); | ||
|
|
||
| /** | ||
| * The pattern in which to format the timestamp. | ||
|
|
@@ -72,7 +72,7 @@ public abstract class FormattedTimestampJsonProvider<Event extends DeferredProce | |
| * The timezone for which to write the timestamp. | ||
| * Only applicable if the pattern is not {@value #UNIX_TIMESTAMP_AS_NUMBER} or {@value #UNIX_TIMESTAMP_AS_STRING} | ||
| */ | ||
| private TimeZone timeZone = DEFAULT_TIMEZONE; | ||
| private TimeZone timeZone = TimeZone.getDefault(); | ||
|
|
||
| /** | ||
| * Writes the timestamp to the JsonGenerator. | ||
|
|
@@ -204,11 +204,60 @@ public void setPattern(String pattern) { | |
| this.pattern = pattern; | ||
| updateTimestampWriter(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the time zone used to write the timestamp. | ||
| * | ||
| * @return the time zone used to write the timestamp | ||
| */ | ||
| public String getTimeZone() { | ||
| return timeZone.getID(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Set the timezone for which to write the timestamp. | ||
| * Only applicable if the pattern is not {@value #UNIX_TIMESTAMP_AS_NUMBER} or {@value #UNIX_TIMESTAMP_AS_STRING}. | ||
| * | ||
| * <p>The TimeZone can be expressed into any format supported by {@link TimeZone#getTimeZone(String)}. | ||
| * It can be a valid time zone ID. For instance, the time zone ID for the | ||
| * U.S. Pacific Time zone is "America/Los_Angeles". | ||
| * | ||
| * <p>If the time zone you want is not represented by one of the | ||
| * supported IDs, then a custom time zone ID can be specified to | ||
| * produce a TimeZone. The syntax of a custom time zone ID is: | ||
| * | ||
| * <blockquote><pre> | ||
| * <i>CustomID:</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> | ||
| * <i>Sign:</i> one of | ||
| * <code>+ -</code> | ||
| * <i>Hours:</i> | ||
| * <i>Digit</i> | ||
| * <i>Digit</i> <i>Digit</i> | ||
| * <i>Minutes:</i> | ||
| * <i>Digit</i> <i>Digit</i> | ||
| * <i>Digit:</i> one of | ||
| * <code>0 1 2 3 4 5 6 7 8 9</code> | ||
| * </pre></blockquote> | ||
| * | ||
| * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be | ||
| * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten | ||
| * hours and ten minutes ahead of GMT, respectively. | ||
| * | ||
| * <p>Use a blank string or {@code null} to use the default TimeZone of the system. | ||
|
||
| * | ||
| * @param timeZoneId the textual representation of the desired time zone | ||
| * @throws IllegalArgumentException if the input string is not a valid TimeZone representation | ||
| */ | ||
| public void setTimeZone(String timeZoneId) { | ||
| this.timeZone = TimeZone.getTimeZone(timeZoneId); | ||
| if (timeZoneId == null || timeZoneId.trim().isEmpty()) { | ||
| this.timeZone = TimeZone.getDefault(); | ||
| } else { | ||
| this.timeZone = TimeZoneUtils.parseTimeZone(timeZoneId); | ||
| } | ||
| updateTimestampWriter(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright 2013-2021 the original author or authors. | ||
| * | ||
| * Licensed 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 net.logstash.logback.util; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.TimeZone; | ||
|
|
||
| public class TimeZoneUtils { | ||
|
|
||
| private TimeZoneUtils() { | ||
| // utility class | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Parse a string into the corresponding {@link TimeZone} using the format described by | ||
| * {@link TimeZone#getTimeZone(String)}. | ||
| * | ||
| * <p>The string can be a valid time zone ID. For instance, the time zone ID for the | ||
| * U.S. Pacific Time zone is "America/Los_Angeles". | ||
| * | ||
| * <p>If the time zone you want is not represented by one of the | ||
| * supported IDs, then a custom time zone ID can be specified to | ||
| * produce a TimeZone. The syntax of a custom time zone ID is: | ||
| * | ||
| * <blockquote><pre> | ||
| * <i>CustomID:</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> | ||
| * <code>GMT</code> <i>Sign</i> <i>Hours</i> | ||
| * <i>Sign:</i> one of | ||
| * <code>+ -</code> | ||
| * <i>Hours:</i> | ||
| * <i>Digit</i> | ||
| * <i>Digit</i> <i>Digit</i> | ||
| * <i>Minutes:</i> | ||
| * <i>Digit</i> <i>Digit</i> | ||
| * <i>Digit:</i> one of | ||
| * <code>0 1 2 3 4 5 6 7 8 9</code> | ||
| * </pre></blockquote> | ||
| * | ||
| * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be | ||
| * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten | ||
| * hours and ten minutes ahead of GMT, respectively. | ||
| * | ||
brenuart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param str the string to parse into a valid {@link TimeZone}. | ||
| * @return the {@link TimeZone} corresponding to the input string | ||
| * @throws IllegalArgumentException thrown when the string is not a valid TimeZone textual | ||
| * representation. | ||
| */ | ||
| public static TimeZone parseTimeZone(String str) { | ||
| TimeZone tz = TimeZone.getTimeZone(Objects.requireNonNull(str)); | ||
|
|
||
| /* | ||
| * Instead of throwing an exception when it fails to parse the string into a valid | ||
| * TimeZone, getTimeZone() returns a TimeZone with id "GMT". | ||
| * | ||
| * If the returned TimeZone is GMT but the input string is not, then the input string | ||
| * was not a valid time zone representation. | ||
| */ | ||
| if ("GMT".equals(tz.getID()) && !"GMT".equals(str)) { | ||
| throw new IllegalArgumentException("Invalid TimeZone value (was '" + str + "')"); | ||
| } | ||
|
|
||
| return tz; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2013-2021 the original author or authors. | ||
| * | ||
| * Licensed 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 net.logstash.logback.util; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * @author brenuart | ||
| * | ||
| */ | ||
| public class TimeZoneUtilsTest { | ||
|
|
||
| @Test | ||
| public void standardTimeZones() { | ||
| assertThat(TimeZoneUtils.parseTimeZone("GMT").getID()).isEqualTo("GMT"); | ||
| assertThat(TimeZoneUtils.parseTimeZone("Europe/Brussels").getID()).isEqualTo("Europe/Brussels"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void customTimeZones() { | ||
| assertThat(TimeZoneUtils.parseTimeZone("GMT+00").getID()).isEqualTo("GMT+00:00"); | ||
| assertThat(TimeZoneUtils.parseTimeZone("GMT+00:00").getID()).isEqualTo("GMT+00:00"); | ||
|
|
||
| assertThat(TimeZoneUtils.parseTimeZone("GMT-00").getID()).isEqualTo("GMT-00:00"); | ||
| assertThat(TimeZoneUtils.parseTimeZone("GMT-00:00").getID()).isEqualTo("GMT-00:00"); | ||
|
|
||
| assertThat(TimeZoneUtils.parseTimeZone("GMT+01:12").getID()).isEqualTo("GMT+01:12"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void invalidTimeZone() { | ||
| assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone("foo")).isInstanceOf(IllegalArgumentException.class); | ||
| assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone("")).isInstanceOf(IllegalArgumentException.class); | ||
| assertThatThrownBy(() -> TimeZoneUtils.parseTimeZone(null)).isInstanceOf(NullPointerException.class); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.