root / projects / DailyDrillingReport_1.2 / XML / HTMLreport / difference / date.difference.template.xsl @ 189

Revision 130, 15.6 kB (checked in by yaolu, 13 years ago)

Lagt til xsd og xsl filene capgemini har lagt til

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