Fixing bug with timezones in DateTimeWithPrecision. NIHVIVO-2197

This commit is contained in:
bdc34 2011-03-04 22:10:11 +00:00
parent 13ff7686ef
commit 5709e42177
2 changed files with 29 additions and 25 deletions

View file

@ -7,16 +7,21 @@ import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
@ -294,25 +299,15 @@ public class DateTimeWithPrecision extends BaseEditElement {
Integer second = parseToInt(fieldName+"-second", queryParameters);
if( second == null )
second = 0;
int mills = 0;
DateTime value = new DateTime(
year.intValue(),month.intValue(),day.intValue(),
hour.intValue(),minute.intValue(),second.intValue(),mills);
hour.intValue(),minute.intValue(),second.intValue(),0/*millis*/
);
Date dValue = value.toDate();
/*This isn't doing what I want it to do. It is recording the correct instance of timeb
* but it is recording it with the timezone UTC/zulu */
//return ResourceFactory.createTypedLiteral(ISODateTimeFormat.dateTimeNoMillis().print(value),XSDDatatype.XSDdateTime);
Calendar c = Calendar.getInstance();
c.setTime(value.toDate());
Model m = ModelFactory.createDefaultModel();
Literal lit = m.createTypedLiteral( c );
return lit;
return ResourceFactory.createTypedLiteral(
ISODateTimeFormat.dateHourMinuteSecond().print(value), /*does not include timezone*/
XSDDatatype.XSDdateTime);
}
/**

View file

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.TimeZone;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -46,7 +47,14 @@ public class DateTimeWithPrecisionTest {
for( String zoneId : allZones ){
Object v[] = new Object[1];
v[0] = TimeZone.getTimeZone(zoneId);
data.add(v);
try{
DateTimeZone dtz = DateTimeZone.forID(zoneId);
if( dtz != null ){
data.add(v);
}
}catch(IllegalArgumentException ex){
//cannot convert to joda datetimezone.
}
}
return data;
}
@ -105,7 +113,6 @@ public class DateTimeWithPrecisionTest {
precisionURI = dtwp.getSubmittedPrecision( queryParameters);
Assert.assertNotNull(precisionURI);
//Assert.assertEquals(dtwp.PRECISIONS[6], precisionURI);
Assert.assertEquals(VitroVocabulary.Precision.SECOND.uri(), precisionURI);
}
@ -300,13 +307,15 @@ public class DateTimeWithPrecisionTest {
Assert.assertNotNull(date);
Assert.assertEquals( XSDDatatype.XSDdateTime.getURI() ,date.getDatatypeURI() );
DateTime result = new DateTime( date.getLexicalForm() );
DateTime expected = new DateTime(1999,1,1,0,0,0,0);
Assert.assertEquals(expected, result);
Object obj = date.getValue();
Assert.assertNotNull(obj);
Assert.assertEquals(XSDDateTime.class, obj.getClass());
DateTime result = new DateTime( date.getLexicalForm());
DateTime expected = new DateTime(1999,1,1,0,0,0,0 );
Assert.assertEquals(expected.toInstant() , result.toInstant());
Assert.assertEquals("1999-01-01T00:00:00" , date.getLexicalForm() );
}