Этот старый пост от Jakob Fix может вам помочь: http://x -query.com / pipermail / talk / 2010-November / 003298.html
xquery version "1.0";
declare namespace exslt = "http://exslt.org/dates-and-times";
(: for those implementations without exslt extensions :)
declare namespace date = "http://noexslt.org/dates-and-times";
declare variable $date-time := "2010-01-02T00:00:00Z";
declare variable $month-lengths := (0, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31);
(:
: returns the week of the year as a number.
:)
declare function date:week-in-year( $date-time as xs:dateTime ) as xs:integer
{
let $year := fn:year-from-dateTime( $date-time )
let $day := fn:day-from-dateTime( $date-time )
let $month := fn:month-from-dateTime( $date-time )
let $days := sum( subsequence( $month-lengths, 1, $month ) )
let $is-leap := ($year mod 4 = 0 and $year mod 100 != 0) or $year
mod 400 = 0
return date:_week-in-year($year, $days + $day + (if ($is-leap and
$month > 2) then 1 else 0))
};
declare function date:_week-in-year( $year as xs:integer, $month-days
as xs:integer) as xs:integer
{
let $previous-year := $year - 1
let $is-leap := ($year mod 4 = 0 and $year mod 100 != 0) or $year
mod 400 = 0
let $dow := ($previous-year + floor($previous-year div 4) -
floor($previous-year div 100) + floor($previous-year div 400) +
$month-days) mod 7
let $day-of-week := if ($dow > 0) then $dow else 7
let $start-day := ($month-days - $day-of-week + 7) mod 7
let $week-number := floor(($month-days - $day-of-week + 7) div 7)
cast as xs:integer
return
if ($start-day >= 4) then $week-number + 1
else if ($week-number = 0) then
let $leap-day := if ((not($previous-year mod 4) and
$previous-year mod 100) or not($previous-year mod 400)) then 1 else 0
return date:_week-in-year( $previous-year, 365 + $leap-day )
else $week-number
};
exslt:week-in-year( $date-time ),
date:week-in-year( $date-time cast as xs:dateTime )