Advertisement
cd62131

UnixEpoch to JST LocalTime in XSLT 1.0

Dec 17th, 2018
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 5.01 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet
  3.  version="1.0"
  4.  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5.   <xsl:output method="html"/>
  6.   <xsl:template match="/">
  7.     <xsl:variable name="timestamp" select="1507387355"/>
  8.     <p>
  9.       <xsl:call-template name="epochToJST">
  10.         <xsl:with-param name="epoch" select="$timestamp"/>
  11.      </xsl:call-template>
  12.     </p>
  13.   </xsl:template>
  14.   <xsl:template name="epochToJST">
  15.     <xsl:param name="epoch"/>
  16.     <!-- 9 h = JST time shift -->
  17.     <xsl:variable name="e2" select="$epoch + 9 * 60 * 60"/>
  18.     <xsl:variable name="days" select="floor($e2 div (24 * 60 * 60))"/>
  19.     <xsl:variable name="secs" select="$e2 mod (24 * 60 * 60)"/>
  20.     <xsl:variable name="years" select="floor($days div 365)"/>
  21.     <xsl:variable name="year" select="1970 + $years"/>
  22.     <xsl:variable name="one">
  23.       <xsl:choose>
  24.         <xsl:when test="$year mod 4 = 0">
  25.           <xsl:value-of select="1"/>
  26.         </xsl:when>
  27.         <xsl:otherwise>
  28.           <xsl:value-of select="0"/>
  29.         </xsl:otherwise>
  30.       </xsl:choose>
  31.     </xsl:variable>
  32.     <!-- 1 = first day, (years + 2) / 4 = leap days, one = still not leap day -->
  33.     <xsl:variable name="remainDays" select="1 + $days - $years * 365 - floor(($years + 2) div 4) + $one"/>
  34.     <xsl:variable name="month">
  35.       <xsl:choose>
  36.         <xsl:when test="$remainDays > 334 + $one">
  37.           <xsl:value-of select="12"/>
  38.         </xsl:when>
  39.         <xsl:when test="$remainDays > 304 + $one">
  40.           <xsl:value-of select="11"/>
  41.         </xsl:when>
  42.         <xsl:when test="$remainDays > 273 + $one">
  43.           <xsl:value-of select="10"/>
  44.         </xsl:when>
  45.         <xsl:when test="$remainDays > 243 + $one">
  46.           <xsl:value-of select="9"/>
  47.         </xsl:when>
  48.         <xsl:when test="$remainDays > 212 + $one">
  49.           <xsl:value-of select="8"/>
  50.         </xsl:when>
  51.         <xsl:when test="$remainDays > 181 + $one">
  52.           <xsl:value-of select="7"/>
  53.         </xsl:when>
  54.         <xsl:when test="$remainDays > 151 + $one">
  55.           <xsl:value-of select="6"/>
  56.         </xsl:when>
  57.         <xsl:when test="$remainDays > 120 + $one">
  58.           <xsl:value-of select="5"/>
  59.         </xsl:when>
  60.         <xsl:when test="$remainDays > 90 + $one">
  61.           <xsl:value-of select="4"/>
  62.         </xsl:when>
  63.         <xsl:when test="$remainDays > 59 + $one">
  64.           <xsl:value-of select="3"/>
  65.         </xsl:when>
  66.         <xsl:when test="$remainDays > 31">
  67.           <xsl:value-of select="2"/>
  68.         </xsl:when>
  69.         <xsl:otherwise>
  70.           <xsl:value-of select="1"/>
  71.         </xsl:otherwise>
  72.       </xsl:choose>
  73.     </xsl:variable>
  74.     <xsl:variable name="day">
  75.       <xsl:choose>
  76.         <xsl:when test="$remainDays > 334 + $one">
  77.           <xsl:value-of select="$remainDays - 334 - $one"/>
  78.         </xsl:when>
  79.         <xsl:when test="$remainDays > 304 + $one">
  80.           <xsl:value-of select="$remainDays - 304 - $one"/>
  81.         </xsl:when>
  82.         <xsl:when test="$remainDays > 273 + $one">
  83.           <xsl:value-of select="$remainDays - 273 - $one"/>
  84.         </xsl:when>
  85.         <xsl:when test="$remainDays > 243 + $one">
  86.           <xsl:value-of select="$remainDays - 243 - $one"/>
  87.         </xsl:when>
  88.         <xsl:when test="$remainDays > 212 + $one">
  89.           <xsl:value-of select="$remainDays - 212 - $one"/>
  90.         </xsl:when>
  91.         <xsl:when test="$remainDays > 181 + $one">
  92.           <xsl:value-of select="$remainDays - 181 - $one"/>
  93.         </xsl:when>
  94.         <xsl:when test="$remainDays > 151 + $one">
  95.           <xsl:value-of select="$remainDays - 151 - $one"/>
  96.         </xsl:when>
  97.         <xsl:when test="$remainDays > 120 + $one">
  98.           <xsl:value-of select="$remainDays - 120 - $one"/>
  99.         </xsl:when>
  100.         <xsl:when test="$remainDays > 90 + $one">
  101.           <xsl:value-of select="$remainDays - 90 - $one"/>
  102.         </xsl:when>
  103.         <xsl:when test="$remainDays > 59 + $one">
  104.           <xsl:value-of select="$remainDays - 59 - $one"/>
  105.         </xsl:when>
  106.         <xsl:when test="$remainDays > 31">
  107.           <xsl:value-of select="$remainDays - 31"/>
  108.         </xsl:when>
  109.         <xsl:otherwise>
  110.           <xsl:value-of name="day" select="$remainDays"/>
  111.         </xsl:otherwise>
  112.       </xsl:choose>
  113.     </xsl:variable>
  114.     <xsl:variable name="hour" select="floor($secs div (60 * 60))"/>
  115.     <xsl:variable name="remainSecs" select="$secs mod (60 * 60)"/>
  116.     <xsl:variable name="minute" select="floor($remainSecs div 60)"/>
  117.     <xsl:variable name="sec" select="$remainSecs mod 60"/>
  118.     <xsl:value-of select="$year"/><xsl:text>-</xsl:text>
  119.     <xsl:value-of select="format-number($month, '00')"/><xsl:text>-</xsl:text>
  120.     <xsl:value-of select="format-number($day, '00')"/><xsl:text>T</xsl:text>
  121.     <xsl:value-of select="format-number($hour, '00')"/><xsl:text>:</xsl:text>
  122.     <xsl:value-of select="format-number($minute, '00')"/><xsl:text>:</xsl:text>
  123.     <xsl:value-of select="format-number($sec, '00')"/><xsl:text>+09:00</xsl:text>
  124.   </xsl:template>
  125. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement