Add support for TODAY constant in Wikidata schemas

This commit is contained in:
Antonin Delpeuch 2019-08-25 13:42:28 +01:00
parent 43980e69dd
commit 80eea8635b
2 changed files with 27 additions and 2 deletions

View File

@ -36,7 +36,6 @@ import java.util.regex.Pattern;
import org.jsoup.helper.Validate;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import com.fasterxml.jackson.annotation.JsonCreator;
@ -106,9 +105,22 @@ public class WbDateConstant implements WbExpression<TimeValue> {
int precision = 0; // default precision (will be overridden if successfully parsed)
int maxLength = 0; // the maximum length parsed
String calendarIri = TimeValue.CM_GREGORIAN_PRO; // Gregorian calendar is assumed by default
String trimmedDatestamp = datestamp.trim();
if("TODAY".equals(trimmedDatestamp)) {
Calendar calendar = Calendar.getInstance();
TimeValue todaysDate = Datamodel.makeTimeValue(
calendar.get(Calendar.YEAR),
(byte)calendar.get(Calendar.MONTH),
(byte)calendar.get(Calendar.DAY_OF_MONTH),
(byte)0, (byte)0, (byte)0, (byte)11, 0,0,0, TimeValue.CM_GREGORIAN_PRO);
return todaysDate;
}
for (Entry<SimpleDateFormat, Integer> entry : acceptedFormats.entrySet()) {
ParsePosition position = new ParsePosition(0);
String trimmedDatestamp = datestamp.trim();
Date date = entry.getKey().parse(trimmedDatestamp, position);
if (date == null) {

View File

@ -23,6 +23,8 @@
******************************************************************************/
package org.openrefine.wikidata.schema;
import java.util.Calendar;
import org.openrefine.wikidata.testing.JacksonSerializationTest;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
@ -86,6 +88,17 @@ public class WbDateConstantTest extends WbExpressionTest<TimeValue> {
evaluatesTo(Datamodel.makeTimeValue(1324, (byte) 2, (byte) 27, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_JULIAN_PRO), julianDay);
}
@Test
public void testToday() {
Calendar calendar = Calendar.getInstance();
TimeValue expectedDate = Datamodel.makeTimeValue(
calendar.get(Calendar.YEAR),
(byte)calendar.get(Calendar.MONTH),
(byte)calendar.get(Calendar.DAY_OF_MONTH),
(byte)0, (byte)0, (byte)0, (byte)11, 0,0,0, TimeValue.CM_GREGORIAN_PRO);
evaluatesTo(expectedDate, new WbDateConstant("TODAY"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testInvalid() {