use of org.wikidata.wdtk.datamodel.interfaces.TimeValue in project OpenRefine by OpenRefine.
the class WbDateConstant method parse.
/**
* Parses a timestamp into a Wikibase {@link TimeValue}. The precision is
* automatically inferred from the format.
*
* @param datestamp
* the time to parse
* @return
* @throws ParseException
* if the time cannot be parsed
*/
public static TimeValue parse(String datestamp) throws ParseException {
Date bestDate = null;
// default precision (will be overridden if successfully parsed)
int precision = 0;
// the maximum length parsed
int maxLength = 0;
// judge whether this is a BCE year
boolean bceFlag = false;
// Gregorian calendar is assumed by default
String calendarIri = TimeValue.CM_GREGORIAN_PRO;
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) + 1), (byte) calendar.get(Calendar.DAY_OF_MONTH), (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
return todaysDate;
}
if (trimmedDatestamp.startsWith("-")) {
trimmedDatestamp = trimmedDatestamp.substring(1);
bceFlag = true;
}
for (Entry<SimpleDateFormat, Integer> entry : acceptedFormats.entrySet()) {
ParsePosition position = new ParsePosition(0);
Date date = entry.getKey().parse(trimmedDatestamp, position);
if (date == null) {
continue;
}
// Potentially parse the calendar Qid after the date
int consumedUntil = position.getIndex();
if (consumedUntil < trimmedDatestamp.length()) {
Matcher matcher = calendarSuffixPattern.matcher(trimmedDatestamp.subSequence(position.getIndex(), trimmedDatestamp.length()));
if (matcher.find()) {
String calendarQid = matcher.group(1);
calendarIri = Datamodel.SITE_WIKIDATA + calendarQid;
consumedUntil = trimmedDatestamp.length();
}
}
// Ignore parses which failed or do not consume all the input
if (date != null && position.getIndex() > maxLength && // only allow to partially consume the input if the precision is day and followed by a T (as in ISO)
(consumedUntil == trimmedDatestamp.length() || (entry.getValue() == 11 && trimmedDatestamp.charAt(consumedUntil) == 'T'))) {
precision = entry.getValue();
bestDate = date;
maxLength = position.getIndex();
}
}
if (bestDate == null || precision == 0) {
throw new ParseException("Invalid date.", 0);
} else {
Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
calendar.setTime(bestDate);
long year = calendar.get(Calendar.YEAR);
int month = precision < 10 ? 0 : calendar.get(Calendar.MONTH) + 1;
int day_of_month = precision < 11 ? 0 : calendar.get(Calendar.DAY_OF_MONTH);
if (bceFlag)
year = -1 * year;
return Datamodel.makeTimeValue(year, (byte) month, (byte) day_of_month, (byte) calendar.get(Calendar.HOUR_OF_DAY), (byte) calendar.get(Calendar.MINUTE), (byte) calendar.get(Calendar.SECOND), (byte) precision, 0, 0, 0, calendarIri);
}
}
use of org.wikidata.wdtk.datamodel.interfaces.TimeValue in project OpenRefine by OpenRefine.
the class DifferenceWithinScrutinizerTest method testTrigger.
@Test
public void testTrigger() {
ItemIdValue idA = TestingData.existingId;
TimeValue lowerYear = new TimeValueImpl(1800, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
Statement statement1 = new StatementImpl("P569", value1, idA);
Statement statement2 = new StatementImpl("P570", value2, idA);
TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
setFetcher(fetcher);
scrutinize(updateA);
assertWarningsRaised(DifferenceWithinRangeScrutinizer.type);
}
use of org.wikidata.wdtk.datamodel.interfaces.TimeValue in project OpenRefine by OpenRefine.
the class DifferenceWithinScrutinizerTest method testNoIssue.
@Test
public void testNoIssue() {
ItemIdValue idA = TestingData.existingId;
TimeValue lowerYear = new TimeValueImpl(2000, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
Statement statement1 = new StatementImpl("P569", value1, idA);
Statement statement2 = new StatementImpl("P570", value2, idA);
TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
setFetcher(fetcher);
scrutinize(updateA);
assertNoWarningRaised();
}
use of org.wikidata.wdtk.datamodel.interfaces.TimeValue in project OpenRefine by OpenRefine.
the class WbDateConstantTest method testToday.
@Test
public void testToday() {
Calendar calendar = Calendar.getInstance();
TimeValue expectedDate = Datamodel.makeTimeValue(calendar.get(Calendar.YEAR), (byte) (calendar.get(Calendar.MONTH) + 1), (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"));
}
use of org.wikidata.wdtk.datamodel.interfaces.TimeValue in project OpenRefine by OpenRefine.
the class LaxValueMatcherTests method testTimeValue.
@Test
public void testTimeValue() {
String calModel = "https://foo.com/calendars/Q123";
TimeValue day1 = Datamodel.makeTimeValue(1987, (byte) 3, (byte) 8, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_DAY, 0, 0, 0, calModel);
TimeValue day2 = Datamodel.makeTimeValue(1987, (byte) 11, (byte) 10, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_DAY, 0, 0, 0, calModel);
TimeValue day3 = Datamodel.makeTimeValue(1987, (byte) 3, (byte) 8, (byte) 10, (byte) 11, (byte) 12, TimeValue.PREC_DAY, 0, 0, 0, calModel);
TimeValue year1 = Datamodel.makeTimeValue(1987, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, TimeValue.PREC_YEAR, 0, 0, 0, calModel);
TimeValue year2 = Datamodel.makeTimeValue(1987, (byte) 5, (byte) 6, (byte) 7, (byte) 0, (byte) 0, TimeValue.PREC_YEAR, 0, 0, 0, calModel);
assertTrue(SUT.match(day1, day1));
assertFalse(SUT.match(day1, day2));
assertFalse(SUT.match(day2, day1));
assertTrue(SUT.match(day1, day3));
assertTrue(SUT.match(day3, day1));
assertTrue(SUT.match(year1, year2));
assertTrue(SUT.match(year2, year1));
}
Aggregations