Author: lukaszlenart
Date: Tue Mar 16 11:24:41 2010
New Revision: 923696

URL: http://svn.apache.org/viewvc?rev=923696&view=rev
Log:
Resolved WW-2326 - add timezone attribute to Date tag

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Date.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java
    struts/struts2/trunk/core/src/site/resources/tags/date.html
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Date.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Date.java?rev=923696&r1=923695&r2=923696&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Date.java 
(original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Date.java 
Tue Mar 16 11:24:41 2010
@@ -21,6 +21,14 @@
 
 package org.apache.struts2.components;
 
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.TextProvider;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.views.annotations.StrutsTag;
+import org.apache.struts2.views.annotations.StrutsTagAttribute;
+
 import java.io.IOException;
 import java.io.Writer;
 import java.text.DateFormat;
@@ -29,15 +37,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
-
-import org.apache.struts2.views.annotations.StrutsTag;
-import org.apache.struts2.views.annotations.StrutsTagAttribute;
-
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.TextProvider;
-import com.opensymphony.xwork2.util.ValueStack;
-import com.opensymphony.xwork2.util.logging.Logger;
-import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import java.util.TimeZone;
 
 /**
  * <!-- START SNIPPET: javadoc -->
@@ -193,6 +193,8 @@ public class Date extends ContextBean {
 
     private boolean nice;
 
+    private String timezone;
+
     public Date(ValueStack stack) {
         super(stack);
     }
@@ -300,6 +302,11 @@ public class Date extends ContextBean {
                 if (nice) {
                     msg = formatTime(tp, date);
                 } else {
+                    TimeZone tz = TimeZone.getDefault();
+                    if (timezone != null) {
+                        tz = TimeZone.getTimeZone(timezone);
+                    }
+
                     if (format == null) {
                         String globalFormat = null;
 
@@ -312,18 +319,22 @@ public class Date extends ContextBean {
                         // DATETAG_PROPERTY
                         if (globalFormat != null
                                 && !DATETAG_PROPERTY.equals(globalFormat)) {
-                            msg = new SimpleDateFormat(globalFormat,
-                                    ActionContext.getContext().getLocale())
-                                    .format(date);
+                            SimpleDateFormat sdf = new 
SimpleDateFormat(globalFormat,
+                                    ActionContext.getContext().getLocale());
+                            sdf.setTimeZone(tz);
+                            msg = sdf.format(date);
                         } else {
-                            msg = DateFormat.getDateTimeInstance(
+                            DateFormat df = DateFormat.getDateTimeInstance(
                                     DateFormat.MEDIUM, DateFormat.MEDIUM,
-                                    ActionContext.getContext().getLocale())
-                                    .format(date);
+                                    ActionContext.getContext().getLocale());
+                            df.setTimeZone(tz);
+                            msg = df.format(date);
                         }
                     } else {
-                        msg = new SimpleDateFormat(format, ActionContext
-                                .getContext().getLocale()).format(date);
+                        SimpleDateFormat sdf = new SimpleDateFormat(format, 
ActionContext
+                                .getContext().getLocale());
+                        sdf.setTimeZone(tz);
+                        msg = sdf.format(date);
                     }
                 }
                 if (msg != null) {
@@ -377,4 +388,17 @@ public class Date extends ContextBean {
     public boolean isNice() {
         return nice;
     }
+
+    /**
+     * @return Returns the name.
+     */
+    public String getTimezone() {
+        return timezone;
+    }
+
+    @StrutsTagAttribute(description = "The specific timezone in which to 
format the date", required = false)
+    public void setTimezone(String timezone) {
+        this.timezone = timezone;
+    }
+
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java?rev=923696&r1=923695&r2=923696&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/DateTag.java
 Tue Mar 16 11:24:41 2010
@@ -21,13 +21,12 @@
 
 package org.apache.struts2.views.jsp;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.opensymphony.xwork2.util.ValueStack;
 import org.apache.struts2.components.Component;
 import org.apache.struts2.components.Date;
 
-import com.opensymphony.xwork2.util.ValueStack;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * @see Date
@@ -39,6 +38,7 @@ public class DateTag extends ContextBean
     protected String name;
     protected String format;
     protected boolean nice;
+    protected String timezone;
 
     public Component getBean(ValueStack stack, HttpServletRequest req, 
HttpServletResponse res) {
         return new Date(stack);
@@ -50,7 +50,7 @@ public class DateTag extends ContextBean
         d.setName(name);
         d.setFormat(format);
         d.setNice(nice);
-
+        d.setTimezone(timezone);
     }
 
     public void setFormat(String format) {
@@ -64,4 +64,9 @@ public class DateTag extends ContextBean
     public void setName(String name) {
         this.name = name;
     }
+
+    public void setTimezone(String timezone) {
+        this.timezone = timezone;
+    }
+
 }

Modified: struts/struts2/trunk/core/src/site/resources/tags/date.html
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/site/resources/tags/date.html?rev=923696&r1=923695&r2=923696&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/site/resources/tags/date.html (original)
+++ struts/struts2/trunk/core/src/site/resources/tags/date.html Tue Mar 16 
11:24:41 2010
@@ -66,6 +66,14 @@ Please do not edit it directly.
                                        <td align="left" valign="top">Whether 
to print out the date nicely</td>
                                </tr>
                                <tr>
+                                       <td align="left" 
valign="top">timezone</td>
+                                       <td align="left" valign="top">false</td>
+                                       <td align="left" valign="top"></td>
+                                       <td align="left" valign="top">false</td>
+                                       <td align="left" 
valign="top">String</td>
+                                       <td align="left" valign="top">The 
specific timezone in which to format the date</td>
+                               </tr>
+                               <tr>
                                        <td align="left" valign="top">var</td>
                                        <td align="left" valign="top">false</td>
                                        <td align="left" valign="top"></td>

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java?rev=923696&r1=923695&r2=923696&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
 Tue Mar 16 11:24:41 2010
@@ -21,15 +21,15 @@
 
 package org.apache.struts2.views.jsp.ui;
 
+import com.opensymphony.xwork2.ActionContext;
+import org.apache.struts2.views.jsp.AbstractTagTest;
+import org.apache.struts2.views.jsp.DateTag;
+
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
-
-import org.apache.struts2.views.jsp.AbstractTagTest;
-import org.apache.struts2.views.jsp.DateTag;
-
-import com.opensymphony.xwork2.ActionContext;
+import java.util.TimeZone;
 
 /**
  * Unit test for {...@link org.apache.struts2.components.Date}.
@@ -52,7 +52,24 @@ public class DateTagTest extends Abstrac
         tag.doEndTag();
         assertEquals(formatted, writer.toString());
     }
-    
+
+    public void testCustomFormatWithTimezone() throws Exception {
+        String format = "yyyy/MM/dd hh:mm:ss";
+        Date now = 
Calendar.getInstance(TimeZone.getTimeZone("UTC+1")).getTime();
+        SimpleDateFormat sdf = new SimpleDateFormat(format);
+        sdf.setTimeZone(TimeZone.getTimeZone("UTC+1"));
+        String formatted = sdf.format(now);
+        context.put("myDate", now);
+
+        tag.setName("myDate");
+        tag.setNice(false);
+        tag.setFormat(format);
+        tag.setTimezone("UTC+1");
+        tag.doStartTag();
+        tag.doEndTag();
+        assertEquals(formatted, writer.toString());
+    }
+
     public void testCustomFormatCalendar() throws Exception {
         String format = "yyyy/MM/dd hh:mm:ss";
         Calendar calendar = Calendar.getInstance();


Reply via email to