sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1799424 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/internal/util/StandardDateFormat.java test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
Date Wed, 21 Jun 2017 10:48:34 GMT
Author: desruisseaux
Date: Wed Jun 21 10:48:34 2017
New Revision: 1799424

URL: http://svn.apache.org/viewvc?rev=1799424&view=rev
Log:
Fix a DateTimeException when formatting the epoch of Julian days.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java?rev=1799424&r1=1799423&r2=1799424&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/StandardDateFormat.java
[UTF-8] Wed Jun 21 10:48:34 2017
@@ -41,6 +41,7 @@ import java.time.temporal.TemporalAccess
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.DateTimeParseException;
+import java.time.format.SignStyle;
 
 
 /**
@@ -88,7 +89,7 @@ public final class StandardDateFormat ex
      */
     public static final DateTimeFormatter FORMAT = new DateTimeFormatterBuilder()
             // parseLenient() is for allowing fields with one digit instead of two.
-            .parseLenient()                    .appendValue(ChronoField.YEAR,           
 4)    // Proleptic year (use negative number if needed).
+            .parseLenient()                    .appendValue(ChronoField.YEAR, 4, 5, SignStyle.NORMAL)
   // Proleptic year (use negative number if needed).
             .optionalStart().appendLiteral('-').appendValue(ChronoField.MONTH_OF_YEAR,  
 2)
             .optionalStart().appendLiteral('-').appendValue(ChronoField.DAY_OF_MONTH,   
 2)
             .optionalStart().appendLiteral('T').appendValue(ChronoField.HOUR_OF_DAY,    
 2)

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java?rev=1799424&r1=1799423&r2=1799424&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/internal/util/StandardDateFormatTest.java
[UTF-8] Wed Jun 21 10:48:34 2017
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.internal.util;
 
+import java.util.Date;
 import java.text.ParseException;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -79,4 +80,25 @@ public final strictfp class StandardDate
         assertEquals(LocalDateTime.of(2016, 6, 27, 16, 48),                        StandardDateFormat.parseBest("2016-06-27T16:48"));
         assertEquals(LocalDate.of(2016, 6, 27),                                    StandardDateFormat.parseBest("2016-06-27"));
     }
+
+    /**
+     * Tests formatting and parsing a negative year.
+     * This test uses the Julian epoch (January 1st, 4713 BC at 12:00 UTC).
+     *
+     * @throws ParseException if an error occurred while parsing the date.
+     */
+    @Test
+    public void testNegativeYear() throws ParseException {
+        final Date julian = new Date(-210866760000000L);                // Same epoch than
CommonCRS.Temporal.JULIAN.
+        /*
+         * java.util.Date and java.time.format disagree on the result of formatting that
date,
+         * maybe because they handle differently some aspects like the transition from Julian
+         * calendar to Gregorian calendar (10 days).  For Julian day calculation purpose,
the
+         * java.util.Date way is a more accurate match.
+         */
+        final String expected = "-4713-11-24T12:00:00.000";
+        final StandardDateFormat f = new StandardDateFormat();
+        assertEquals(expected, f.format(julian));
+        assertEquals(julian, f.parse(expected));
+    }
 }



Mime
View raw message