You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2012/09/27 14:56:20 UTC

svn commit: r1390980 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/time/FastDatePrinter.java test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java

Author: sebb
Date: Thu Sep 27 12:56:19 2012
New Revision: 1390980

URL: http://svn.apache.org/viewvc?rev=1390980&view=rev
Log:
LANG-818 FastDateFormat's "z" pattern does not respect timezone of Calendar instances passed to format()

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1390980&r1=1390979&r2=1390980&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Thu Sep 27 12:56:19 2012
@@ -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>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java?rev=1390980&r1=1390979&r2=1390980&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java Thu Sep 27 12:56:19 2012
@@ -1093,7 +1093,8 @@ public class FastDatePrinter implements 
      * <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 
          * @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 
          */
         @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 
          */
         @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));
             }
         }
     }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java?rev=1390980&r1=1390979&r2=1390980&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java Thu Sep 27 12:56:19 2012
@@ -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);
+    }
 }