Author: cbrisson
Date: Sun Nov 20 10:19:41 2016
New Revision: 1770547

URL: http://svn.apache.org/viewvc?rev=1770547&view=rev
Log:
[tools] introduce 'iso' (date/time with time zone, locale independent) and 
'intl' (date/time without time zone, locale and time zone independent) formats

Modified:
    
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
    
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/DateTool.java

Modified: 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java?rev=1770547&r1=1770546&r2=1770547&view=diff
==============================================================================
--- 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
 (original)
+++ 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ConversionUtils.java
 Sun Nov 20 10:19:41 2016
@@ -30,7 +30,9 @@ import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Calendar;
+import java.util.HashMap;
 import java.util.Locale;
+import java.util.Map;
 import java.util.TimeZone;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -52,6 +54,18 @@ public class ConversionUtils
     //NOTE: '3' belongs to a non-public "scientific" style
     private static final int STYLE_INTEGER      = 4;
 
+    /* Java DateFormat standard constants extensions */
+    private static final int STYLE_INTL = 5; /* international format without 
time zone*/
+    private static final int STYLE_ISO = 6;  /* international format with 
timezone (RFC 3339) */
+
+
+    /* iso/intl date/time formats (locale-independant) */
+    private static DateFormat isoDateFormat = new 
SimpleDateFormat("yyyy-MM-dd");  /* date for iso/intl */
+    private static DateFormat intlTimeFormat = new 
SimpleDateFormat("HH:mm:ss");   /* time without timezone */
+    private static DateFormat isoTimeFormat = new 
SimpleDateFormat("HH:mm:ssXXX"); /* time with ISO 8601 timezone */
+    private static DateFormat intlTimestampFormat = new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   /* timestamp without timezone */
+    private static DateFormat isoTimestampFormat = new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ssXXX"); /* timestamp with ISO 88601 
timezone */
+
     // cache custom formats
     private static ConcurrentMap<String,NumberFormat> customFormatsCache = new 
ConcurrentHashMap<String,NumberFormat>();
 
@@ -401,19 +415,53 @@ public class ConversionUtils
             else if (timeStyle < 0)
             {
                 // only a date style was specified
-                df = DateFormat.getDateInstance(dateStyle, locale);
+                switch (dateStyle)
+                {
+                    case STYLE_INTL:
+                    case STYLE_ISO:
+                        df = isoDateFormat;
+                        break;
+                    default:
+                        df = DateFormat.getDateInstance(dateStyle, locale);
+                        df.setTimeZone(timezone);
+                        break;
+                }
             }
             else if (dateStyle < 0)
             {
                 // only a time style was specified
-                df = DateFormat.getTimeInstance(timeStyle, locale);
+                switch (timeStyle)
+                {
+                    case STYLE_INTL:
+                        df = intlTimeFormat;
+                        break;
+                    case STYLE_ISO:
+                        df = (DateFormat) isoTimeFormat.clone();
+                        df.setTimeZone(timezone);
+                        break;
+                    default:
+                        df = DateFormat.getTimeInstance(timeStyle, locale);
+                        df.setTimeZone(timezone);
+                        break;
+                }
             }
             else
             {
-                df = DateFormat.getDateTimeInstance(dateStyle, timeStyle,
-                                                    locale);
+                switch (dateStyle)
+                {
+                    case STYLE_INTL:
+                        df = intlTimestampFormat;
+                        break;
+                    case STYLE_ISO:
+                        df = (DateFormat) isoTimestampFormat.clone();
+                        df.setTimeZone(timezone);
+                        break;
+                    default:
+                        df = DateFormat.getDateTimeInstance(dateStyle, 
timeStyle, locale);
+                        df.setTimeZone(timezone);
+                        break;
+                }
             }
-            df.setTimeZone(timezone);
             return df;
         }
         catch (Exception suppressed)
@@ -423,6 +471,19 @@ public class ConversionUtils
         }
     }
 
