jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From resc...@apache.org
Subject svn commit: r1870756 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/util/ISO8601.java test/java/org/apache/jackrabbit/util/ISO8601Test.java
Date Tue, 03 Dec 2019 14:14:00 GMT
Author: reschke
Date: Tue Dec  3 14:14:00 2019
New Revision: 1870756

URL: http://svn.apache.org/viewvc?rev=1870756&view=rev
Log:
JCR-4502: ISO8601: add variants outputting 'SHORT' format (no milliseconds)

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ISO8601Test.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java?rev=1870756&r1=1870755&r2=1870756&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ISO8601.java
Tue Dec  3 14:14:00 2019
@@ -326,6 +326,10 @@ public final class ISO8601 {
      * with more than four digits).
      */
     public static String format(Calendar cal) throws IllegalArgumentException {
+        return format(cal, true);
+    }
+
+    private static String format(Calendar cal, boolean includeMs) throws IllegalArgumentException
{
         if (cal == null) {
             throw new IllegalArgumentException("argument can not be null");
         }
@@ -355,9 +359,11 @@ public final class ISO8601 {
         buf.append(':');
         // second (ss)
         appendZeroPaddedInt(buf, cal.get(Calendar.SECOND), 2);
-        buf.append('.');
-        // millisecond (SSS)
-        appendZeroPaddedInt(buf, cal.get(Calendar.MILLISECOND), 3);
+        if (includeMs) {
+            buf.append('.');
+            // millisecond (SSS)
+            appendZeroPaddedInt(buf, cal.get(Calendar.MILLISECOND), 3);
+        }
         // time zone designator (Z or +00:00 or -00:00)
         TimeZone tz = cal.getTimeZone();
         // determine offset of timezone from UTC (incl. daylight saving)
@@ -403,6 +409,62 @@ public final class ISO8601 {
         return year;
     }
 
+
+    /**
+     * Variants that exclude the milliseconds from the formatted string.
+     *
+     */
+    public static class SHORT {
+
+        /**
+         * @see ISO8601#format(Date)
+         */
+        public static String format(Date date) throws IllegalArgumentException {
+            return format(date, 0);
+        }
+
+        /**
+         * @see ISO8601#format(Clock)
+         */
+        public static String format(Clock clock) throws IllegalArgumentException {
+            return format(clock.millis(), clock.getZone().getRules().getOffset(clock.instant()).getTotalSeconds());
+        }
+
+        /**
+         * @see ISO8601#format(long)
+         */
+        public static String format(long millisSinceEpoch) throws IllegalArgumentException
{
+            return format(millisSinceEpoch, 0);
+        }
+
+        /**
+         * @see ISO8601#format(Date, int)
+         */
+        public static String format(Date date, int tzOffsetInSeconds) throws IllegalArgumentException
{
+            if (date == null) {
+                throw new IllegalArgumentException("argument can not be null");
+            }
+            return format(date.getTime(), tzOffsetInSeconds);
+        }
+
+        /**
+         * @see ISO8601#format(long, int)
+         */
+        public static String format(long millisSinceEpoch, int tzOffsetInSeconds) throws
IllegalArgumentException {
+            Calendar cal = Calendar.getInstance();
+            cal.setTimeZone(tzOffsetInSeconds == 0 ? UTC : new SimpleTimeZone(tzOffsetInSeconds
* 1000, ""));
+            cal.setTimeInMillis(millisSinceEpoch);
+            return format(cal);
+        }
+
+        /**
+         * @see ISO8601#format(Calendar)
+         */
+        public static String format(Calendar cal) throws IllegalArgumentException {
+            return ISO8601.format(cal, false);
+        }
+    }
+
     /**
      * Appends a zero-padded number to the given string buffer.
      * <p>

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ISO8601Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ISO8601Test.java?rev=1870756&r1=1870755&r2=1870756&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ISO8601Test.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ISO8601Test.java
Tue Dec  3 14:14:00 2019
@@ -76,6 +76,30 @@ public class ISO8601Test extends TestCas
         assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(new Date(123456789012L),
0));
     }
 
+    public void testFormatUTCShort() {
+        Calendar c = Calendar.getInstance();
+        c.setTimeZone(TimeZone.getTimeZone("UTC"));
+        Clock clock;
+
+        c.setTimeInMillis(0);
+        clock = Clock.fixed(Instant.ofEpochMilli(0), ZoneId.of("Z"));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(c));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(clock));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(0));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(new Date(0)));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(0, 0));
+        assertEquals("1970-01-01T00:00:00Z", ISO8601.SHORT.format(new Date(0), 0));
+
+        c.setTimeInMillis(123456789012L);
+        clock = Clock.fixed(Instant.ofEpochMilli(123456789012L), ZoneId.of("Z"));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(c));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(clock));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(123456789012L));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(new Date(123456789012L)));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(123456789012L, 0));
+        assertEquals("1973-11-29T21:33:09Z", ISO8601.SHORT.format(new Date(123456789012L),
0));
+    }
+
     public void testFormatCustomTz() {
         Calendar c = Calendar.getInstance();
         c.setTimeZone(customPlus);



Mime
View raw message