First pass at parsing ISO dates

pull/14/head
Sam Bosley 12 years ago
parent 1987e9e7d6
commit e7613f0961

@ -5,6 +5,7 @@
*/ */
package com.todoroo.andlib.utility; package com.todoroo.andlib.utility;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
@ -267,4 +268,24 @@ public class DateUtilities {
return date.getTime(); return date.getTime();
} }
public static Date parseIso8601(String iso8601String) throws ParseException {
String formatString;
if (iso8601String.endsWith("Z")) { //$NON-NLS-1$
// Time exists
iso8601String = iso8601String.replace("Z", "+00:00"); //$NON-NLS-1$ //$NON-NLS-2$
try {
iso8601String = iso8601String.substring(0, 22) + iso8601String.substring(23);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
throw new ParseException("Invalid ISO 8601 length", 0); //$NON-NLS-1$
}
formatString = "yyyy-MM-dd'T'HH:mm:ssZ"; //$NON-NLS-1$
} else {
formatString = "yyyy-MM-dd"; //$NON-NLS-1$
}
Date result = new SimpleDateFormat(formatString).parse(iso8601String);
return result;
}
} }

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import java.text.ParseException;
import java.util.Iterator; import java.util.Iterator;
import org.json.JSONException; import org.json.JSONException;
@ -15,11 +16,13 @@ import com.todoroo.andlib.data.Property.IntegerProperty;
import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.SyncFlags; import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.Task;
@SuppressWarnings("nls") @SuppressWarnings("nls")
public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage { public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage {
@ -166,8 +169,15 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
value = 0; value = 0;
else if (property.checkFlag(Property.PROP_FLAG_DATE)) { else if (property.checkFlag(Property.PROP_FLAG_DATE)) {
String valueString = data.getString(key); String valueString = data.getString(key);
// Parse string and setup time value try {
value = value * 1000L; value = DateUtilities.parseIso8601(valueString).getTime();
if (Task.DUE_DATE.equals(property)) {
boolean hasDueTime = valueString.endsWith("Z");
value = Task.createDueDate(hasDueTime ? Task.URGENCY_SPECIFIC_DAY_TIME : Task.URGENCY_SPECIFIC_DAY, value);
}
} catch (ParseException e){
value = 0;
}
} }
model.setValue((LongProperty) property, value); model.setValue((LongProperty) property, value);
} catch (JSONException e) { } catch (JSONException e) {

@ -5,7 +5,10 @@
*/ */
package com.todoroo.andlib.utility; package com.todoroo.andlib.utility;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone;
import com.todoroo.andlib.test.TodorooTestCase; import com.todoroo.andlib.test.TodorooTestCase;
@ -62,4 +65,43 @@ public class DateUtilitiesTest extends TodorooTestCase {
}); });
} }
public void testParseISO8601() {
String withTime = "2013-01-28T13:17:02Z";
Date date = new Date();
Calendar cal = Calendar.getInstance();
try {
date = DateUtilities.parseIso8601(withTime);
cal.setTime(date);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
} catch (ParseException e) {
e.printStackTrace();
fail("Parse exception");
}
assertEquals(2013, cal.get(Calendar.YEAR));
assertEquals(0, cal.get(Calendar.MONTH));
assertEquals(28, cal.get(Calendar.DATE));
assertEquals(13, cal.get(Calendar.HOUR_OF_DAY));
assertEquals(17, cal.get(Calendar.MINUTE));
assertEquals(2, cal.get(Calendar.SECOND));
}
public void testParseISO8601NoTime() {
String withTime = "2013-01-28";
Date date = new Date();
Calendar cal = Calendar.getInstance();
try {
date = DateUtilities.parseIso8601(withTime);
cal.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
fail("Parse exception");
}
assertEquals(2013, cal.get(Calendar.YEAR));
assertEquals(0, cal.get(Calendar.MONTH));
assertEquals(28, cal.get(Calendar.DATE));
assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
assertEquals(0, cal.get(Calendar.MINUTE));
assertEquals(0, cal.get(Calendar.SECOND));
}
} }

Loading…
Cancel
Save