You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2019/11/27 13:07:28 UTC

svn commit: r1870503 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/util/ISO8601.java test/java/org/apache/jackrabbit/util/ISO8601Test.java

Author: reschke
Date: Wed Nov 27 13:07:27 2019
New Revision: 1870503

URL: http://svn.apache.org/viewvc?rev=1870503&view=rev
Log:
JCR-4502: ISO8601: add convenience methods that do not require passing a Calendar - also add support for java.time.Clock

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=1870503&r1=1870502&r2=1870503&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 Wed Nov 27 13:07:27 2019
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.util;
 
+import java.time.Clock;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
@@ -245,6 +246,20 @@ public final class ISO8601 {
     }
 
     /**
+     * Formats a clock time instant into an ISO8601-compliant date/time string.
+     * 
+     * @param clock
+     *            clock to obtain time and time zone from
+     * @return the formatted date/time string.
+     * @throws IllegalArgumentException
+     *             if the calendar cannot be represented as defined by ISO 8601
+     *             (i.e. year with more than four digits).
+     */
+    public static String format(Clock clock) throws IllegalArgumentException {
+        return format(clock.millis(), clock.getZone().getRules().getOffset(clock.instant()).getTotalSeconds());
+    }
+
+    /**
      * Formats a time instant into an ISO8601-compliant date/time string using
      * the UTC timezone.
      *
@@ -265,18 +280,18 @@ public final class ISO8601 {
      *
      * @param date
      *            date to be formatted
-     * @param tzOffsetInMillis
-     *            timezone offset from UTC in milliseconds
+     * @param tzOffsetInSeconds
+     *            timezone offset from UTC in seconds
      * @return the formatted date/time string.
      * @throws IllegalArgumentException
      *             if the calendar cannot be represented as defined by ISO 8601
      *             (i.e. year with more than four digits).
      */
-    public static String format(Date date, int tzOffsetInMillis) throws IllegalArgumentException {
+    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(), tzOffsetInMillis);
+        return format(date.getTime(), tzOffsetInSeconds);
     }
 
     /**
@@ -285,17 +300,17 @@ public final class ISO8601 {
      *
      * @param millisSinceEpoch
      *            milliseconds since the epoch
-     * @param tzOffsetInMillis
-     *            timezone offset from UTC in milliseconds
+     * @param tzOffsetInSeconds
+     *            timezone offset from UTC in seconds
      * @return the formatted date/time string.
      * @throws IllegalArgumentException
      *             if a <code>null</code> argument is passed the calendar cannot
      *             be represented as defined by ISO 8601 (i.e. year with more
      *             than four digits).
      */
-    public static String format(long millisSinceEpoch, int tzOffsetInMillis) throws IllegalArgumentException {
+    public static String format(long millisSinceEpoch, int tzOffsetInSeconds) throws IllegalArgumentException {
         Calendar cal = Calendar.getInstance();
-        cal.setTimeZone(tzOffsetInMillis == 0 ? UTC : new SimpleTimeZone(tzOffsetInMillis, ""));
+        cal.setTimeZone(tzOffsetInSeconds == 0 ? UTC : new SimpleTimeZone(tzOffsetInSeconds * 1000, ""));
         cal.setTimeInMillis(millisSinceEpoch);
         return format(cal);
     }

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=1870503&r1=1870502&r2=1870503&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 Wed Nov 27 13:07:27 2019
@@ -16,6 +16,9 @@
  */
 package org.apache.jackrabbit.util;
 
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneId;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
@@ -27,15 +30,17 @@ import junit.framework.TestCase;
 public class ISO8601Test extends TestCase {
 
     private TimeZone customPlus = new SimpleTimeZone(83 * 60 * 1000, "test");
-    private TimeZone customMinus = new SimpleTimeZone(-1202 * 60 * 1000, "test");
+    private TimeZone customMinus = new SimpleTimeZone(-122 * 60 * 1000, "test");
+    private ZoneId customPlusZI = ZoneId.of("+01:23");
+    private ZoneId customMinusZI = ZoneId.of("-02:02");
 
     public void testFormatThrowsIllegalArgumentException() {
         try {
-            ISO8601.format((Calendar)null);
+            ISO8601.format((Calendar) null);
         } catch (IllegalArgumentException expected) {
         }
         try {
-            ISO8601.format((Date)null);
+            ISO8601.format((Date) null);
         } catch (IllegalArgumentException expected) {
         }
     }
@@ -50,16 +55,21 @@ public class ISO8601Test extends TestCas
     public void testFormatUTC() {
         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:00.000Z", ISO8601.format(c));
+        assertEquals("1970-01-01T00:00:00.000Z", ISO8601.format(clock));
         assertEquals("1970-01-01T00:00:00.000Z", ISO8601.format(0));
         assertEquals("1970-01-01T00:00:00.000Z", ISO8601.format(new Date(0)));
         assertEquals("1970-01-01T00:00:00.000Z", ISO8601.format(0, 0));
         assertEquals("1970-01-01T00:00:00.000Z", ISO8601.format(new Date(0), 0));
 
         c.setTimeInMillis(123456789012L);
+        clock = Clock.fixed(Instant.ofEpochMilli(123456789012L), ZoneId.of("Z"));
         assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(c));
+        assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(clock));
         assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(123456789012L));
         assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(new Date(123456789012L)));
         assertEquals("1973-11-29T21:33:09.012Z", ISO8601.format(123456789012L, 0));
