'Should xsd:date convert for datetimes before common era in SPARQL

Consider this SPARQL where datetime is a date in 36 BC:

SELECT ?date ?year WHERE {
  wd:Q1180731 wdt:P577 ?datetime .
  BIND(xsd:date(?datetime) AS ?date)
  # BIND(YEAR(?datetime) AS ?year)
}

The xsd:date(?datetime) function fails on the Wikidata Query Service Blazegraph instance with java.lang.IllegalArgumentException: -036-01-01T00:00:00Z.

Is it suppose to work? The line with YEAR does not complain (when uncommented).

I have come up with this variation instead:

SELECT ?date WHERE {
  wd:Q1180731 wdt:P577 ?datetime .
  BIND(REPLACE(STR(?datetime), 'T.*', '') AS ?date)
}

In this case the result is -036-01-01. The sortability of such BC dates may not be good. Do we have other methods/functions to make (string?) dates before year 1000 (including BC dates) sortable?



Solution 1:[1]

ISO 8601 dates are expected to have at least four digits for the year (and it is not unknown for implementations to be unwilling to deal with more than four digits). That means that the literal string for your date should probably be '-0036-01-01', not '-036-01-01'.

Some implementations are unwilling to contemplate BCE dates; this counts as a resource limitation similar in kind to being unwilling to contemplate strings of more than 10^10^10^10^10 bytes, so for what it's worth this is a quality of implementation issue, not a conformance issue.

Note that if you are using BCE dates at all seriously, you will want to be using XSD 1.1, in which the literal '-0036-01-01' denotes a day in (Gregorian) 37 BCE, not 36 BCE. (Year 0000 is 1 BCE, -0001 is 2 BCE, etc.) As far as anyone in the WG knew, the primary use of Gregorian dates for the period before the Christian era is among astronomers, and since this off-by-one usage is theirs (introduced by Cassini to simplify date calculations), it is really not a good idea to use -0036 to denote 36 BCE.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TallTed