Skip to content

Commit

Permalink
[FIXED JENKINS-22586]
Browse files Browse the repository at this point in the history
Respond to system parameter "org.apache.commons.jelly.tags.fmt.timeZone"

See https://wiki.jenkins-ci.org/display/JENKINS/Change+time+zone
  • Loading branch information
StevenGBrown committed Jun 7, 2014
1 parent 9e8671a commit 033dea2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Expand Up @@ -42,6 +42,7 @@

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;

/**
* Global configuration for the Timestamper plug-in, as shown on the Jenkins
Expand All @@ -58,8 +59,12 @@ public final class TimestamperConfig extends GlobalConfiguration {
public TimestampFormatter apply(@Nonnull StaplerRequest request) {
TimestamperConfig config = GlobalConfiguration.all().get(
TimestamperConfig.class);
// This System property is used to configure the time zone.
// See the "Change time zone" Jenkins wiki page.
Optional<String> timeZoneId = Optional.fromNullable(System
.getProperty("org.apache.commons.jelly.tags.fmt.timeZone"));
return new TimestampFormatterImpl(config.getSystemTimeFormat(),
config.getElapsedTimeFormat(), request);
config.getElapsedTimeFormat(), timeZoneId, request);
}
};

Expand Down
Expand Up @@ -29,6 +29,7 @@

import java.io.Serializable;
import java.util.Date;
import java.util.TimeZone;

import javax.annotation.concurrent.Immutable;
import javax.servlet.http.Cookie;
Expand All @@ -38,6 +39,7 @@
import org.apache.commons.lang.time.FastDateFormat;

import com.google.common.base.Function;
import com.google.common.base.Optional;

/**
* Formats a time-stamp to be displayed in the console log page, according to
Expand All @@ -63,11 +65,14 @@ public final class TimestampFormatterImpl implements TimestampFormatter {
* the system clock time format
* @param elapsedTimeFormat
* the elapsed time format
* @param timeZoneId
* the configured time zone identifier
* @param request
* the current HTTP request
*/
public TimestampFormatterImpl(String systemTimeFormat,
String elapsedTimeFormat, HttpServletRequest request) {
String elapsedTimeFormat, Optional<String> timeZoneId,
HttpServletRequest request) {

String cookieValue = null;
Cookie[] cookies = request.getCookies();
Expand All @@ -86,7 +91,13 @@ public TimestampFormatterImpl(String systemTimeFormat,
formatTimestamp = new EmptyFormatFunction();
} else {
// "system", no cookie, or unrecognised cookie
formatTimestamp = new SystemTimeFormatFunction(systemTimeFormat);
TimeZone timeZone = null;
if (timeZoneId.isPresent()) {
timeZone = TimeZone.getTimeZone(timeZoneId.get());
}
FastDateFormat format = FastDateFormat.getInstance(systemTimeFormat,
timeZone);
formatTimestamp = new SystemTimeFormatFunction(format);
}
}

Expand All @@ -112,16 +123,15 @@ private static class SystemTimeFormatFunction implements

private static final long serialVersionUID = 1L;

private final String systemTimeFormat;
private final FastDateFormat format;

SystemTimeFormatFunction(String systemTimeFormat) {
this.systemTimeFormat = checkNotNull(systemTimeFormat);
SystemTimeFormatFunction(FastDateFormat format) {
this.format = checkNotNull(format);
}

@Override
public String apply(Timestamp timestamp) {
return FastDateFormat.getInstance(systemTimeFormat).format(
new Date(timestamp.millisSinceEpoch));
return format.format(new Date(timestamp.millisSinceEpoch));
}
}

Expand Down
Expand Up @@ -49,6 +49,8 @@
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.google.common.base.Optional;

/**
* Unit test for the {@link TimestampFormatterImpl} class.
*
Expand Down Expand Up @@ -157,6 +159,23 @@ public void testMarkupElapsedTimeWithDifferentTimeZoneAfterSerialization() {
+ "line"));
}

/**
*/
@Test
public void testMarkupElapsedTimeWithConfiguredTimeZone() {
assertThat(markup("line", "GMT+8").toString(true),
is(prefixInDifferentTimezone + "line"));
}

/**
*/
@Test
public void testMarkupElapsedTimeWithConfiguredTimeZoneAfterSerialization() {
serialize = true;
assertThat(markup("line", "GMT+8").toString(true),
is(prefixInDifferentTimezone + "line"));
}

/**
*/
@Test
Expand Down Expand Up @@ -192,12 +211,20 @@ public void testAntTargetNoteThenMarkupAfterSerialization() {
}

private MarkupText markup(String text) {
return markup(new MarkupText(text));
return markup(new MarkupText(text), Optional.<String> absent());
}

private MarkupText markup(String text, String timeZoneId) {
return markup(new MarkupText(text), Optional.of(timeZoneId));
}

private MarkupText markup(MarkupText markupText) {
return markup(markupText, Optional.<String> absent());
}

private MarkupText markup(MarkupText markupText, Optional<String> timeZoneId) {
TimestampFormatter formatter = new TimestampFormatterImpl("HH:mm:ss ",
"ss.S ", request);
"ss.S ", timeZoneId, request);
if (serialize) {
formatter = (TimestampFormatter) SerializationUtils.clone(formatter);
}
Expand Down

0 comments on commit 033dea2

Please sign in to comment.