[336] | 1 | <?xml version="1.0"?> |
---|
| 2 | <xsl:stylesheet version="1.0" |
---|
| 3 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
---|
| 4 | xmlns:date="http://exslt.org/dates-and-times" |
---|
| 5 | xmlns:func="http://exslt.org/functions" |
---|
| 6 | extension-element-prefixes="date func"> |
---|
| 7 | |
---|
| 8 | <date:month-lengths> |
---|
| 9 | <date:month>31</date:month> |
---|
| 10 | <date:month>28</date:month> |
---|
| 11 | <date:month>31</date:month> |
---|
| 12 | <date:month>30</date:month> |
---|
| 13 | <date:month>31</date:month> |
---|
| 14 | <date:month>30</date:month> |
---|
| 15 | <date:month>31</date:month> |
---|
| 16 | <date:month>31</date:month> |
---|
| 17 | <date:month>30</date:month> |
---|
| 18 | <date:month>31</date:month> |
---|
| 19 | <date:month>30</date:month> |
---|
| 20 | <date:month>31</date:month> |
---|
| 21 | </date:month-lengths> |
---|
| 22 | |
---|
| 23 | <func:function name="date:difference"> |
---|
| 24 | <xsl:param name="start" /> |
---|
| 25 | <xsl:param name="end" /> |
---|
| 26 | |
---|
| 27 | <xsl:variable name="start-neg" select="starts-with($start, '-')" /> |
---|
| 28 | <xsl:variable name="start-no-neg"> |
---|
| 29 | <xsl:choose> |
---|
| 30 | <xsl:when test="$start-neg or starts-with($start, '+')"> |
---|
| 31 | <xsl:value-of select="substring($start, 2)" /> |
---|
| 32 | </xsl:when> |
---|
| 33 | <xsl:otherwise> |
---|
| 34 | <xsl:value-of select="$start" /> |
---|
| 35 | </xsl:otherwise> |
---|
| 36 | </xsl:choose> |
---|
| 37 | </xsl:variable> |
---|
| 38 | <xsl:variable name="start-no-neg-length" select="string-length($start-no-neg)" /> |
---|
| 39 | <xsl:variable name="start-timezone"> |
---|
| 40 | <xsl:choose> |
---|
| 41 | <xsl:when test="substring($start-no-neg, $start-no-neg-length) = 'Z'">Z</xsl:when> |
---|
| 42 | <xsl:otherwise> |
---|
| 43 | <xsl:variable name="tz" select="substring($start-no-neg, $start-no-neg-length - 5)" /> |
---|
| 44 | <xsl:if test="(substring($tz, 1, 1) = '-' or |
---|
| 45 | substring($tz, 1, 1) = '+') and |
---|
| 46 | substring($tz, 4, 1) = ':'"> |
---|
| 47 | <xsl:value-of select="$tz" /> |
---|
| 48 | </xsl:if> |
---|
| 49 | </xsl:otherwise> |
---|
| 50 | </xsl:choose> |
---|
| 51 | </xsl:variable> |
---|
| 52 | <xsl:variable name="end-neg" select="starts-with($end, '-')" /> |
---|
| 53 | <xsl:variable name="end-no-neg"> |
---|
| 54 | <xsl:choose> |
---|
| 55 | <xsl:when test="$end-neg or starts-with($end, '+')"> |
---|
| 56 | <xsl:value-of select="substring($end, 2)" /> |
---|
| 57 | </xsl:when> |
---|
| 58 | <xsl:otherwise> |
---|
| 59 | <xsl:value-of select="$end" /> |
---|
| 60 | </xsl:otherwise> |
---|
| 61 | </xsl:choose> |
---|
| 62 | </xsl:variable> |
---|
| 63 | <xsl:variable name="end-no-neg-length" select="string-length($end-no-neg)" /> |
---|
| 64 | <xsl:variable name="end-timezone"> |
---|
| 65 | <xsl:choose> |
---|
| 66 | <xsl:when test="substring($end-no-neg, $end-no-neg-length) = 'Z'">Z</xsl:when> |
---|
| 67 | <xsl:otherwise> |
---|
| 68 | <xsl:variable name="tz" select="substring($end-no-neg, $end-no-neg-length - 5)" /> |
---|
| 69 | <xsl:if test="(substring($tz, 1, 1) = '-' or |
---|
| 70 | substring($tz, 1, 1) = '+') and |
---|
| 71 | substring($tz, 4, 1) = ':'"> |
---|
| 72 | <xsl:value-of select="$tz" /> |
---|
| 73 | </xsl:if> |
---|
| 74 | </xsl:otherwise> |
---|
| 75 | </xsl:choose> |
---|
| 76 | </xsl:variable> |
---|
| 77 | |
---|
| 78 | <xsl:variable name="difference"> |
---|
| 79 | <xsl:if test="(not(string($start-timezone)) or |
---|
| 80 | $start-timezone = 'Z' or |
---|
| 81 | (substring($start-timezone, 2, 2) <= 23 and |
---|
| 82 | substring($start-timezone, 5, 2) <= 59)) and |
---|
| 83 | (not(string($end-timezone)) or |
---|
| 84 | $end-timezone = 'Z' or |
---|
| 85 | (substring($end-timezone, 2, 2) <= 23 and |
---|
| 86 | substring($end-timezone, 5, 2) <= 59))"> |
---|
| 87 | <xsl:variable name="start-dt" select="substring($start-no-neg, 1, $start-no-neg-length - string-length($start-timezone))" /> |
---|
| 88 | <xsl:variable name="start-dt-length" select="string-length($start-dt)" /> |
---|
| 89 | <xsl:variable name="end-dt" select="substring($end-no-neg, 1, $end-no-neg-length - string-length($end-timezone))" /> |
---|
| 90 | <xsl:variable name="end-dt-length" select="string-length($end-dt)" /> |
---|
| 91 | |
---|
| 92 | <xsl:variable name="start-year" select="substring($start-dt, 1, 4) * (($start-neg * -2) + 1)" /> |
---|
| 93 | <xsl:variable name="end-year" select="substring($end-dt, 1, 4) * (($end-neg * -2) + 1)" /> |
---|
| 94 | <xsl:variable name="diff-year" select="$end-year - $start-year" /> |
---|
| 95 | <xsl:choose> |
---|
| 96 | <xsl:when test="not(number($start-year) and number($end-year))" /> |
---|
| 97 | <xsl:when test="$start-dt-length = 4 or $end-dt-length = 4"> |
---|
| 98 | <xsl:choose> |
---|
| 99 | <xsl:when test="$diff-year < 0">-P<xsl:value-of select="$diff-year * -1" />Y</xsl:when> |
---|
| 100 | <xsl:otherwise>P<xsl:value-of select="$diff-year" />Y</xsl:otherwise> |
---|
| 101 | </xsl:choose> |
---|
| 102 | </xsl:when> |
---|
| 103 | <xsl:when test="substring($start-dt, 5, 1) = '-' and |
---|
| 104 | substring($end-dt, 5, 1) = '-'"> |
---|
| 105 | <xsl:variable name="start-month" select="substring($start-dt, 6, 2)" /> |
---|
| 106 | <xsl:variable name="end-month" select="substring($end-dt, 6, 2)" /> |
---|
| 107 | <xsl:variable name="diff-month" select="$end-month - $start-month" /> |
---|
| 108 | <xsl:choose> |
---|
| 109 | <xsl:when test="not($start-month <= 12 and $end-month <= 12)" /> |
---|
| 110 | <xsl:when test="$start-dt-length = 7 or $end-dt-length = 7"> |
---|
| 111 | <xsl:variable name="months" select="$diff-month + ($diff-year * 12)" /> |
---|
| 112 | <xsl:variable name="abs-months" select="$months * ((($months >= 0) * 2) - 1)" /> |
---|
| 113 | <xsl:variable name="y" select="floor($abs-months div 12)" /> |
---|
| 114 | <xsl:variable name="m" select="$abs-months mod 12" /> |
---|
| 115 | <xsl:if test="$months < 0">-</xsl:if> |
---|
| 116 | <xsl:text>P</xsl:text> |
---|
| 117 | <xsl:if test="$y"><xsl:value-of select="$y" />Y</xsl:if> |
---|
| 118 | <xsl:if test="$m"><xsl:value-of select="$m" />M</xsl:if> |
---|
| 119 | </xsl:when> |
---|
| 120 | <xsl:when test="substring($start-dt, 8, 1) = '-' and |
---|
| 121 | substring($end-dt, 8, 1) = '-'"> |
---|
| 122 | <xsl:variable name="start-day" select="substring($start-dt, 9, 2)" /> |
---|
| 123 | <xsl:variable name="end-day" select="substring($end-dt, 9, 2)" /> |
---|
| 124 | <xsl:if test="$start-day <= 31 and $end-day <= 31"> |
---|
| 125 | <xsl:variable name="month-lengths" select="document('')/*/date:month-lengths/date:month" /> |
---|
| 126 | <xsl:variable name="start-y-1" select="$start-year - 1" /> |
---|
| 127 | <xsl:variable name="start-leaps" |
---|
| 128 | select="floor($start-y-1 div 4) - |
---|
| 129 | floor($start-y-1 div 100) + |
---|
| 130 | floor($start-y-1 div 400)" /> |
---|
| 131 | <xsl:variable name="start-leap" select="(not($start-year mod 4) and $start-year mod 100) or not($start-year mod 400)" /> |
---|
| 132 | <xsl:variable name="start-month-days" |
---|
| 133 | select="sum($month-lengths[position() < $start-month])" /> |
---|
| 134 | <xsl:variable name="start-days"> |
---|
| 135 | <xsl:variable name="days" |
---|
| 136 | select="($start-year * 365) + $start-leaps + |
---|
| 137 | $start-month-days + $start-day" /> |
---|
| 138 | <xsl:choose> |
---|
| 139 | <xsl:when test="$start-leap"> |
---|
| 140 | <xsl:value-of select="$days + 1" /> |
---|
| 141 | </xsl:when> |
---|
| 142 | <xsl:otherwise> |
---|
| 143 | <xsl:value-of select="$days" /> |
---|
| 144 | </xsl:otherwise> |
---|
| 145 | </xsl:choose> |
---|
| 146 | </xsl:variable> |
---|
| 147 | <xsl:variable name="end-y-1" select="$end-year - 1" /> |
---|
| 148 | <xsl:variable name="end-leaps" |
---|
| 149 | select="floor($end-y-1 div 4) - |
---|
| 150 | floor($end-y-1 div 100) + |
---|
| 151 | floor($end-y-1 div 400)" /> |
---|
| 152 | <xsl:variable name="end-leap" select="(not($end-year mod 4) and $end-year mod 100) or not($end-year mod 400)" /> |
---|
| 153 | <xsl:variable name="end-month-days" |
---|
| 154 | select="sum($month-lengths[position() < $end-month])" /> |
---|
| 155 | <xsl:variable name="end-days"> |
---|
| 156 | <xsl:variable name="days" |
---|
| 157 | select="($end-year * 365) + $end-leaps + |
---|
| 158 | $end-month-days + $end-day" /> |
---|
| 159 | <xsl:choose> |
---|
| 160 | <xsl:when test="$end-leap"> |
---|
| 161 | <xsl:value-of select="$days + 1" /> |
---|
| 162 | </xsl:when> |
---|
| 163 | <xsl:otherwise> |
---|
| 164 | <xsl:value-of select="$days" /> |
---|
| 165 | </xsl:otherwise> |
---|
| 166 | </xsl:choose> |
---|
| 167 | </xsl:variable> |
---|
| 168 | <xsl:variable name="diff-days" select="$end-days - $start-days" /> |
---|
| 169 | <xsl:choose> |
---|
| 170 | <xsl:when test="$start-dt-length = 10 or $end-dt-length = 10"> |
---|
| 171 | <xsl:choose> |
---|
| 172 | <xsl:when test="$diff-days < 0">-P<xsl:value-of select="$diff-days * -1" />D</xsl:when> |
---|
| 173 | <xsl:otherwise>P<xsl:value-of select="$diff-days" />D</xsl:otherwise> |
---|
| 174 | </xsl:choose> |
---|
| 175 | </xsl:when> |
---|
| 176 | <xsl:when test="substring($start-dt, 11, 1) = 'T' and |
---|
| 177 | substring($end-dt, 11, 1) = 'T' and |
---|
| 178 | substring($start-dt, 14, 1) = ':' and |
---|
| 179 | substring($start-dt, 17, 1) = ':' and |
---|
| 180 | substring($end-dt, 14, 1) = ':' and |
---|
| 181 | substring($end-dt, 17, 1) = ':'"> |
---|
| 182 | <xsl:variable name="start-hour" select="substring($start-dt, 12, 2)" /> |
---|
| 183 | <xsl:variable name="start-min" select="substring($start-dt, 15, 2)" /> |
---|
| 184 | <xsl:variable name="start-sec" select="substring($start-dt, 18)" /> |
---|
| 185 | <xsl:variable name="end-hour" select="substring($end-dt, 12, 2)" /> |
---|
| 186 | <xsl:variable name="end-min" select="substring($end-dt, 15, 2)" /> |
---|
| 187 | <xsl:variable name="end-sec" select="substring($end-dt, 18)" /> |
---|
| 188 | <xsl:if test="$start-hour <= 23 and $end-hour <= 23 and |
---|
| 189 | $start-min <= 59 and $end-min <= 59 and |
---|
| 190 | $start-sec <= 60 and $end-sec <= 60"> |
---|
| 191 | <xsl:variable name="min-s" select="60" /> |
---|
| 192 | <xsl:variable name="hour-s" select="60 * 60" /> |
---|
| 193 | <xsl:variable name="day-s" select="60 * 60 * 24" /> |
---|
| 194 | |
---|
| 195 | <xsl:variable name="start-tz-adj"> |
---|
| 196 | <xsl:variable name="tz" |
---|
| 197 | select="(substring($start-timezone, 2, 2) * $hour-s) + |
---|
| 198 | (substring($start-timezone, 5, 2) * $min-s)" /> |
---|
| 199 | <xsl:choose> |
---|
| 200 | <xsl:when test="starts-with($start-timezone, '-')"> |
---|
| 201 | <xsl:value-of select="$tz" /> |
---|
| 202 | </xsl:when> |
---|
| 203 | <xsl:when test="starts-with($start-timezone, '+')"> |
---|
| 204 | <xsl:value-of select="$tz * -1" /> |
---|
| 205 | </xsl:when> |
---|
| 206 | <xsl:otherwise>0</xsl:otherwise> |
---|
| 207 | </xsl:choose> |
---|
| 208 | </xsl:variable> |
---|
| 209 | <xsl:variable name="end-tz-adj"> |
---|
| 210 | <xsl:variable name="tz" |
---|
| 211 | select="(substring($end-timezone, 2, 2) * $hour-s) + |
---|
| 212 | (substring($end-timezone, 5, 2) * $min-s)" /> |
---|
| 213 | <xsl:choose> |
---|
| 214 | <xsl:when test="starts-with($end-timezone, '-')"> |
---|
| 215 | <xsl:value-of select="$tz" /> |
---|
| 216 | </xsl:when> |
---|
| 217 | <xsl:when test="starts-with($end-timezone, '+')"> |
---|
| 218 | <xsl:value-of select="$tz * -1" /> |
---|
| 219 | </xsl:when> |
---|
| 220 | <xsl:otherwise>0</xsl:otherwise> |
---|
| 221 | </xsl:choose> |
---|
| 222 | </xsl:variable> |
---|
| 223 | |
---|
| 224 | <xsl:variable name="start-secs" select="$start-sec + ($start-min * $min-s) + ($start-hour * $hour-s) + ($start-days * $day-s) + $start-tz-adj" /> |
---|
| 225 | <xsl:variable name="end-secs" select="$end-sec + ($end-min * $min-s) + ($end-hour * $hour-s) + ($end-days * $day-s) + $end-tz-adj" /> |
---|
| 226 | <xsl:variable name="diff-secs" select="$end-secs - $start-secs" /> |
---|
| 227 | <xsl:variable name="s" select="$diff-secs * ((($diff-secs < 0) * -2) + 1)" /> |
---|
| 228 | <xsl:variable name="days" select="floor($s div $day-s)" /> |
---|
| 229 | <xsl:variable name="hours" select="floor(($s - ($days * $day-s)) div $hour-s)" /> |
---|
| 230 | <xsl:variable name="mins" select="floor(($s - ($days * $day-s) - ($hours * $hour-s)) div $min-s)" /> |
---|
| 231 | <xsl:variable name="secs" select="$s - ($days * $day-s) - ($hours * $hour-s) - ($mins * $min-s)" /> |
---|
| 232 | <xsl:if test="$diff-secs < 0">-</xsl:if> |
---|
| 233 | <xsl:text>P</xsl:text> |
---|
| 234 | <xsl:if test="$days"> |
---|
| 235 | <xsl:value-of select="$days" /> |
---|
| 236 | <xsl:text>D</xsl:text> |
---|
| 237 | </xsl:if> |
---|
| 238 | <xsl:if test="$hours or $mins or $secs">T</xsl:if> |
---|
| 239 | <xsl:if test="$hours"> |
---|
| 240 | <xsl:value-of select="$hours" /> |
---|
| 241 | <xsl:text>H</xsl:text> |
---|
| 242 | </xsl:if> |
---|
| 243 | <xsl:if test="$mins"> |
---|
| 244 | <xsl:value-of select="$mins" /> |
---|
| 245 | <xsl:text>M</xsl:text> |
---|
| 246 | </xsl:if> |
---|
| 247 | <xsl:if test="$secs"> |
---|
| 248 | <xsl:value-of select="$secs" /> |
---|
| 249 | <xsl:text>S</xsl:text> |
---|
| 250 | </xsl:if> |
---|
| 251 | </xsl:if> |
---|
| 252 | </xsl:when> |
---|
| 253 | </xsl:choose> |
---|
| 254 | </xsl:if> |
---|
| 255 | </xsl:when> |
---|
| 256 | </xsl:choose> |
---|
| 257 | </xsl:when> |
---|
| 258 | </xsl:choose> |
---|
| 259 | </xsl:if> |
---|
| 260 | </xsl:variable> |
---|
| 261 | <func:result select="$difference" /> |
---|
| 262 | </func:function> |
---|
| 263 | |
---|
| 264 | </xsl:stylesheet> |
---|