You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/12/30 22:03:50 UTC

[1/2] jena git commit: JENA-837 : Use FastDateFormat.format(Calendar) to do the timezone.

Repository: jena
Updated Branches:
  refs/heads/master 5af38dde8 -> 311ee927f


JENA-837 : Use FastDateFormat.format(Calendar) to do the timezone.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/0c7285c2
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/0c7285c2
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/0c7285c2

Branch: refs/heads/master
Commit: 0c7285c279d340703c9bcdbfe4d99e3ad14bdea8
Parents: 5af38dd
Author: Andy Seaborne <an...@apache.org>
Authored: Tue Dec 30 20:35:14 2014 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Tue Dec 30 20:35:14 2014 +0000

----------------------------------------------------------------------
 .../java/com/hp/hpl/jena/sparql/util/Utils.java | 169 +++++++++----------
 .../com/hp/hpl/jena/sparql/util/TestUtils.java  |  10 +-
 2 files changed, 82 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/0c7285c2/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
index e1ce615..2cd29c3 100644
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
+++ b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
@@ -16,154 +16,141 @@
  * limitations under the License.
  */
 
-package com.hp.hpl.jena.sparql.util;
+package com.hp.hpl.jena.sparql.util ;
 
 import java.math.BigDecimal ;
 import java.util.Calendar ;
 import java.util.Date ;
 import java.util.GregorianCalendar ;
-import java.util.TimeZone ;
 
-import org.apache.commons.lang3.time.FastDateFormat;
+import org.apache.commons.lang3.time.FastDateFormat ;
 
 import com.hp.hpl.jena.datatypes.xsd.XSDDateTime ;
 
 /** Miscellaneous operations - not query specific */
 
