google chrome - How to convert ISO 8601 date/time into milliseconds in XSLT 1.0? -


how can convert iso 8601 2013-08-13t17:57:55z date/time text "milliseconds since epoch" using xslt 1.0? more specifically, google chrome's version of xslt.

extending algorithm julian dates taught me old manager (hi, george!), got in turn collected algorithms of acm (although not appear included in online version of calgo , have not been able find algorithm number, date of publication, or author [but see postscript below]), following template calculates, given iso 8601 timestamp in time zone z, number of milliseconds since beginning of julian day 0, excluding leap seconds. julian day 0 begins @ noon on 1 january 4713 bc in julian calendar; that's noon on 24 november 4714 bce in gregorian calendar.

checking input sanity, adjustment use different epoch, extension handle time zones other z, , extension handle leap seconds , dates before common era left exercise reader. (or call out javascript, where's fun in that?)

<xsl:template name="ts2i">   <!--* timestamp integer:  convert iso 8601 time stamp       * number of milliseconds since epoch.       * our epoch 1 january 4713 bce (julian!),       * julian day 0.       * use 1970-01-01t00:00:00z epoch,       * subtract 210866760000000 ms.       *-->   <xsl:param name="ts"/>   <!--* checking timestamp bad data left       * exercise of reader.  our contract simpler:       * give me correct timestamp in time zone z,        * date on or after 0001-01-01, , i'll       * give correct answer (modulo overflow).       *-->   <!--* yyyy-mm-ddthh:mm:ss.sss...z        * ....|....|....|....|...   |        * 1   5   10   15   20      n       *-->    <!--* parse out c, y, m, d, hh, mm, ss (for century,        * years in current century, months since february,        * days in current month, hours, minutes, seconds).       * y , m values adjusted make        * year begin 1 march (so leap day last       * day of year).       *-->   <xsl:variable name="y0" select="substring($ts,1,4)"/>   <xsl:variable name="m0" select="substring($ts,6,2)"/>   <xsl:variable name="d"  select="substring($ts,9,2)"/>   <xsl:variable name="y1">     <xsl:choose>       <xsl:when test="$m0 &lt; 3"><xsl:value-of select="$y0 - 1"/></xsl:when>       <xsl:otherwise><xsl:value-of select="$y0"/></xsl:otherwise>     </xsl:choose>   </xsl:variable>   <xsl:variable name="m" select="($m0 + 9) mod 12"/>   <xsl:variable name="c" select="floor($y1 div 100)"/>   <xsl:variable name="y" select="($y1 mod 100)"/>   <xsl:variable name="hh" select="substring($ts,12,2)"/>   <xsl:variable name="mm" select="substring($ts,15,2)"/>   <xsl:variable name="s0" select="substring($ts,18)"/>   <xsl:variable name="ss">     <xsl:choose>       <xsl:when test="contains($s0,'z')">         <xsl:value-of select="substring-before($s0,'z')"/>       </xsl:when>       <xsl:otherwise>         <xsl:value-of select="$s0"/>           </xsl:otherwise>     </xsl:choose>   </xsl:variable>   <!--* h holds offset in days between julian day 0       * , beginning of common era.       * j holds offset in ms between midnight ,       * noon, when julian day 0 began.       *-->   <xsl:variable name="h" select="1721119"/>   <xsl:variable name="j" select="43200000"/>    <!--* calculate julian day begins on        * given date.  there 146097 days in each       * 400-year period (including 25 leap days),       * 1461 in each 4-year period, , there       * (($m * 153) + 2) div 5 days in $m months        * elapsed since 1 march.       * straight collected algorithms.       *-->   <xsl:variable name="j" select="floor(($c * 146097) div 4)                                   + floor(($y * 1461) div 4)                                   + floor((($m * 153) + 2) div 5)                                   + $d + $h"/>    <!--* calculate milliseconds since beginning       * of julian day 0.  extension, ,        * have off-by-one error.       *-->   <xsl:value-of select="$j   * 86400000                         + $hh * 3600000                         + $mm *   60000                         + $ss *    1000                         - $j"/> </xsl:template> 

when given input value "2013-08-13t17:57:55z", template given returns numeric value 2.12243176675e+14, i.e. 212243176675000.

as noted in 1 of comments, worried overflow when wrote this, xslt 1.0 numbers ieee doubles, , have (i believe) mantissas of 52 bits. overflow not problem in next several thousand years; milliseconds not subject rounding errors until 15 april 138001 or so.

postscript: little research in acm digital library turned believe must have been source manager george yanos learned algorithm: robert g. tantzen, "algorithm 199: conversions between calendar date , julian day number", communications of acm 6.8 (aug 1963): 444. notice tantzen not explain of magic constants, gives laconic algol code air of mystery.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -