Using XSLT 1.0 & receive dates in the following format:
20111129060804
and I have a need to be able to calculate differences between two of them. However, I cannot use any extensions.
I also have other limitations as well:
- I am limited as to the namespaces that may be used inside an XSLT,
- and where I am using the xsl stylesheet (a 3rd party application) it will not properly process any include/import actions within the xsl.
So in effect, the xslt that I need to use must be pretty much self-inclusive, in that whatever is called or referenced must be in the xslt itself.
I had given thought to taking the date & time & converting it to a Julian serial number & handling the date math much the way that Excel does. Example -
09/14/2011 08:19:37 = 40800.346956018500000
09/15/2011 12:22:46 = 40801.515810185200000
difference 1 day 4 hours 3 minutes and 9 seconds
difference = 1.168854166666280
where everything left of the decimal is in Days, and everything right of the decimal is in time (i.e. .168854166666280 * 24 = 4.0524999999990680, or 4 hours and .0525 percent of an hour, or 3.15 minutes).
Has anyone ever done this type of elapsed date-time math in XSLT 1.0 without the use of extensions?
Would appreciate any thoughts, suggestions or references if there's an existing set of code I can re-purpose.
EDIT -
I decided to proceed with the above, and have hit a snag on conversion of TIME.
The example I am using is: Start Time = 08:19:37 End Time = 15:58:33
which is a duration/difference of 7:38:56 (BTW, this is only for same-day calculation - I have it to throw an error if Endtime < Start Time or if the Day is > 0).
To arrive at the time, I convert everything to seconds & then do the math. 08:19:37 converts to 29977 (8 * 3600 for hours + 19 * 60 for minutes + seconds). 15:58:33 converts to 57513 (15 * 3600 for hours + 58 *60 for minutes + seconds).
The difference is 27536 seconds.
I get: 27536/3600 =7.6488888888888888888 <-- the integer part is hours then the difference of 27536 - 25200 = 2336 <-- the 25200 = 7 hours * 3600 seconds 2336/60 = 38.93333333333333333 <-- the integer part is 38 minutes then the difference of 2336 - 2280 = 56 seconds from above.
However, if I try to grab the integer portion of these numbers, its subject to rounding.
<xsl:template name="time-difference">
<xsl:param name="from-hour"/>
<xsl:param name="from-minute"/>
<xsl:param name="from-second"/>
<xsl:param name="to-hour"/>
<xsl:param name="to-minute"/>
<xsl:param name="to-second"/>
<xsl:variable name="f-secs" select="($from-hour * 3600) + ($from-minute * 60) + ($from-second)"/>
<xsl:variable name="t-secs" select="($to-hour * 3600) + ($to-minute * 60) + ($to- second)"/>
<xs开发者_C百科l:variable name="sec-diff" select="$t-secs - $f-secs"/>
<xsl:variable name="daysec-diff" select="format-number(($sec-diff div 86400),'#')"/>
<xsl:variable name="q" select="($sec-diff div 3600)"/>
<xsl:variable name="t-hrs-diff" select="format-number(($sec-diff div 3600),'00')"/>
The variable q above has 7.648888888888888888 (which is correct) however, the t-hrs-diff once the format-number happens now has "08" in it, making my time incorrect.
I know any other formatting attempt to grab the integer portion of the number would result in rounding.
Outside of the substring-before & substring-after - which would (I think) make me have to convert the result to a number before using in the next calculation - is there ANY other way to grab the unaltered integer portion of the number & not have to do conversion from a string to number to use the pieces?
Try using floor() to truncate your values.
floor(7.64) = 7
精彩评论