query-3f94aae14bf98ec631e357235ea5f132

rq turtle/ttl

value used for days calculation is changed to 31 December if the time precision is "year" (9):(P582)end time : In the following modified query the Bouzinac@

Use at

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?item ?itemLabel ?start ?t1 ?end ?t2 ?startprecision ?endprecision ?precision ?duration ?tp ?conv ?converted 
{
    {  SELECT *
       WHERE
       {
          #  VALUES ?item { wd:Q2139412 wd:Q585291 wd:Q840250 wd:Q815122 }
           ?item wdt:P31 wd:Q188055 .
           ?item p:P580/psv:P580 [ wikibase:timeValue ?start ; wikibase:timePrecision ?startprecision ] .
           ?item p:P582/psv:P582 [ wikibase:timeValue ?end ; wikibase:timePrecision ?endprecision ]
       }
       LIMIT 200
    }
    #determine lowest precision
    BIND( IF( ?startprecision > ?endprecision , ?endprecision , ?startprecision ) as ?precision) 
    #change dates to lowest precision (here: only year precision)
    BIND( IF( ?startprecision < 10, xsd:dateTime( CONCAT( str(YEAR(?start)),"-01-01T00:00:00Z")), ?start)  as ?t1) 
    BIND( IF( ?endprecision < 10, xsd:dateTime( CONCAT( str(YEAR(?end)),"-12-31T00:00:00Z")), ?end)  as ?t2) 
    #calc duration on adjusted dates (in days)
    BIND( ?t2 - ?t1 as ?duration)                  
    #get unit corresponding to precision and conversion factor from days
    ?tp wdt:P2803 ?pr . FILTER( str(?pr) = str(?precision) )
    OPTIONAL { ?tp p:P2442 / psv:P2442 [ wikibase:quantityAmount ?conv ; wikibase:quantityUnit wd:Q573 ] .
             ?tp rdfs:label ?unit . FILTER(lang(?unit) = "en") 
             } 
    # convert from days to unit corresponding to precision, add "ca." and unit label
    BIND( CONCAT( "ca. ", str(ROUND(FLOOR((1 / ?conv * ?duration) +1))), " ", ?unit ) as ?converted) 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v13("?conv"):::projected v14("?converted"):::projected v11("?duration"):::projected v7("?end"):::projected v8("?endprecision"):::projected v4("?item"):::projected v1("?pr") v9("?precision"):::projected v5("?start"):::projected v6("?startprecision"):::projected v9("?t1"):::projected v10("?t2"):::projected v12("?tp"):::projected v3("?unit") a2((" ")) a1((" ")) a4((" ")) a3((" ")) a6((" ")) a5((" ")) c1(["en"]):::literal c18(["bd:serviceParam"]):::iri c3(["wd:Q188055"]):::iri c13(["wd:Q573"]):::iri f0[["str(?pr) = str(?precision)"]] f0 --> v1 f0 --> v9 v4 --"p:direct/P31"--> c3 a1 --"wikibase:timeValue"--> v5 a1 --"wikibase:timePrecision"--> v6 v4 --"p:P580"--> a2 a2 --"p:statement/value/P580"--> a1 a3 --"wikibase:timeValue"--> v7 a3 --"wikibase:timePrecision"--> v8 v4 --"p:P582"--> a4 a4 --"p:statement/value/P582"--> a3 bind1[/"if(?startprecision > ?endprecision,?endprecision,?startprecision)"/] v6 --o bind1 v8 --o bind1 bind1 --as--o v9 bind2[/"if(?startprecision < '10^^xsd:integer',http://www.w3.org/2001/XMLSchema#dateTime(concat(str(year-from-dateTime(?start)),'-01-01T00:00:00Z')),?start)"/] v6 --o bind2 v5 --o bind2 bind2 --as--o v9 bind3[/"if(?endprecision < '10^^xsd:integer',http://www.w3.org/2001/XMLSchema#dateTime(concat(str(year-from-dateTime(?end)),'-12-31T00:00:00Z')),?end)"/] v8 --o bind3 v7 --o bind3 bind3 --as--o v10 bind4[/"?t2 - ?t1"/] v10 --o bind4 v9 --o bind4 bind4 --as--o v11 v12 --"p:direct/P2803"--> v1 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; a5 -."wikibase:quantityAmount".-> v13 a5 --"wikibase:quantityUnit"--> c13 v12 --"p:P2442"--> a6 a6 --"p:statement/value/P2442"--> a5 v12 --"rdfs:label"--> v3 end bind5[/"concat('ca. ',str(numeric-round(numeric-floor('1^^xsd:integer' / ?conv * ?duration + '+1^^xsd:integer'))),' ',?unit)"/] v13 --o bind5 v11 --o bind5 v3 --o bind5 bind5 --as--o v14 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c18 --"wikibase:language"--> c1 end