-
Christian Schaper authoredChristian Schaper authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
date.xqm 12.78 KiB
xquery version "3.0" encoding "UTF-8";
(:~
: XQuery module for processing dates
~:)
module namespace date="http://xquery.weber-gesamtausgabe.de/modules/date";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace mei="http://www.music-encoding.org/ns/mei";
declare namespace pdr="http://pdr.bbaw.de/namespaces/pdrws/";
import module namespace datetime="http://exist-db.org/xquery/datetime" at "java:org.exist.xquery.modules.datetime.DateTimeModule";
import module namespace functx="http://www.functx.com";
declare variable $date:DATE_FORMAT_ERROR := QName("http://xquery.weber-gesamtausgabe.de/modules/date", "DateFormatError");
(:~
: Construct one normalized xs:date from a tei:date element's date or duration attributes (@from, @to, @when, @notBefore, @notAfter)
:
: @author Christian Epp
: @author Peter Stadler
: @param $date the tei:date
: @param $latest a boolean whether the constructed date shall be the latest or earliest possible
: @return the constructed date as xs:date or empty
:)
declare function date:getOneNormalizedDate($date as element()?, $latest as xs:boolean) as xs:date? {
if($latest) then max($date/@* ! date:getCastableDate(., $latest))
else min($date/@* ! date:getCastableDate(., $latest))
};
(:~
: Checks, if given $date is castable as xs:date and returns this date.
: If $date is castable as xs:gYear the first or the last day of the year (depending on $latest) will be returned
: (Helper function for date:getOneNormalizedDate() and date:printDate())
:
: @author Christian Epp
: @author Peter Stadler
: @param $date the date to test as xs:string
: @param $latest if $latest is set to true() the last day of the year will be returned
: @return the (constructed) date as xs:date, empty() if no conversion is possible
:)
declare %private function date:getCastableDate($date as xs:string, $latest as xs:boolean) as xs:date? {
if($date castable as xs:date) then xs:date($date)
else if($date castable as xs:dateTime) then
if(starts-with($date, '-')) then xs:date(substring($date, 1, 11))
else xs:date(substring($date, 1, 10))
else if($date castable as xs:gYear) then
if($latest) then xs:date(concat($date,'-12-31'))
else xs:date(concat($date,'-01-01'))
else if($date castable as xs:gYearMonth) then
if($latest) then xs:date(concat($date, functx:days-in-month(xs:date(concat($date,'-01')))))
else xs:date(concat($date,'-01'))
else()
};
(:~
: format year specification depending on positive or negative value
:
: @author Peter Stadler
: @param $year the year as (positive or negative) integer
: @param $lang the language switch (en|de)
: @return xs:string
:)
declare function date:formatYear($year as xs:int, $lang as xs:string) as xs:string {
if($year gt 0) then $year cast as xs:string
else if($lang eq 'en') then concat($year*-1,' BC')
else concat($year*-1,' v. Chr.')
};