@@ -69,28 +79,37 @@ public class ISO8601Test extends TestCas
     public void testFormatCustomTz() {
         Calendar c = Calendar.getInstance();
         c.setTimeZone(customPlus);
+        Clock clock;
 
         c.setTimeInMillis(0);
+        clock = Clock.fixed(Instant.ofEpochMilli(0), customPlusZI);
         assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(c));
-        assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(0, customPlus.getRawOffset()));
-        assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(new Date(0), customPlus.getRawOffset()));
+        assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(clock));
+        assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(0, customPlus.getRawOffset() / 1000));
+        assertEquals("1970-01-01T01:23:00.000+01:23", ISO8601.format(new Date(0), customPlus.getRawOffset() / 1000));
 
         c.setTimeInMillis(123456789012L);
+        clock = Clock.fixed(Instant.ofEpochMilli(123456789012L), customPlusZI);
         assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(c));
-        assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(123456789012L, customPlus.getRawOffset()));
-        assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(new Date(123456789012L), customPlus.getRawOffset()));
+        assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(clock));
+        assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(123456789012L, customPlus.getRawOffset() / 1000));
+        assertEquals("1973-11-29T22:56:09.012+01:23", ISO8601.format(new Date(123456789012L), customPlus.getRawOffset() / 1000));
 
         c.setTimeZone(customMinus);
 
         c.setTimeInMillis(0);
-        assertEquals("1969-12-31T03:58:00.000-20:02", ISO8601.format(c));
-        assertEquals("1969-12-31T03:58:00.000-20:02", ISO8601.format(0, customMinus.getRawOffset()));
-        assertEquals("1969-12-31T03:58:00.000-20:02", ISO8601.format(new Date(0), customMinus.getRawOffset()));
+        clock = Clock.fixed(Instant.ofEpochMilli(0), customMinusZI);
+        assertEquals("1969-12-31T21:58:00.000-02:02", ISO8601.format(c));
+        assertEquals("1969-12-31T21:58:00.000-02:02", ISO8601.format(clock));
+        assertEquals("1969-12-31T21:58:00.000-02:02", ISO8601.format(0, customMinus.getRawOffset() / 1000));
+        assertEquals("1969-12-31T21:58:00.000-02:02", ISO8601.format(new Date(0), customMinus.getRawOffset() / 1000));
 
         c.setTimeInMillis(123456789012L);
-        assertEquals("1973-11-29T01:31:09.012-20:02", ISO8601.format(c));
-        assertEquals("1973-11-29T01:31:09.012-20:02", ISO8601.format(123456789012L, customMinus.getRawOffset()));
-        assertEquals("1973-11-29T01:31:09.012-20:02", ISO8601.format(new Date(123456789012L), customMinus.getRawOffset()));
+        clock = Clock.fixed(Instant.ofEpochMilli(123456789012L), customMinusZI);
+        assertEquals("1973-11-29T19:31:09.012-02:02", ISO8601.format(c));
+        assertEquals("1973-11-29T19:31:09.012-02:02", ISO8601.format(clock));
+        assertEquals("1973-11-29T19:31:09.012-02:02", ISO8601.format(123456789012L, customMinus.getRawOffset() / 1000));
+        assertEquals("1973-11-29T19:31:09.012-02:02", ISO8601.format(new Date(123456789012L), customMinus.getRawOffset() / 1000));
     }
 
     public void testParseUTC() {
@@ -112,11 +131,11 @@ public class ISO8601Test extends TestCas
         assertEquals(123456789012L, c.getTimeInMillis());
         assertEquals(customPlus.getRawOffset(), c.getTimeZone().getRawOffset());
 
-        c = ISO8601.parse("1969-12-31T03:58:00.000-20:02");
+        c = ISO8601.parse("1969-12-31T21:58:00.000-02:02");
         assertEquals(0, c.getTimeInMillis());
         assertEquals(customMinus.getRawOffset(), c.getTimeZone().getRawOffset());
 
-        c = ISO8601.parse("1973-11-29T01:31:09.012-20:02");
+        c = ISO8601.parse("1973-11-29T19:31:09.012-02:02");
         assertEquals(123456789012L, c.getTimeInMillis());
         assertEquals(customMinus.getRawOffset(), c.getTimeZone().getRawOffset());
     }