velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1771188 - in /velocity/tools/trunk/velocity-tools-generic/src: main/java/org/apache/velocity/tools/ConversionUtils.java main/java/org/apache/velocity/tools/generic/DateTool.java test/java/org/apache/velocity/tools/generic/DateToolTests.java
Date Thu, 24 Nov 2016 16:49:24 GMT
Author: cbrisson
Date: Thu Nov 24 16:49:23 2016
New Revision: 1771188

URL: http://svn.apache.org/viewvc?rev=1771188&view=rev
Log:
[tools] review DateTool new formatting styles.

Here's the new proposed implementation:

 * 'short', 'medium', 'long', 'full' (from java.text.DateFormat), optionally suffixed by '_date'
or '_time' to get a date-only or time-only format        
 * 'iso' for extended ISO 8601 without time zone (ex: '2016-11-24T10:27:30'), optionally suffixed
by '_date' or '_time' to get a date-only or time-only format</li>
 * 'iso_tz', like 'iso' with time zone offset (ex: '2016-11-24T10:27:30+01:00'), optionally
suffixed by '_time' to get a time-only format ('10:27:30+01:00')</li>
 * 'intl', like 'iso' but with a space separator between date and time (ex: '2016-11-24 10:27:30'),
optionally suffixed by '_date' or '_time' to get a date-only or time-only format</li>
 * 'intl_tz', like 'intl' but with the time zone short id suffixed after another space (ex:
'2016-11-24 10:27:30 CET'), optionally suffixed by '_time' to get a time-only format ('10:27:30+01:00')</li>
 * a custom format, as specified in java.text.SimpleDateFormat


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
    velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/DateToolTests.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=1771188&r1=1771187&r2=1771188&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
Thu Nov 24 16:49:23 2016
@@ -55,16 +55,20 @@ public class ConversionUtils
     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)
*/
-
+    private static final int STYLE_ISO = 5;  /* ISO 8601 format */
+    private static final int STYLE_ISO_TZ = 6; /* ISO 8601 format with timezone offset */
+    private static final int STYLE_INTL = 7;  /* ISO 8601 human-readable format */
+    private static final int STYLE_INTL_TZ = 8; /* ISO 8601 human-readable format with timezone
ID */
 
     /* 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 */
+    private static DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd");  /* ISO
8601 date */
+    private static DateFormat isoTimeFormat = new SimpleDateFormat("HH:mm:ss"); /* ISO 8601
time */
+    private static DateFormat isoTimestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/* ISO 88601 timestamp */
+    private static DateFormat intlTimestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  /* human-readable ISO-8601 timestamp */
+    private static DateFormat isoTimeTzFormat = new SimpleDateFormat("HH:mm:ssXXX"); /* ISO
8601 time with timezone offset */
+    private static DateFormat isoTimestampTzFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
/* ISO 88601 timestamp with timezone offset */
+    private static DateFormat intlTimeTzFormat = new SimpleDateFormat("HH:mm:ss zzz");  
/* human-readable ISO-8601 time with timezone Olson ID */
+    private static DateFormat intlTimestampTzFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss
zzz");   /* human-readable ISO-8601 timestamp with timezone Olson ID */
 
     // cache custom formats
     private static ConcurrentMap<String,NumberFormat> customFormatsCache = new ConcurrentHashMap<String,NumberFormat>();
