Timestamp from datetime using XSLT

    In life, it so happens that despite all the love for differentiating data and presentation, the day comes when it becomes necessary to transfer part of the logic to the XSLT template.

    In my case, nothing criminal was foreseen on the horizon: it was necessary to calculate the time between two events in a hierarchical XML log. The date and time was stored in a format partially compatible with RFC 3339 .

    This compatibility was ensured by the correct date yyyy-MM-ddand time notation hh:mm:ss.SS, but the following deviations from the standard took place:
    1. Date and time were separated by a space, not a letter T;
    2. The number of digits representing milliseconds could vary from “niode” to “many, many”;
    3. The time zone was not indicated at all.
    At first I wanted to use a ready-made solution with exslt.org - date:difference, but I had to abandon it. The fact is that the difference was required to be obtained with an accuracy of milliseconds, and this algorithm returned a valid xsd:duration(ISO 8601), which does not contain milliseconds. In addition, parsing someone else's output, although formalized, is not very grateful. Thus, digging a bit in exslt, I decided to write the parser myself, in the hope that I could do it quickly ... It

    was decided to assemble the templates in extension with the same namespace as exslt, so that the type of container is appropriate: The extension namespace declared with the help of the extension , will be used by templates, and XML namespace will be used only once to declare the following container:
    1.                 xmlns:date="date"
    2.                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    3.                 extension-element-prefixes="date">
    4.     
    extension-element-prefixesdate:*
    1.     31
    2.     28
    3.     31
    4.     30
    5.     31
    6.     30
    7.     31
    8.     31
    9.     30
    10.     31
    11.     30
    12.     31
    For convenience, getting the number of days by number or month name, we introduce a variable: Now you can write XPath like - the total number of days in a leap year for the fifth month inclusive. In the process of parsing a date-time string, we often had to check the numerical values ​​for coincidence with and, in the case of a positive comparison, replace them with . That would spawn a lot that cluttered the code a lot . Therefore, I started using this look: But this code also does not look neat. After some deliberation, an option was chosen with autoformat fractional numbers: Interestingly, in Opera 10.53, the function

                  select="document('')//date:month"/>

    sum($date:month/*[$i>=position()])+($i>2)$i

    NaN0translate

    translate($expression,'NaN',0)





    format-numbernot able to work with three arguments and generates unknown error, which does not allow the use of named formats of numbers decimal-formatlike this: That is, here is such an XPath drop template:
    1.                     NaN="0">
    format-number($expression,0,'date:NaN')

    Date-time to timestamp

    The following listing is a template for converting date-time to millisecond timetamp: I won’t go into details of calculations, I’ll just talk about its capabilities. @param Accepts the only parameter to which the formatted string is passed. The presence and number of spaces inside the line does not matter - they are all broadcast. Date separators can be any single characters except space. The generalized pattern for the parameter is as follows: - year - month - day - date identifier - hours - minutes - fractional part of a second, may contain an arbitrary number of digits (including none) - UTC timezone identifier
    1.     
    2.     
    3.                   select="
    4.          normalize-space(
    5.              translate($date-time,'TZ ',''))"/>
    6.     
    7.                   select="
    8.          translate(
    9.              substring($compact,1,
    10.                  4+(starts-with($compact,'+') or
    11.                     starts-with($compact,'-'))),
    12.              '+','')"/>
    13.     
    14.                   select="substring-after($compact,$year)"/>
    15.     
    16.                   select="substring($date,7)"/>
    17.     
    18.                   select="format-number(substring($date,2,2)-1,0)"/>
    19.     
    20.         
    21.                       select="
    22.              concat(
    23.                  substring-after($time,'+'),
    24.                  substring-after($time,'-'))"/>
    25.         
    26.     
    27.     


    $date-time



    RFC 3339 date-time pattern

    yyyy
    MM
    dd
    T
    hh
    mm
    S
    Z
    []- the contents of the brackets may or may not be present;
    ()- the contents of the brackets must be present;
    |- or

    @output Returns the number of milliseconds since the beginning of the Unix era 1970-01-01T00: 00: 00Z.

    Timestamp to date-time

    This listing is the inverse conversion from a number to an RFC formatted string. @param Like the previous template, it takes a single parameter. - The number of milliseconds elapsed since 1970-01-01T00: 00: 00Z. If the date is earlier than the beginning of 1970, then the timestamp must be negative. @output A line of the form I checked the patterns, both with negative years and with positive ones - the result is similar to the truth. Tests and source can be picked up at rapidshare . If I don’t like rapid, I’ll post it somewhere else.
    1.     
    2.     
    3.         
    4.                       select="$timestamp div (24*3600000)"/>
    5.         
    6.                       select="
    7.              $timestamp div 1000
    8.             -floor($days)*24*3600"/>
    9.         
    10.                       select="
    11.              1970+floor(
    12.                  format-number($days div 365.24,'0.#'))"/>
    13.         
    14.                       select="
    15.              719528-$year*365
    16.              -floor($year div 4)
    17.              +floor($year div 100)
    18.              -floor($year div 400)
    19.              +floor($days)"/>
    20.         
    21.                       select="
    22.              count($date:month
    23.                    /*[$year-offset>=sum(preceding-sibling::*)][last()]
    24.                    /preceding-sibling::*)"/>
    25.         
    26.                       select="floor($time div 3600)"/>
    27.         
    28.                       select="floor($time div 60-$hours*60)"/>
    29.         
    30.                       select="floor($time -$hours*3600-$min*60)"/>
    31.         
    32.     
    $timestamp

    [-|+]yyyy-MM-ddThh:mm:ss.SSSZ


    Also popular now: