Support BCE dates in Wikibase upload (#3843)

* fixes #3816
This commit is contained in:
loulan 2021-04-24 21:47:05 +08:00 committed by GitHub
parent ee72a200e1
commit 109956d235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 4 deletions

View File

@ -1021,7 +1021,7 @@ SchemaAlignment._initField = function(inputContainer, mode, initialValue, change
changedCallback();
});
SchemaAlignment.setupStringInputValidation(input, /^((\d{4}(-[0-1]\d(-[0-3]\d)?)?)|TODAY)$/);
SchemaAlignment.setupStringInputValidation(input, /^(([\-]?\d{4}(-[0-1]\d(-[0-3]\d)?)?)|TODAY)$/);
} else if (mode === "globe-coordinate") {
input.attr("placeholder", "lat,lon");
var propagateValue = function(val) {

View File

@ -104,6 +104,7 @@ public class WbDateConstant implements WbExpression<TimeValue> {
Date bestDate = null;
int precision = 0; // default precision (will be overridden if successfully parsed)
int maxLength = 0; // the maximum length parsed
boolean bceFlag = false; // judge whether this is a BCE year
String calendarIri = TimeValue.CM_GREGORIAN_PRO; // Gregorian calendar is assumed by default
String trimmedDatestamp = datestamp.trim();
@ -117,7 +118,11 @@ public class WbDateConstant implements WbExpression<TimeValue> {
(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);
@ -155,7 +160,10 @@ public class WbDateConstant implements WbExpression<TimeValue> {
Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
calendar.setTime(bestDate);
return Datamodel.makeTimeValue(calendar.get(Calendar.YEAR), (byte) (calendar.get(Calendar.MONTH) + 1),
long year = calendar.get(Calendar.YEAR);
if(bceFlag)
year = -1*year;
return Datamodel.makeTimeValue(year, (byte) (calendar.get(Calendar.MONTH) + 1),
(byte) calendar.get(Calendar.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);

View File

@ -47,6 +47,21 @@ public class WbDateConstantTest extends WbExpressionTest<TimeValue> {
private WbDateConstant julianYear = new WbDateConstant("1324_Q1985786");
private WbDateConstant julianDecade = new WbDateConstant("1320D_Q1985786");
private WbDateConstant BCEmillenium = new WbDateConstant("-1001M");
private WbDateConstant BCEcentury = new WbDateConstant("-1701C");
private WbDateConstant BCEdecade = new WbDateConstant("-1990D");
private WbDateConstant BCEyear = new WbDateConstant("-2018");
private WbDateConstant BCEmonth = new WbDateConstant("-2018-02");
private WbDateConstant BCEday = new WbDateConstant("-2018-02-27");
private WbDateConstant BCEwhitespace = new WbDateConstant(" -2018-02-27 ");
private WbDateConstant BCEsecond = new WbDateConstant("-2017-01-03T04:12:45");
private WbDateConstant BCEsecondz = new WbDateConstant("-2017-01-03T04:12:45Z");
private WbDateConstant BCEjulianDay = new WbDateConstant("-1324-02-27_Q1985786");
private WbDateConstant BCEjulianMonth = new WbDateConstant("-1324-02_Q1985786");
private WbDateConstant BCEjulianYear = new WbDateConstant("-1324_Q1985786");
private WbDateConstant BCEjulianDecade = new WbDateConstant("-1320D_Q1985786");
@Test
public void testSerialize() {
@ -88,7 +103,39 @@ 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 testEvaluateBCE() {
evaluatesTo(Datamodel.makeTimeValue(-1001, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 6, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEmillenium);
evaluatesTo(Datamodel.makeTimeValue(-1701, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 7, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEcentury);
evaluatesTo(Datamodel.makeTimeValue(-1990, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 8, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEdecade);
evaluatesTo(Datamodel.makeTimeValue(-2018, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 9, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEyear);
evaluatesTo(Datamodel.makeTimeValue(-2018, (byte) 2, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 10, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEmonth);
evaluatesTo(Datamodel.makeTimeValue(-2018, (byte) 2, (byte) 27, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEday);
evaluatesTo(Datamodel.makeTimeValue(-2017, (byte) 1, (byte) 3, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEsecond);
evaluatesTo(Datamodel.makeTimeValue(-2017, (byte) 1, (byte) 3, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEsecondz);
evaluatesTo(Datamodel.makeTimeValue(-2018, (byte) 2, (byte) 27, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_GREGORIAN_PRO), BCEwhitespace);
evaluatesTo(Datamodel.makeTimeValue(-1320, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 8, 0, 0, 0,
TimeValue.CM_JULIAN_PRO), BCEjulianDecade);
evaluatesTo(Datamodel.makeTimeValue(-1324, (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 9, 0, 0, 0,
TimeValue.CM_JULIAN_PRO), BCEjulianYear);
evaluatesTo(Datamodel.makeTimeValue(-1324, (byte) 2, (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 10, 0, 0, 0,
TimeValue.CM_JULIAN_PRO), BCEjulianMonth);
evaluatesTo(Datamodel.makeTimeValue(-1324, (byte) 2, (byte) 27, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0,
TimeValue.CM_JULIAN_PRO), BCEjulianDay);
}
@Test
public void testToday() {
Calendar calendar = Calendar.getInstance();