-public class Utils
-{
+public class Utils {
+    // Include timezone (even xsd:dates have timezones; Calendars have
+    // timezones)
+    // NB in SimpleDateFormat != FastDateFormat
+    //  SimpleDateFormat does not format Calendars.
+    //  SimpleDateFormat has "X" for  ISO format tmezones (+00:00)  
+    //    FastDateFormat uses "ZZ" for this.
     private static final FastDateFormat dateTimeFmt_display = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss z") ;
-    private static final FastDateFormat dateFmt_yyyymmdd    = FastDateFormat.getInstance("yyyy-MM-dd") ;
-    private static final FastDateFormat dateTimeFmt_XSD     = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS") ;
-    private static final FastDateFormat timeFmt_hhmmssSS    = FastDateFormat.getInstance("HH:mm:ss.SSS");
+    private static final FastDateFormat dateFmt_yyyymmdd    = FastDateFormat.getInstance("yyyy-MM-ddZZ") ;
+    private static final FastDateFormat dateTimeFmt_XSD     = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ") ;
+    private static final FastDateFormat timeFmt_hhmmssSS    = FastDateFormat.getInstance("HH:mm:ss.SSSZZ") ;
 
     static public String className(Object obj) {
         if ( obj == null )
             return "null" ;
         return classShortName(obj.getClass()) ;
     }
-    
-    static public String classShortName(Class<?> cls)
-    {
+
+    static public String classShortName(Class<? > cls) {
         String tmp = cls.getName() ;
         int i = tmp.lastIndexOf('.') ;
-        tmp = tmp.substring(i+1) ;
+        tmp = tmp.substring(i + 1) ;
         return tmp ;
     }
-    
-    public static String nowAsXSDDateTimeString()
-    {
+
+    public static String nowAsXSDDateTimeString() {
         return calendarToXSDDateTimeString(new GregorianCalendar()) ;
     }
-    
-    public static String todayAsXSDDateString()
-    {
+
+    public static String todayAsXSDDateString() {
         return calendarToXSDDateString(new GregorianCalendar()) ;
     }
 
-    public static String XSDDateTime2String(XSDDateTime xdt)
-    {
+    public static String XSDDateTime2String(XSDDateTime xdt) {
         return xdt.toString() ;
     }
-    
+
     /** Return "now" as readable string (date in yyyy/MM/dd format) */
-    public static String nowAsString()
-    { return nowAsString(dateTimeFmt_display) ; }
-    
-    public static String nowAsString(String formatString)
-    {
+    public static String nowAsString() {
+        return nowAsString(dateTimeFmt_display) ;
+    }
+
+    public static String nowAsString(String formatString) {
         FastDateFormat df = FastDateFormat.getInstance(formatString) ;
         return df.format(new Date()) ;
     }
 
-    public static String nowAsString(FastDateFormat dateFormat)
-    {
+    public static String nowAsString(FastDateFormat dateFormat) {
         return dateFormat.format(new Date()) ;
     }
 
-    public static String calendarToXSDDateTimeString(Calendar cal)
-    {
+    public static String calendarToXSDDateTimeString(Calendar cal) {
         return calendarToXSDString(cal, dateTimeFmt_XSD) ;
     }
-    
-    public static String calendarToXSDDateString(Calendar cal)
-    {
+
+    public static String calendarToXSDDateString(Calendar cal) {
         return calendarToXSDString(cal, dateFmt_yyyymmdd) ;
     }
-    
-    public static String calendarToXSDTimeString(Calendar cal)
-    {
-        return calendarToXSDString(cal, timeFmt_hhmmssSS);
-    }
 
-    private static String calendarToXSDString(Calendar cal, FastDateFormat fmt )
-    {
-        // c.f. Constructor on Jena's XSDDateTime
-        // Only issue is that it looses the timezone through (Xerces)
-        // normalizing to UTC.
-        Date date = cal.getTime();
-        String lex = fmt.format(date);
-        lex = lex + calcTimezone(cal);
-        return lex;
+    public static String calendarToXSDTimeString(Calendar cal) {
+        return calendarToXSDString(cal, timeFmt_hhmmssSS) ;
     }
 
-    private static String calcTimezone(Calendar cal)
-    {
-        Date date = cal.getTime() ;
-        TimeZone z = cal.getTimeZone() ;
-        int tz = z.getRawOffset();
+    private static String calendarToXSDString(Calendar cal, FastDateFormat fmt) {
+        String lex = fmt.format(cal) ;
+        // lex = lex + calcTimezone(cal) ;
+        return lex ;
+    }
 
-        if ( z.inDaylightTime(date) )
-        {
-            int tzDst = z.getDSTSavings() ;
-            tz = tz + tzDst ;
-        }
-        
-        String sign = "+" ;
-        if ( tz < 0 )
-        {
-            sign = "-" ;
-            tz = -tz ;
-        }
+    // No tneeded for FastDateFormat.s
+//    private static String calcTimezone(Calendar cal) {
+//        Date date = cal.getTime() ;
+//        TimeZone z = cal.getTimeZone() ;
+//        int tz = z.getRawOffset() ;
+//
+//        if ( z.inDaylightTime(date) ) {
+//            int tzDst = z.getDSTSavings() ;
+//            tz = tz + tzDst ;
+//        }
+//
+//        String sign = "+" ;
+//        if ( tz < 0 ) {
+//            sign = "-" ;
+//            tz = -tz ;
+//        }
+//
+//        int tzH = tz / (60 * 60 * 1000) ; // Integer divide towards zero.
+//        int tzM = (tz - tzH * 60 * 60 * 1000) / (60 * 1000) ;
+//
+//        String tzH_str = Integer.toString(tzH) ;
+//        String tzM_str = Integer.toString(tzM) ;
+//
+//        if ( tzH < 10 )
+//            tzH_str = "0" + tzH_str ;
+//        if ( tzM < 10 )
+//            tzM_str = "0" + tzM_str ;
+//        return sign + tzH_str + ":" + tzM_str ;
+//    }
 
-        int tzH = tz/(60*60*1000) ;             // Integer divide towards zero.
-        int tzM = (tz-tzH*60*60*1000)/(60*1000) ;
-        
-        String tzH_str = Integer.toString(tzH) ;
-        String tzM_str = Integer.toString(tzM) ;
-        
-        if ( tzH < 10 )
-            tzH_str = "0"+ tzH_str ;
-        if ( tzM < 10 )
-            tzM_str = "0"+ tzM_str ;
-        return sign+tzH_str+":"+tzM_str ;
-    }
-    
-    static public String stringForm(BigDecimal decimal)
-    { 
+    static public String stringForm(BigDecimal decimal) {
         return decimal.toPlainString() ;
     }
-    
-    static public String stringForm(double d)
-    { 
-        if ( Double.isInfinite(d) )
-        {
-            if ( d < 0 ) return "-INF" ; 
+
+    static public String stringForm(double d) {
+        if ( Double.isInfinite(d) ) {
+            if ( d < 0 )
+                return "-INF" ;
             return "INF" ;
         }
 
-        if ( Double.isNaN(d) ) return "NaN" ;
-        
+        if ( Double.isNaN(d) )
+            return "NaN" ;
+
         // Otherwise, SPARQL form always has "e0"
         String x = Double.toString(d) ;
         if ( (x.indexOf('e') != -1) || (x.indexOf('E') != -1) )
             return x ;
         // Renormalize?
-        return x+"e0" ;
+        return x + "e0" ;
     }
-    
-    static public String stringForm(float f)
-    { 
+
+    static public String stringForm(float f) {
         // No SPARQL short form.
         return Float.toString(f) ;
     }

http://git-wip-us.apache.org/repos/asf/jena/blob/0c7285c2/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
index e2d59b7..1bd66ae 100644
--- a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
+++ b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
@@ -35,7 +35,7 @@ public class TestUtils {
 		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1,"Z") ;
 		assertEquals("1984-03-22T14:32:01.000+00:00", calendarToXSDDateTimeString(cal));
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
-		assertEquals("1984-03-22T14:32:01.000-07:00", calendarToXSDDateTimeString(cal));
+		assertEquals("1984-03-22T07:32:01.000-07:00", calendarToXSDDateTimeString(cal));
 	}
 
 	@Test
@@ -45,17 +45,15 @@ public class TestUtils {
 		assertEquals("1984-03-22+00:00", calendarToXSDDateString(cal));
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
 		assertEquals("1984-03-22-07:00", calendarToXSDDateString(cal));
-
 	}
 
 	@Test
 	public void testCalendarToXSDTimeString() throws Exception {
 		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, "GMT+01:00");
-		cal.setTimeZone(TimeZone.getTimeZone("GMT+01:00")) ;
-		// Moves the cal
-		assertEquals("13:32:01.000+01:00", calendarToXSDTimeString(cal));
+		assertEquals("14:32:01.000+01:00", calendarToXSDTimeString(cal));
+		// Different timezone - moves the cal point-in-time.
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
-		assertEquals("13:32:01.000-07:00", calendarToXSDTimeString(cal));
+		assertEquals("06:32:01.000-07:00", calendarToXSDTimeString(cal));
 	}
 	
 	private static Calendar createCalendar(int year, int month, int dayOfMonth, int hourOfDay,


[2/2] jena git commit: JENA-838 : Sort of canonical time portions.

Posted by an...@apache.org.
JENA-838 : Sort of canonical time portions.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/311ee927
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/311ee927
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/311ee927

Branch: refs/heads/master
Commit: 311ee927f9c51e8e56671a4bb8728421a5534d9f
Parents: 0c7285c
Author: Andy Seaborne <an...@apache.org>
Authored: Tue Dec 30 21:03:29 2014 +0000
Committer: Andy Seaborne <an...@apache.org>
Committed: Tue Dec 30 21:03:29 2014 +0000

----------------------------------------------------------------------
 .../java/com/hp/hpl/jena/sparql/util/Utils.java | 37 +++++++++++++---
 .../com/hp/hpl/jena/sparql/util/TestUtils.java  | 46 ++++++++++++++------
 2 files changed, 64 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/311ee927/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
index 2cd29c3..1b97627 100644
--- a/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
+++ b/jena-arq/src/main/java/com/hp/hpl/jena/sparql/util/Utils.java
@@ -38,9 +38,24 @@ public class Utils {
     //    FastDateFormat uses "ZZ" for this.
     private static final FastDateFormat dateTimeFmt_display = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss z") ;
     private static final FastDateFormat dateFmt_yyyymmdd    = FastDateFormat.getInstance("yyyy-MM-ddZZ") ;
-    private static final FastDateFormat dateTimeFmt_XSD     = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ") ;
-    private static final FastDateFormat timeFmt_hhmmssSS    = FastDateFormat.getInstance("HH:mm:ss.SSSZZ") ;
-
+    
+    // Canonical form of datetimes: as of XML Schema Datatypes 1.1
+    // http://www.w3.org/TR/xmlschema11-2/
+    // The canonical form of seconds, including fractional part is the minimum length number,
+    // so the milliseconds never ends in zero.  
+    // Whole seconds have no fractional part.
+    // Jena uses millis as 3 digits if non-zero.
+    
+    // For milliseconds == 0
+    private static final FastDateFormat dateTimeFmt_XSD_ms0     = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ") ;
+    // For milliseconds != 0
+    private static final FastDateFormat dateTimeFmt_XSD_ms      = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ") ;
+
+    // For milliseconds == 0
+    private static final FastDateFormat timeFmt_XSD_ms0         = FastDateFormat.getInstance("HH:mm:ssZZ") ;
+    // For milliseconds != 0
+    private static final FastDateFormat timeFmt_XSD_ms          = FastDateFormat.getInstance("HH:mm:ss.SSSZZ") ;
+    
     static public String className(Object obj) {
         if ( obj == null )
             return "null" ;
@@ -80,16 +95,28 @@ public class Utils {
         return dateFormat.format(new Date()) ;
     }
 
+    private static boolean hasZeroMilliSeconds(Calendar cal) {
+        return ! cal.isSet(Calendar.MILLISECOND) || cal.get(Calendar.MILLISECOND) == 0 ;
+    }
+    
+    // Canonical fom : if ms == 0, don't include in the string.
     public static String calendarToXSDDateTimeString(Calendar cal) {
-        return calendarToXSDString(cal, dateTimeFmt_XSD) ;
+        FastDateFormat fmt = hasZeroMilliSeconds(cal) 
+            ? dateTimeFmt_XSD_ms0 
+            : dateTimeFmt_XSD_ms ;
+        return calendarToXSDString(cal, fmt) ;
     }
 
     public static String calendarToXSDDateString(Calendar cal) {
         return calendarToXSDString(cal, dateFmt_yyyymmdd) ;
     }
 
+    // Canonical fom : if ms == 0, don't include in the string.
     public static String calendarToXSDTimeString(Calendar cal) {
-        return calendarToXSDString(cal, timeFmt_hhmmssSS) ;
+        FastDateFormat fmt = hasZeroMilliSeconds(cal) 
+            ? timeFmt_XSD_ms0 
+            : timeFmt_XSD_ms ;
+        return calendarToXSDString(cal, fmt) ;
     }
 
     private static String calendarToXSDString(Calendar cal, FastDateFormat fmt) {

http://git-wip-us.apache.org/repos/asf/jena/blob/311ee927/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
index 1bd66ae..cb9aced 100644
--- a/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
+++ b/jena-arq/src/test/java/com/hp/hpl/jena/sparql/util/TestUtils.java
@@ -31,36 +31,54 @@ import org.junit.Test;
 public class TestUtils {
 
 	@Test
-	public void testCalendarToXSDDateTimeString() throws Exception {
-		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1,"Z") ;
-		assertEquals("1984-03-22T14:32:01.000+00:00", calendarToXSDDateTimeString(cal));
+	public void testCalendarToXSDDateTimeString_1() throws Exception {
+		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, 0, "Z") ;
+		assertEquals("1984-03-22T14:32:01+00:00", calendarToXSDDateTimeString(cal));
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
-		assertEquals("1984-03-22T07:32:01.000-07:00", calendarToXSDDateTimeString(cal));
+		assertEquals("1984-03-22T07:32:01-07:00", calendarToXSDDateTimeString(cal));
 	}
 
-	@Test
+    @Test
+    public void testCalendarToXSDDateTimeString_2() throws Exception {
+        Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, 50, "Z") ;
+        assertEquals("1984-03-22T14:32:01.050+00:00", calendarToXSDDateTimeString(cal));
+        cal.setTimeZone(TimeZone.getTimeZone("MST"));
+        assertEquals("1984-03-22T07:32:01.050-07:00", calendarToXSDDateTimeString(cal));
+    }
+
+
+    @Test
 	public void testCalendarToXSDDateString() throws Exception {
-		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 23, 59, 1, "Z");
+		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 23, 59, 1, 0, "Z");
 		cal.setTimeZone(TimeZone.getTimeZone("Z")) ;
 		assertEquals("1984-03-22+00:00", calendarToXSDDateString(cal));
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
 		assertEquals("1984-03-22-07:00", calendarToXSDDateString(cal));
 	}
-
-	@Test
-	public void testCalendarToXSDTimeString() throws Exception {
-		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, "GMT+01:00");
-		assertEquals("14:32:01.000+01:00", calendarToXSDTimeString(cal));
+    
+    @Test
+	public void testCalendarToXSDTimeString_1() throws Exception {
+		Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, 0, "GMT+01:00");
+		assertEquals("14:32:01+01:00", calendarToXSDTimeString(cal));
 		// Different timezone - moves the cal point-in-time.
 		cal.setTimeZone(TimeZone.getTimeZone("MST"));
-		assertEquals("06:32:01.000-07:00", calendarToXSDTimeString(cal));
+		assertEquals("06:32:01-07:00", calendarToXSDTimeString(cal));
 	}
 	
+    @Test
+    public void testCalendarToXSDTimeString_2() throws Exception {
+        Calendar cal = createCalendar(1984, Calendar.MARCH, 22, 14, 32, 1, 500, "GMT+01:00");
+        assertEquals("14:32:01.500+01:00", calendarToXSDTimeString(cal));
+        // Different timezone - moves the cal point-in-time.
+        cal.setTimeZone(TimeZone.getTimeZone("MST"));
+        assertEquals("06:32:01.500-07:00", calendarToXSDTimeString(cal));
+    }
+    
 	private static Calendar createCalendar(int year, int month, int dayOfMonth, int hourOfDay,
-	                                       int minute, int second, String tz) {
+	                                       int minute, int second, int milli, String tz) {
 	    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(tz)) ;
 	    cal.set(year, month, dayOfMonth, hourOfDay, minute, second) ;
-	    cal.set(Calendar.MILLISECOND, 0) ;
+	    cal.set(Calendar.MILLISECOND, milli) ;
 	    return cal ;
 	}
 }
\ No newline at end of file