Fixing bug with timezones in DateTimeWithPrecision. NIHVIVO-2197
This commit is contained in:
parent
13ff7686ef
commit
5709e42177
2 changed files with 29 additions and 25 deletions
|
@ -7,16 +7,21 @@ import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.TimeZone;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.joda.time.DateTime;
|
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.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
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.controller.freemarker.FreemarkerHttpServlet;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||||
|
@ -294,25 +299,15 @@ public class DateTimeWithPrecision extends BaseEditElement {
|
||||||
Integer second = parseToInt(fieldName+"-second", queryParameters);
|
Integer second = parseToInt(fieldName+"-second", queryParameters);
|
||||||
if( second == null )
|
if( second == null )
|
||||||
second = 0;
|
second = 0;
|
||||||
int mills = 0;
|
|
||||||
|
|
||||||
|
|
||||||
DateTime value = new DateTime(
|
DateTime value = new DateTime(
|
||||||
year.intValue(),month.intValue(),day.intValue(),
|
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();
|
return ResourceFactory.createTypedLiteral(
|
||||||
|
ISODateTimeFormat.dateHourMinuteSecond().print(value), /*does not include timezone*/
|
||||||
/*This isn't doing what I want it to do. It is recording the correct instance of timeb
|
XSDDatatype.XSDdateTime);
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Map;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
import org.joda.time.DateTimeZone;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -46,8 +47,15 @@ public class DateTimeWithPrecisionTest {
|
||||||
for( String zoneId : allZones ){
|
for( String zoneId : allZones ){
|
||||||
Object v[] = new Object[1];
|
Object v[] = new Object[1];
|
||||||
v[0] = TimeZone.getTimeZone(zoneId);
|
v[0] = TimeZone.getTimeZone(zoneId);
|
||||||
|
try{
|
||||||
|
DateTimeZone dtz = DateTimeZone.forID(zoneId);
|
||||||
|
if( dtz != null ){
|
||||||
data.add(v);
|
data.add(v);
|
||||||
}
|
}
|
||||||
|
}catch(IllegalArgumentException ex){
|
||||||
|
//cannot convert to joda datetimezone.
|
||||||
|
}
|
||||||
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +113,6 @@ public class DateTimeWithPrecisionTest {
|
||||||
precisionURI = dtwp.getSubmittedPrecision( queryParameters);
|
precisionURI = dtwp.getSubmittedPrecision( queryParameters);
|
||||||
|
|
||||||
Assert.assertNotNull(precisionURI);
|
Assert.assertNotNull(precisionURI);
|
||||||
//Assert.assertEquals(dtwp.PRECISIONS[6], precisionURI);
|
|
||||||
Assert.assertEquals(VitroVocabulary.Precision.SECOND.uri(), precisionURI);
|
Assert.assertEquals(VitroVocabulary.Precision.SECOND.uri(), precisionURI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,13 +307,15 @@ public class DateTimeWithPrecisionTest {
|
||||||
Assert.assertNotNull(date);
|
Assert.assertNotNull(date);
|
||||||
Assert.assertEquals( XSDDatatype.XSDdateTime.getURI() ,date.getDatatypeURI() );
|
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();
|
Object obj = date.getValue();
|
||||||
Assert.assertNotNull(obj);
|
Assert.assertNotNull(obj);
|
||||||
Assert.assertEquals(XSDDateTime.class, obj.getClass());
|
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() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue