Author: ngn
Date: Sat Sep 12 21:00:18 2009
New Revision: 814235

URL: http://svn.apache.org/viewvc?rev=814235&view=rev
Log:
Add support for parsing date times

Added:
    
mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/
    
mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/DateTimeProfileTestCase.java
Modified:
    
mina/sandbox/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/datetime/DateTimeProfile.java

Modified: 
mina/sandbox/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/datetime/DateTimeProfile.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/datetime/DateTimeProfile.java?rev=814235&r1=814234&r2=814235&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/datetime/DateTimeProfile.java
 (original)
+++ 
mina/sandbox/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/datetime/DateTimeProfile.java
 Sat Sep 12 21:00:18 2009
@@ -4,8 +4,13 @@
 import static 
org.apache.vysper.compliance.SpecCompliant.ComplianceCoverage.COMPLETE;
 import static 
org.apache.vysper.compliance.SpecCompliant.ComplianceStatus.IN_PROGRESS;
 
+import java.util.Calendar;
 import java.util.TimeZone;
 import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.text.DateFormat;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 
 /**
@@ -20,6 +25,11 @@
     protected static final SimpleDateFormat utcDateTimeFormatter;
     protected static final SimpleDateFormat utcTimeFormatter;
 
+    protected static final SimpleDateFormat utcDateParser;
+    protected static final SimpleDateFormat utcDateTimeParser;
+    protected static final SimpleDateFormat utcTimeParser;
+
+    
     static {
         TIME_ZONE_UTC = TimeZone.getTimeZone("UTC");
         utcDateTimeFormatter = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
@@ -28,6 +38,10 @@
         utcDateFormatter.setTimeZone(TIME_ZONE_UTC); // convert to UTC
         utcTimeFormatter = new SimpleDateFormat("HH:mm:ss'Z'");
         utcTimeFormatter.setTimeZone(TIME_ZONE_UTC); // convert to UTC
+
+        utcDateTimeParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+        utcDateParser = new SimpleDateFormat("yyyy-MM-dd");
+        utcTimeParser = new SimpleDateFormat("HH:mm:ss");
     }
     
     private final static DateTimeProfile SINGLETON = new DateTimeProfile();
@@ -52,4 +66,58 @@
         return utcTimeFormatter.format(time);
     }
 
+    public Date fromDateTime(String time) throws ParseException {
+        return parseWithTz(utcDateTimeParser, time, false);
+    }
+
+    public Date fromTime(String time) throws ParseException {
+        return parseWithTz(utcTimeParser, time, true);
+    }
+
+    public Date fromDate(String time) throws ParseException {
+        return utcDateParser.parse(time);
+    }
+
+    
+    private Date parseWithTz(DateFormat format, String time, boolean 
optionalTz) throws ParseException {
+        int tzOffset;
+        String timeWithoutTz;
+        // tz is required for datetimes and optional for times by XEP-0082
+        if(time.endsWith("Z")) {
+            timeWithoutTz = time.substring(0, time.length() - 1);
+            tzOffset = 0;
+        } else {
+            Pattern tzPattern = Pattern.compile("([+-])(\\d\\d):(\\d\\d)$");
+            
+            Matcher matcher = tzPattern.matcher(time);
+            
+            if(matcher.find()) {
+                timeWithoutTz = time.substring(0, time.length() - 6);
+                
+                String sign = matcher.group(1);
+                int hours = Integer.parseInt(matcher.group(2));
+                int min = Integer.parseInt(matcher.group(3));
+                
+                tzOffset = hours * 60 + min;
+                if(sign.equals("-")) tzOffset = -tzOffset;
+            } else {
+                if(optionalTz) {
+                    timeWithoutTz = time;
+                    tzOffset = 0;
+                } else {
+                    throw new IllegalArgumentException("Time zone required for 
date time: " + time);
+                }
+            }
+        }
+        
+        // parse without time zone
+        Date actual = format.parse(timeWithoutTz);
+        
+        // correct for time zone
+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        cal.setTime(actual);
+        cal.add(Calendar.MINUTE, tzOffset);
+        return cal.getTime();
+        
+    }
 }

Added: 
mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/DateTimeProfileTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/DateTimeProfileTestCase.java?rev=814235&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/DateTimeProfileTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/datetime/DateTimeProfileTestCase.java
 Sat Sep 12 21:00:18 2009
@@ -0,0 +1,105 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.vysper.xmpp.datetime;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+...@suppresswarnings("deprecation")
+public class DateTimeProfileTestCase extends TestCase {
+    
+    private DateTimeProfile dt = DateTimeProfile.getInstance();
+
+    public void testFormatDateTime() throws Exception {
+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        cal.set(2009, 8, 11, 11, 12, 13);
+
+        String actual = dt.getDateTimeInUTC(cal.getTime());
+        assertEquals("2009-09-11T11:12:13Z", actual);
+    }
+
+    public void testFormatDate() throws Exception {
+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        cal.set(2009, 8, 11);
+
+        String actual = dt.getDateInUTC(cal.getTime());
+        assertEquals("2009-09-11", actual);
+    }
+
+    public void testFormatTime() throws Exception {
+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+        cal.set(Calendar.HOUR_OF_DAY, 11);
+        cal.set(Calendar.MINUTE, 12);
+        cal.set(Calendar.SECOND, 13);
+        
+        String actual = dt.getTimeInUTC(cal.getTime());
+        assertEquals("11:12:13Z", actual);
+    }
+
+    
+    public void testParseDateTimeWithTz() throws Exception {
+        Date actual = dt.fromDateTime("2009-09-11T11:12:13-01:30");
+        Date expected = new Date(109, 8, 11, 9, 42, 13);
+        assertEquals(expected, actual);
+    }
+
+    public void testParseDateTimeWithUTCTz() throws Exception {
+        Date actual = dt.fromDateTime("2009-09-11T11:12:13Z");
+        Date expected = new Date(109, 8, 11, 11, 12, 13);
+        assertEquals(expected, actual);
+    }
+
+    public void testParseDateTimeWithoutTz() throws Exception {
+        try {
+            dt.fromDateTime("2009-09-11T11:12:13");
+            fail("Must throw IllegalArgumentException");
+        } catch(IllegalArgumentException e) {
+            // OK
+        }
+    }
+
+    public void testParseTimeWithTz() throws Exception {
+        Date actual = dt.fromTime("11:12:13-01:30");
+        Date expected = new Date(70, 0, 1, 9, 42, 13);
+        assertEquals(expected, actual);
+    }
+
+    public void testParseTimeWithUTCTz() throws Exception {
+        Date actual = dt.fromTime("11:12:13Z");
+        Date expected = new Date(70, 0, 1, 11, 12, 13);
+        assertEquals(expected, actual);
+    }
+
+    public void testParseTimeWithoutTz() throws Exception {
+        Date actual = dt.fromTime("11:12:13");
+        Date expected = new Date(70, 0, 1, 11, 12, 13);
+        assertEquals(expected, actual);
+    }
+
+    public void testParseDate() throws Exception {
+        Date actual = dt.fromDate("2009-09-11");
+        Date expected = new Date(109, 8, 11);
+        assertEquals(expected, actual);
+    }
+
+}


Reply via email to