Last active
January 22, 2020 21:22
-
-
Save adamretter/fcd8c99cf977ff31ad83dc37c4e49783 to your computer and use it in GitHub Desktop.
Date/Time/DateTime XQuery functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import module namespace functx = "http://www.functx.com"; | |
(: NOTE -- functx uses 1 to 7 to represent MON-SUN, whereas eXist-db's datetime module used 1 to 7 to represent SUN-SAT :) | |
declare variable $local:MON := 1; | |
declare variable $local:TUES := 2; | |
declare variable $local:WEDS := 3; | |
declare variable $local:THURS := 4; | |
declare variable $local:FRI := 5; | |
declare variable $local:SAT := 6; | |
declare variable $local:SUN := 7; | |
declare function local:count-day-in-month($weekday as xs:integer, $date as xs:date) as xs:integer { | |
let $month := fn:month-from-date($date) | |
let $first := functx:date(fn:year-from-date($date), $month, 1) | |
let $first-week-days := ($first, (1 to 6) ! ($first + xs:dayTimeDuration("P" || . || "D"))) | |
let $first-match := $first-week-days[functx:day-of-week(.) eq $weekday] | |
let $search-space := ($first-match, (1 to 4) ! ($first-match + xs:dayTimeDuration("P" || . * 7 || "D"))) | |
return | |
count($search-space[fn:month-from-date(.) eq $month]) | |
}; | |
(: local:count-day-in-month($local:FRI, xs:date("2008-02-24")) :) | |
declare function local:date-from-dateTime($date-time as xs:dateTime) as xs:date { | |
$date-time cast as xs:date | |
}; | |
(: local:date-from-dateTime(fn:current-dateTime()) :) | |
declare function local:time-from-dateTime($date-time as xs:dateTime) as xs:time { | |
$date-time cast as xs:time | |
}; | |
(: local:time-from-dateTime(fn:current-dateTime()) :) | |
declare function local:date-range($start-date as xs:date, $increment as xs:duration, $iterations as xs:integer) as xs:date* { | |
if ($iterations eq 0) | |
then | |
() | |
else | |
(1 to $iterations) ! ($start-date + ($increment * .)) | |
}; | |
(: local:date-range(fn:current-date(), xs:dayTimeDuration("P1D"), $iterations) :) | |
declare function local:dateTime-range($start-date-time as xs:dateTime, $increment as xs:duration, $iterations as xs:integer) as xs:dateTime* { | |
if ($iterations eq 0) | |
then | |
() | |
else | |
(1 to $iterations) ! ($start-date-time + ($increment * .)) | |
}; | |
(: local:dateTime-range(fn:current-dateTime(), xs:dayTimeDuration("P1D"), $iterations) :) | |
declare function local:time-range($start-time as xs:time, $increment as xs:dayTimeDuration, $iterations as xs:integer) as xs:time* { | |
if ($iterations eq 0) | |
then | |
() | |
else | |
(1 to $iterations) ! ($start-time + ($increment * .)) | |
}; | |
(: local:time-range(fn:current-time(), xs:dayTimeDuration("P1D"), $iterations) :) | |
declare function local:timestamp() as xs:unsignedLong { | |
(fn:current-dateTime() - xs:dateTime("1970-01-01T00:00:00-00:00")) div xs:dayTimeDuration('PT0.001S') | |
}; | |
(: local:timestamp() :) | |
declare function local:dateTime-from-timestamp($timestamp as xs:unsignedLong) as xs:dateTime { | |
xs:dateTime("1970-01-01T00:00:00-00:00") + xs:dayTimeDuration("PT" || $timestamp div 1000 || "S") | |
}; | |
(: | |
local:dateTime-from-timestamp(local:timestamp()) | |
:) | |
declare function local:date-for($year as xs:integer, $month as xs:integer, $week as xs:integer, $weekday as xs:integer) as xs:date { | |
let $first := functx:date($year, $month, 1) | |
let $in-week-date := $first + (($week - 1) * xs:dayTimeDuration("P7D")) | |
return | |
if(functx:day-of-week($in-week-date) > $weekday) | |
then | |
$in-week-date - xs:dayTimeDuration("P" || (functx:day-of-week($in-week-date) - $weekday) || "D") | |
else | |
$in-week-date + xs:dayTimeDuration("P" || ($weekday - functx:day-of-week($in-week-date)) || "D") | |
}; | |
(: | |
local:date-for(2019, 3, 3, 5) | |
:) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ is missing for what should be variable $iterations at lines 41, 52, and 63