Untitled

mail@pastecode.io avatar
unknown
xquery
2 years ago
2.3 kB
3
Indexable
Never
xquery version "1.0" encoding "Cp1250";

declare namespace xf = "http://tempuri.org/SYNPAC_GLI/ProxyServices/timeLimiterREQ/";

declare function xf:timeLimiterREQ ($dateTime1 as xs:dateTime, $time1 as xs:time, $dayNum as xs:integer) as xs:boolean {
		let $evenWeek := xf:week-in-year($dateTime1) mod 2
    let $res :=
    	if($dayNum = 3 and $evenWeek) then
        	((xs:time($time1) > xs:time("18:55:00+01:00")) or (xs:time($time1) < xs:time("05:00:00+01:00")))
        else 
        	((xs:time($time1) > xs:time("23:55:00+01:00")) or (xs:time($time1) < xs:time("05:00:00+01:00")))
    return $res
};

declare function xf:week-in-year( $date-time as xs:dateTime ) as xs:integer
{
		let $month-lengths := (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    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 xf:_week-in-year($year, $days + $day + (if ($is-leap and
		$month > 2) then 1 else 0))
};

declare function xf:_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 xf:_week-in-year( $previous-year, 365 + $leap-day )
            else $week-number
};


declare variable $dateTime1 as xs:dateTime external;
declare variable $time1 as xs:time external;
declare variable $dayNum as xs:integer external;

xf:timeLimiterREQ($dateTime1,
    $time1, $dayNum)