+    static Map<String, Integer> stylesMap;
+    static
+    {
+        stylesMap = new HashMap<String, Integer>();
+        stylesMap.put("FULL", DateFormat.FULL);
+        stylesMap.put("LONG", DateFormat.LONG);
+        stylesMap.put("MEDIUM", DateFormat.MEDIUM);
+        stylesMap.put("SHORT", DateFormat.SHORT);
+        stylesMap.put("DEFAULT", DateFormat.DEFAULT);
+        stylesMap.put("INTL", STYLE_INTL);
+        stylesMap.put("ISO", STYLE_ISO);
+    }
+
     /**
      * Checks a string to see if it matches one of the standard DateFormat
      * style patterns: full, long, medium, short, or default.  If it does,
@@ -435,32 +496,8 @@ public class ConversionUtils
      */
     public static int getDateStyleAsInt(String style)
     {
-        // avoid needlessly running through all the string comparisons
-        if (style == null || style.length() < 4 || style.length() > 7) {
-            return -1;
-        }
-        if (style.equalsIgnoreCase("full"))
-        {
-            return DateFormat.FULL;
-        }
-        if (style.equalsIgnoreCase("long"))
-        {
-            return DateFormat.LONG;
-        }
-        if (style.equalsIgnoreCase("medium"))
-        {
-            return DateFormat.MEDIUM;
-        }
-        if (style.equalsIgnoreCase("short"))
-        {
-            return DateFormat.SHORT;
-        }
-        if (style.equalsIgnoreCase("default"))
-        {
-            return DateFormat.DEFAULT;
-        }
-        // ok, it's not any of the standard patterns
-        return -1;
+        Integer intStyle = stylesMap.get(style.toUpperCase());
+        return intStyle == null ? -1 : intStyle.intValue();
     }
 
 

Modified: 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/DateTool.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/DateTool.java?rev=1770547&r1=1770546&r2=1770547&view=diff
==============================================================================
--- 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/DateTool.java
 (original)
+++ 
velocity/tools/trunk/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/DateTool.java
 Sun Nov 20 10:19:41 2016
@@ -28,16 +28,22 @@ import java.util.Calendar;
 import java.util.Locale;
 import java.util.TimeZone;
 import org.apache.velocity.tools.ConversionUtils;
-import org.apache.velocity.tools.ToolContext;
 import org.apache.velocity.tools.config.DefaultKey;
 
 /**
- * Tool for working with {@link Date} and {@link Calendar}
+ * <p>Tool for working with {@link Date} and {@link Calendar}
  * in Velocity templates.  It is useful for accessing and
  * formatting the "current" date as well as for formatting
  * arbitrary {@link Date} and {@link Calendar} objects. Also
  * the tool can be used to retrieve {@link DateFormat} instances
- * or make conversions to and from various date types.
+ * or make conversions to and from various date types.</p>
+ * <p>Possible formats include:
+ * <ul>
+ *     <li>'short', 'medium', 'long', 'full' (from {@link 
java.text.DateFormat}, optionally suffixed by '_time' or '_date' to get a 
specific time or date format</li>
+ *     <li>'intl' for RFC 3339 without time zone (locale and time-zone 
independent), optionally suffixed by '_time' or '_date' to get a specific time 
or date format</li>
+ *     <li>'iso' for RFC 3339 with time zone (locale independent), optionally 
suffixed by '_time' or '_date' to get a specific time or date format</li>
+ *     <li>a custom format, as specified in {@link SimpleDateFormat}</li>
+ * </ul></p>
  * <p><pre>
  * Example of formatting the "current" date:
  *  $date                         -> Oct 19, 2003 9:54:50 PM
@@ -46,6 +52,7 @@ import org.apache.velocity.tools.config.
  *  $date.full_date               -> Sunday, October 19, 2003
  *  $date.get('default','short')  -> Oct 19, 2003 9:54 PM
  *  $date.get('yyyy-M-d H:m:s')   -> 2003-10-19 21:54:50
+ *  $date.iso                     -> 2003-10-19 21:54:50-07:00
  *
  * Example of formatting an arbitrary date:
  *  $myDate                        -> Tue Oct 07 03:14:50 PDT 2003
@@ -77,7 +84,7 @@ public class DateTool extends FormatConf
      */
     public static final String TIMEZONE_KEY = "timezone";
 
-    private TimeZone timezone = TimeZone.getDefault(); 
+    private TimeZone timezone = TimeZone.getDefault();
 
     /**
      * Does the actual configuration. This is protected, so
@@ -567,7 +574,7 @@ public class DateTool extends FormatConf
      * @param locale the {@link Locale} to be used
      * @param timezone the {@link TimeZone} to be used
      * @return an instance of {@link DateFormat}
-     * @see #getDateFormat(int timeStyle, int dateStyle, Locale locale, 
TimeZone timezone)
+     * @see {@link DateFormat#getDateFormat(int timeStyle, int dateStyle, 
Locale locale, TimeZone timezone)}
      * @since VelocityTools 1.1
      */
     public DateFormat getDateFormat(String dateStyle, String timeStyle,


Reply via email to