From 109956d2350360fc03ecc5a720e3631376f865cc Mon Sep 17 00:00:00 2001 From: loulan <55950901+loulankxh@users.noreply.github.com> Date: Sat, 24 Apr 2021 21:47:05 +0800 Subject: [PATCH] Support BCE dates in Wikibase upload (#3843) * fixes #3816 --- .../module/scripts/schema-alignment.js | 2 +- .../wikidata/schema/WbDateConstant.java | 12 ++++- .../wikidata/schema/WbDateConstantTest.java | 49 ++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/extensions/wikidata/module/scripts/schema-alignment.js b/extensions/wikidata/module/scripts/schema-alignment.js index 0c106c550..d03f428fd 100644 --- a/extensions/wikidata/module/scripts/schema-alignment.js +++ b/extensions/wikidata/module/scripts/schema-alignment.js @@ -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) { diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbDateConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbDateConstant.java index 86213f14e..c1fd7a328 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbDateConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbDateConstant.java @@ -104,6 +104,7 @@ public class WbDateConstant implements WbExpression { 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 { (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 entry : acceptedFormats.entrySet()) { ParsePosition position = new ParsePosition(0); @@ -155,7 +160,10 @@ public class WbDateConstant implements WbExpression { 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); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbDateConstantTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbDateConstantTest.java index d87ec4ba4..9145be764 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbDateConstantTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbDateConstantTest.java @@ -47,6 +47,21 @@ public class WbDateConstantTest extends WbExpressionTest { 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 { 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();