@@ -417,8 +421,10 @@ public class ConversionUtils
                 // only a date style was specified
                 switch (dateStyle)
                 {
-                    case STYLE_INTL:
                     case STYLE_ISO:
+                    case STYLE_ISO_TZ: /* isgnore TZ */
+                    case STYLE_INTL:
+                    case STYLE_INTL_TZ:  /* ignore TZ */
                         df = isoDateFormat;
                         break;
                     default:
@@ -432,11 +438,16 @@ public class ConversionUtils
                 // only a time style was specified
                 switch (timeStyle)
                 {
+                    case STYLE_ISO:
                     case STYLE_INTL:
-                        df = intlTimeFormat;
+                        df = isoTimeFormat;
                         break;
-                    case STYLE_ISO:
-                        df = (DateFormat) isoTimeFormat.clone();
+                    case STYLE_ISO_TZ:
+                        df = (DateFormat)isoTimeTzFormat.clone();
+                        df.setTimeZone(timezone);
+                        break;
+                    case STYLE_INTL_TZ:
+                        df = (DateFormat)intlTimeTzFormat.clone();
                         df.setTimeZone(timezone);
                         break;
                     default:
@@ -449,11 +460,18 @@ public class ConversionUtils
             {
                 switch (dateStyle)
                 {
+                    case STYLE_ISO:
+                        df = isoTimestampFormat;
+                        break;
+                    case STYLE_ISO_TZ:
+                        df = (DateFormat)isoTimestampTzFormat.clone();
+                        df.setTimeZone(timezone);
+                        break;
                     case STYLE_INTL:
                         df = intlTimestampFormat;
                         break;
-                    case STYLE_ISO:
-                        df = (DateFormat) isoTimestampFormat.clone();
+                    case STYLE_INTL_TZ:
+                        df = (DateFormat) intlTimestampTzFormat.clone();
                         df.setTimeZone(timezone);
                         break;
                     default:
@@ -480,8 +498,10 @@ public class ConversionUtils
         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);
+        stylesMap.put("ISO_TZ", STYLE_ISO_TZ);
+        stylesMap.put("INTL", STYLE_INTL);
+        stylesMap.put("INTL_TZ", STYLE_INTL_TZ);
     }
 
     /**

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=1771188&r1=1771187&r2=1771188&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
Thu Nov 24 16:49:23 2016
@@ -39,12 +39,13 @@ import org.apache.velocity.tools.config.
  * 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>'short', 'medium', 'long', 'full' (from {@link java.text.DateFormat}, optionally
suffixed by '_date' or '_time' to get a date-only or time-only format</li>
+ *     <li>'iso' for extended ISO 8601 without time zone (ex: '2016-11-24T10:27:30'),
optionally suffixed by '_date' or '_time' to get a date-only or time-only format</li>
+ *     <li>'iso_tz', like 'iso' with time zone offset (ex: '2016-11-24T10:27:30+01:00'),
optionally suffixed by '_time' to get a time-only format ('10:27:30+01:00')</li>
+ *     <li>'intl', like 'iso' but with a space separator between date and time (ex:
'2016-11-24 10:27:30'), optionally suffixed by '_date' or '_time' to get a date-only or time-only
format</li>
+ *     <li>'intl_tz', like 'intl' but with the time zone short id suffixed after another
space (ex: '2016-11-24 10:27:30 CET'), optionally suffixed by '_time' to get a time-only format
('10:27:30+01:00')</li>
  *     <li>a custom format, as specified in {@link SimpleDateFormat}</li>
  * </ul></p>
- * <p>Iso formats don't display milliseconds and use a space separator between date
and time.</p>
  * <p><pre>
  * Example of formatting the "current" date:
  *  $date                         -> Oct 19, 2003 9:54:50 PM
@@ -53,7 +54,9 @@ 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
+ *  $date.iso                     -> 2003-10-19T21:54:50-07:00
+ *  $date.iso_tz_time             -> 21:54:50-07:00
+ *  $date.intl_tz                 -> 2003-10-19 21:54:50 CET
  *
  * Example of formatting an arbitrary date:
  *  $myDate                        -> Tue Oct 07 03:14:50 PDT 2003
@@ -68,6 +71,9 @@ import org.apache.velocity.tools.config.
  * &lt;/tools&gt;
  * </pre></p>
  *
+ * <p>Should you need to use several formats, you can either use explicit formats by
means of the <code>toDate(format, date)</code> method,
+ * or you can declare several date tool instances with different formats.</p>
+ *
  * <p>The methods of this tool are highly interconnected, and overriding
  * key methods provides an easy way to create subclasses that use
  * a non-default format, calendar, locale, or timezone.</p>
@@ -420,40 +426,17 @@ public class DateTool extends FormatConf
      *
      * <p>
      * The specified format may be a standard style pattern ('full', 'long',
-     * 'medium', 'short', or 'default').
+     * 'medium', 'short', or 'default') or extended style pattern ('iso', 'iso_tz', 'intl',
'intl_tz').
      * </p>
      * <p>
      * You may also specify that you want only the date or time portion be
      * appending '_date' or '_time' respectively to the standard style pattern.
-     * (e.g. 'full_date' or 'long_time')
+     * (e.g. 'full_date', 'long_time', 'intl_date')
      * </p>
      * <p>
      * If the format fits neither of these patterns, then the output
      * will be formatted according to the symbols defined by
-     * {@link SimpleDateFormat}:
-     * <pre>
-     *   Symbol   Meaning                 Presentation        Example
-     *   ------   -------                 ------------        -------
-     *   G        era designator          (Text)              AD
-     *   y        year                    (Number)            1996
-     *   M        month in year           (Text & Number)     July & 07
-     *   d        day in month            (Number)            10
-     *   h        hour in am/pm (1~12)    (Number)            12
-     *   H        hour in day (0~23)      (Number)            0
-     *   m        minute in hour          (Number)            30
-     *   s        second in minute        (Number)            55
-     *   S        millisecond             (Number)            978
-     *   E        day in week             (Text)              Tuesday
-     *   D        day in year             (Number)            189
-     *   F        day of week in month    (Number)            2 (2nd Wed in July)
-     *   w        week in year            (Number)            27
-     *   W        week in month           (Number)            2
-     *   a        am/pm marker            (Text)              PM
-     *   k        hour in day (1~24)      (Number)            24
-     *   K        hour in am/pm (0~11)    (Number)            0
-     *   z        time zone               (Text)              Pacific Standard Time
-     *   '        escape for text         (Delimiter)
-     *   ''       single quote            (Literal)           '
+     * {@link SimpleDateFormat}.
      *
      *   Examples: "E, MMMM d" will result in "Tue, July 24"
      *             "EEE, M-d (H:m)" will result in "Tuesday, 7-24 (14:12)"

Modified: velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java?rev=1771188&r1=1771187&r2=1771188&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java
(original)
+++ velocity/tools/trunk/velocity-tools-generic/src/test/java/org/apache/velocity/tools/generic/DateToolTests.java
Thu Nov 24 16:49:23 2016
@@ -93,8 +93,15 @@ public class DateToolTests
         DateTool dt = new DateTool();
         Date date = new Date();
         dt.setTimeZone(TimeZone.getTimeZone("CET"));
-        assertEquals("DateTool incorrectly formatted iso format",
-                new SimpleDateFormat("yyyy-MM-dd HH:mm:ssXXX").format(date),
-                dt.format("iso",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(date),
dt.format("iso", date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(date),
dt.format("iso_tz",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss").format(date), dt.format("intl",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss zzz").format(date), dt.format("intl_tz",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd").format(date),
dt.format("iso_date",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("yyyy-MM-dd").format(date),
dt.format("intl_date",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("HH:mm:ss").format(date),
dt.format("iso_time",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("HH:mm:ss").format(date),
dt.format("intl_time",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("HH:mm:ssXXX").format(date),
dt.format("iso_tz_time",date));
+        assertEquals("DateTool incorrectly formatted iso format", new SimpleDateFormat("HH:mm:ss
zzz").format(date), dt.format("intl_tz_time",date));
     }
 }



Mime
View raw message