LANG-818 FastDateFormat's "z" pattern does not respect timezone of Calendar instances passed to format()

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390980 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-27 12:56:19 +00:00
parent cfab1e4fa1
commit 04babff379
3 changed files with 39 additions and 6 deletions

View File

@ -28,6 +28,7 @@
<action issue="LANG-828" type="fix">FastDateParser does not handle non-Gregorian calendars properly</action>
<action issue="LANG-826" type="fix">FastDateParser does not handle non-ASCII digits correctly</action>
<action issue="LANG-825" type="add">Create StrBuilder APIs similar to String.format(String, Object...)</action>
<action issue="LANG-818" type="fix">FastDateFormat's "z" pattern does not respect timezone of Calendar instances passed to format()</action>
<action issue="LANG-817" type="fix">Add org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8</action>
<action issue="LANG-813" type="fix">StringUtils.equalsIgnoreCase doesn't check string reference equality</action>
<action issue="LANG-810" type="fix">StringUtils.join() endIndex, bugged for loop</action>

View File

@ -1093,7 +1093,8 @@ public class FastDatePrinter implements DatePrinter, Serializable {
* <p>Inner class to output a time zone name.</p>
*/
private static class TimeZoneNameRule implements Rule {
private final TimeZone mTimeZone;
private final Locale mLocale;
private final int mStyle;
private final String mStandard;
private final String mDaylight;
@ -1105,8 +1106,9 @@ public class FastDatePrinter implements DatePrinter, Serializable {
* @param style the style
*/
TimeZoneNameRule(TimeZone timeZone, Locale locale, int style) {
mTimeZone = timeZone;
mLocale = locale;
mStyle = style;
mStandard = getTimeZoneDisplay(timeZone, false, style, locale);
mDaylight = getTimeZoneDisplay(timeZone, true, style, locale);
}
@ -1116,6 +1118,9 @@ public class FastDatePrinter implements DatePrinter, Serializable {
*/
@Override
public int estimateLength() {
// We have no access to the Calendar object that will be passed to
// appendTo so base estimate on the TimeZone passed to the
// constructor
return Math.max(mStandard.length(), mDaylight.length());
}
@ -1124,10 +1129,12 @@ public class FastDatePrinter implements DatePrinter, Serializable {
*/
@Override
public void appendTo(StringBuffer buffer, Calendar calendar) {
if (mTimeZone.useDaylightTime() && calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(mDaylight);
TimeZone zone = calendar.getTimeZone();
if (zone.useDaylightTime()
&& calendar.get(Calendar.DST_OFFSET) != 0) {
buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale));
} else {
buffer.append(mStandard);
buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
}
}
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -260,4 +261,28 @@ public class FastDatePrinterTest {
DatePrinter printer= getInstance(YYYY_MM_DD, NEW_YORK);
assertEquals(NEW_YORK, printer.getTimeZone());
}
@Test
public void testCalendarTimezoneRespected() {
String[] availableZones = TimeZone.getAvailableIDs();
TimeZone currentZone = TimeZone.getDefault();
TimeZone anotherZone = null;
for (String zone : availableZones) {
if (!zone.equals(currentZone.getID())) {
anotherZone = TimeZone.getTimeZone(zone);
}
}
assertNotNull("Cannot find another timezone", anotherZone);
final String pattern = "h:mma z";
final Calendar cal = Calendar.getInstance(anotherZone);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setTimeZone(anotherZone);
String expectedValue = sdf.format(cal.getTime());
String actualValue = FastDateFormat.getInstance(pattern).format(cal);
assertEquals(expectedValue, actualValue);
}
}