0

Apologies in advanced for the large block of code.

I am able to output a timestamp with the code below. I would now like it to output in an EPOCH format. I cant seem to get anything to work looking at other submitted tickets. I am admittedly a bit of noob to XSL. I am confined to XSL1

Any help is appreciated. Thank you in advanced.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:dt="urn:schemas-microsoft-com:datatypes"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:output method="html" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:param name="MessageMergeTime"></xsl:param>
<xsl:param name="MessageID"></xsl:param>
<xsl:template match="/">

  <xsl:variable name="Mergetime" select="$MessageMergeTime"></xsl:variable>
  <xsl:variable name="duration" select="'P0'"></xsl:variable>
   <xsl:variable name="format" select="'yyyyMMdd'"></xsl:variable>
   <xsl:variable name="format-time" select="''"></xsl:variable>
    <xsl:variable name="locale" select="'en'"></xsl:variable>
    <xsl:variable name="eom" select="'0'"></xsl:variable>

    <xsl:variable name="datetoAdd" select="substring(translate($Mergetime,' ','T'),1,19)"></xsl:variable>

<xsl:call-template name="date:add">
      <xsl:with-param name="date-time" select="$datetoAdd" />
      <xsl:with-param name="duration" select="$duration" />
      <xsl:with-param name="format" select="$format" />
      <xsl:with-param name="format-time" select="$format-time" />
      <xsl:with-param name="locale" select="$locale" />
      <xsl:with-param name="eom" select="$eom" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="date:add">
    <xsl:param name="date-time" />
    <xsl:param name="duration" />
    <xsl:param name="format" select="'yyyy-MM-dd'" />
    <xsl:param name="format-time" select="''" />
    <xsl:param name="locale" select="fr" />
    <xsl:param name="eom" />

    <xsl:variable name="dt-neg" select="starts-with($date-time, '-')" />
    <xsl:variable name="dt-no-neg">
      <xsl:choose>
        <xsl:when test="$dt-neg or starts-with($date-time, '+')">
          <xsl:value-of select="substring($date-time, 2)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$date-time" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="dt-no-neg-length" select="string-length($dt-no-neg)" />
    <xsl:variable name="timezone">
      <xsl:choose>
        <xsl:when test="substring($dt-no-neg, $dt-no-neg-length) = 'Z'">Z</xsl:when>
        <xsl:otherwise>
          <xsl:variable name="tz" select="substring($dt-no-neg, $dt-no-neg-length - 5)" />
          <xsl:if test="(substring($tz, 1, 1) = '-' or 
                           substring($tz, 1, 1) = '+') and
                          substring($tz, 4, 1) = ':'">
            <xsl:value-of select="$tz" />
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="new-dt">
      <xsl:if test="not(string($timezone)) or
                    $timezone = 'Z' or 
                    (substring($timezone, 2, 2) &lt;= 23 and
                     substring($timezone, 5, 2) &lt;= 59)">
        <xsl:variable name="dt" select="substring($dt-no-neg, 1, $dt-no-neg-length - string-length($timezone))" />
        <xsl:variable name="dt-length" select="string-length($dt)" />
        <xsl:variable name="du-neg" select="starts-with($duration, '-')" />
        <xsl:variable name="du">
          <xsl:choose>
            <xsl:when test="$du-neg">
              <xsl:value-of select="substring($duration, 2)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$duration" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:if test="starts-with($du, 'P') and
                       not(translate($du, '0123456789PYMDTHS.', ''))">
          <xsl:variable name="du-date">
            <xsl:choose>
              <xsl:when test="contains($du, 'T')">
                <xsl:value-of select="substring-before(substring($du, 2), 'T')" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="substring($du, 2)" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:variable name="du-time">
            <xsl:if test="contains($du, 'T')">
              <xsl:value-of select="substring-after($du, 'T')" />
            </xsl:if>
          </xsl:variable>
          <xsl:if test="(not($du-date) or
                           (not(translate($du-date, '0123456789YMD', '')) and
                            not(substring-after($du-date, 'D')) and
                            (contains($du-date, 'D') or 
                             (not(substring-after($du-date, 'M')) and
                              (contains($du-date, 'M') or
                               not(substring-after($du-date, 'Y'))))))) and
                          (not($du-time) or
                           (not(translate($du-time, '0123456789HMS.', '')) and
                            not(substring-after($du-time, 'S')) and
                            (contains($du-time, 'S') or
                             not(substring-after($du-time, 'M')) and
                             (contains($du-time, 'M') or
                              not(substring-after($du-time, 'H'))))))">
            <xsl:variable name="duy-str">
              <xsl:choose>
                <xsl:when test="contains($du-date, 'Y')">
                  <xsl:value-of select="substring-before($du-date, 'Y')" />
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="dum-str">
              <xsl:choose>
                <xsl:when test="contains($du-date, 'M')">
                  <xsl:choose>
                    <xsl:when test="contains($du-date, 'Y')">
                      <xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'M')" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-before($du-date, 'M')" />
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="dud-str">
              <xsl:choose>
                <xsl:when test="contains($du-date, 'D')">
                  <xsl:choose>
                    <xsl:when test="contains($du-date, 'M')">
                      <xsl:value-of select="substring-before(substring-after($du-date, 'M'), 'D')" />
                    </xsl:when>
                    <xsl:when test="contains($du-date, 'Y')">
                      <xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'D')" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-before($du-date, 'D')" />
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="duh-str">
              <xsl:choose>
                <xsl:when test="contains($du-time, 'H')">
                  <xsl:value-of select="substring-before($du-time, 'H')" />
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="dumin-str">
              <xsl:choose>
                <xsl:when test="contains($du-time, 'M')">
                  <xsl:choose>
                    <xsl:when test="contains($du-time, 'H')">
                      <xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'M')" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-before($du-time, 'M')" />
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="dus-str">
              <xsl:choose>
                <xsl:when test="contains($du-time, 'S')">
                  <xsl:choose>
                    <xsl:when test="contains($du-time, 'M')">
                      <xsl:value-of select="substring-before(substring-after($du-time, 'M'), 'S')" />
                    </xsl:when>
                    <xsl:when test="contains($du-time, 'H')">
                      <xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'S')" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-before($du-time, 'S')" />
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:variable name="mult" select="($du-neg * -2) + 1" />
            <xsl:variable name="duy" select="$duy-str * $mult" />
            <xsl:variable name="dum" select="$dum-str * $mult" />
            <xsl:variable name="dud" select="$dud-str * $mult" />
            <xsl:variable name="duh" select="$duh-str * $mult" />
            <xsl:variable name="dumin" select="$dumin-str * $mult" />
            <xsl:variable name="dus" select="$dus-str * $mult" />

            <xsl:variable name="year" select="substring($dt, 1, 4) * (($dt-neg * -2) + 1)" />
            <xsl:choose>
              <xsl:when test="$year and
                                  string($duy) = 'NaN' or 
                                  string($dum) = 'NaN' or 
                                  string($dud) = 'NaN' or 
                                  string($duh) = 'NaN' or 
                                  string($dumin) = 'NaN' or 
                                  string($dus) = 'NaN'" />
              <xsl:when test="$dt-length > 4 or
                                  $dum or $dud or $duh or $dumin or $dus">
                <xsl:variable name="month">
                  <xsl:choose>
                    <xsl:when test="$dt-length > 4">
                      <xsl:if test="substring($dt, 5, 1) = '-'">
                        <xsl:value-of select="substring($dt, 6, 2)" />
                      </xsl:if>
                    </xsl:when>
                    <xsl:otherwise>1</xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:choose>
                  <xsl:when test="not($month) or $month > 12" />
                  <xsl:when test="$dt-length > 7 or
                                        $dud or $duh or $dumin or $dus">
                    <xsl:variable name="day">
                      <xsl:choose>
                        <xsl:when test="$dt-length > 7">
                          <xsl:if test="substring($dt, 8, 1) = '-'">
                            <xsl:value-of select="substring($dt, 9, 2)" />
                          </xsl:if>
                        </xsl:when>
                        <xsl:otherwise>1</xsl:otherwise>
                      </xsl:choose>
                    </xsl:variable>
                    <xsl:choose>
                      <xsl:when test="not($day) or $day > 31" />
                      <xsl:when test="$dt-length > 10 or
                                              $duh or $dumin or $dus">
                        <xsl:if test="$dt-length = 10 or
                                               (substring($dt, 11, 1) = 'T' and
                                                substring($dt, 14, 1) = ':' and
                                                substring($dt, 17, 1) = ':')">
                          <xsl:variable name="hour">
                            <xsl:choose>
                              <xsl:when test="$dt-length > 10">
                                <xsl:value-of select="substring($dt, 12, 2)" />
                              </xsl:when>
                              <xsl:otherwise>0</xsl:otherwise>
                            </xsl:choose>
                          </xsl:variable>
                          <xsl:variable name="minute">
                            <xsl:choose>
                              <xsl:when test="$dt-length > 10">
                                <xsl:value-of select="substring($dt, 15, 2)" />
                              </xsl:when>
                              <xsl:otherwise>0</xsl:otherwise>
                            </xsl:choose>
                          </xsl:variable>
                          <xsl:variable name="second">
                            <xsl:choose>
                              <xsl:when test="$dt-length > 10">
                                <xsl:value-of select="substring($dt, 18)" />
                              </xsl:when>
                              <xsl:otherwise>0</xsl:otherwise>
                            </xsl:choose>
                          </xsl:variable>
                          <xsl:if test="$hour &lt;= 23 and $minute &lt;= 59 and $second &lt;= 60">
                            <xsl:variable name="new-second" select="$second + $dus" />
                            <xsl:variable name="new-minute" select="$minute + $dumin + floor($new-second div 60)" />
                            <xsl:variable name="new-hour" select="$hour + $duh + floor($new-minute div 60)" />
                            <xsl:variable name="new-month" select="$month + $dum" />
                            <xsl:call-template name="date:_add-days">
                              <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
                              <xsl:with-param name="month">
                                <xsl:variable name="m">
                                  <xsl:choose>
                                    <xsl:when test="$new-month &lt; 1">
                                      <xsl:value-of select="$new-month + 12" />
                                    </xsl:when>
                                    <xsl:otherwise>
                                      <xsl:value-of select="$new-month" />
                                    </xsl:otherwise>
                                  </xsl:choose>
                                </xsl:variable>
                                <xsl:choose>
                                  <xsl:when test="$m mod 12">
                                    <xsl:value-of select="format-number($m mod 12, '00')" />
                                  </xsl:when>
                                  <xsl:otherwise>12</xsl:otherwise>
                                </xsl:choose>
                              </xsl:with-param>
                              <xsl:with-param name="day" select="$day" />
                              <xsl:with-param name="days" select="$dud + floor($new-hour div 24)" />
                              <xsl:with-param name="eom" select="$eom" />
                            </xsl:call-template>
                            <xsl:text>T</xsl:text>
                            <xsl:value-of select="format-number(($new-hour + 24) mod 24, '00')" />
                            <xsl:text>:</xsl:text>
                            <xsl:value-of select="format-number($new-minute mod 60, '00')" />
                            <xsl:text>:</xsl:text>
                            <xsl:if test="$new-second mod 60 &lt; 10">0</xsl:if>
                            <xsl:value-of select="$new-second mod 60" />
                            <xsl:value-of select="$timezone" />
                          </xsl:if>
                        </xsl:if>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:variable name="new-month" select="$month + $dum" />
                        <xsl:call-template name="date:_add-days">
                          <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
                          <xsl:with-param name="month">
                            <xsl:variable name="m">
                              <xsl:choose>
                                <xsl:when test="$new-month &lt; 1">
                                  <xsl:value-of select="$new-month + 12" />
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="$new-month" />
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:variable>
                            <xsl:choose>
                              <xsl:when test="$m mod 12">
                                <xsl:value-of select="format-number($m mod 12, '00')" />
                              </xsl:when>
                              <xsl:otherwise>12</xsl:otherwise>
                            </xsl:choose>
                          </xsl:with-param>
                          <xsl:with-param name="day" select="$day" />
                          <xsl:with-param name="days" select="$dud" />
                          <xsl:with-param name="eom" select="$eom" />
                        </xsl:call-template>
                        <xsl:value-of select="$timezone" />
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:variable name="new-month" select="$month + $dum" />
                    <xsl:value-of select="format-number($year + $duy + floor(($new-month - 1) div 12), '0000')" />
                    <xsl:text>-</xsl:text>
                    <xsl:variable name="m">
                      <xsl:choose>
                        <xsl:when test="$new-month &lt; 1">
                          <xsl:value-of select="$new-month + 12" />
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="$new-month" />
                        </xsl:otherwise>
                      </xsl:choose>
                    </xsl:variable>
                    <xsl:choose>
                      <xsl:when test="$m mod 12">
                        <xsl:value-of select="format-number($m mod 12, '00')" />
                      </xsl:when>
                      <xsl:otherwise>12</xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="$timezone" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number($year + $duy, '0000')" />
                <xsl:value-of select="$timezone" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
        </xsl:if>
      </xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="string-length($date-time) > 10">
        <xsl:value-of select="ms:format-date($new-dt, $format, $locale)" />
        <xsl:if test="$format-time != ''">
           <xsl:value-of select="ms:format-time($new-dt, $format-time, $locale)" />       
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="ms:format-date(substring($new-dt, 1, string-length($date-time)),$format, $locale)" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="date:_add-days">
    <xsl:param name="year" />
    <xsl:param name="month" />
    <xsl:param name="day" />
    <xsl:param name="days" />
    <xsl:param name="new-day" select="'NaN'" />
    <xsl:param name="eom" />
    <xsl:variable name="leap" select="(not($year mod 4) and $year mod 100) or not($year mod 400)" />
    <xsl:variable name="days-in-month">
      <xsl:choose>
        <xsl:when test="$month = 2 and $leap">
          <xsl:value-of select="29" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="number($month) = 1 or number($month) = 3  or number($month) = 5 or number($month) = 7 or number($month) = 8 or number($month) = 10 or number($month) = 12">
              <xsl:value-of select="31" />
            </xsl:when>
            <xsl:when test="number($month) = 4 or number($month) = 6  or number($month) = 9 or number($month) = 11">
              <xsl:value-of select="30" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="28" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$new-day = 'NaN'">
        <xsl:call-template name="date:_add-days">
          <xsl:with-param name="year" select="$year" />
          <xsl:with-param name="month" select="$month" />
          <xsl:with-param name="new-day">
            <xsl:choose>
              <xsl:when test="$day > $days-in-month">
                <xsl:value-of select="$days-in-month + $days" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$day + $days" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name="eom" select="$eom" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$new-day &lt; 1">
            <xsl:call-template name="date:_add-days">
              <xsl:with-param name="year" select="$year - ($month = 1)" />
              <xsl:with-param name="month">
                <xsl:choose>
                  <xsl:when test="$month = 1">12</xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$month - 1" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:with-param>
              <xsl:with-param name="new-day">
                <xsl:variable name="days-in-new-month">
                  <xsl:choose>
                    <xsl:when test="$leap and $month = 3">29</xsl:when>
                    <xsl:when test="$month = 1">31</xsl:when>
                    <xsl:otherwise>
                      <xsl:choose>
                        <xsl:when test="number($month) = 2 or number($month) = 4 or number($month) = 6 or number($month) = 8 or number($month) = 9 or number($month) = 11">
                          <xsl:value-of select="31" />
                        </xsl:when>
                        <xsl:when test="number($month) = 3 or number($month) = 5 or number($month) = 7 or number($month) = 10 or number($month) = 12">
                          <xsl:value-of select="30" />
                        </xsl:when>
                      </xsl:choose>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:value-of select="$new-day + $days-in-new-month" />
              </xsl:with-param>
              <xsl:with-param name="eom" select="$eom" />
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="$new-day > $days-in-month">
            <xsl:call-template name="date:_add-days">
              <xsl:with-param name="year" select="$year + ($month = 12)" />
              <xsl:with-param name="month">
                <xsl:choose>
                  <xsl:when test="$month = 12">1</xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$month + 1" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:with-param>
              <xsl:with-param name="new-day" select="$new-day - $days-in-month" />
              <xsl:with-param name="eom" select="$eom" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number($year, '0000')" />
            <xsl:text>-</xsl:text>
            <xsl:value-of select="format-number($month, '00')" />
            <xsl:text>-</xsl:text>
            <xsl:choose>
              <xsl:when test="$eom = '1'">
                <xsl:value-of select="format-number($days-in-month, '00')" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="format-number($new-day, '00')" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
badsmell
  • 75
  • 11
  • can you please improve your Q to include a small sample set of input data, and the expected output from that input? You might also include the name of the XML tool you are using and if you are limited to using V 1.0 solutions. Good luck. – shellter Apr 06 '17 at 18:02
  • Im honestly not sure how to get a small sample of input data, however the output should be something like 20170101 for Jan 1, 2017. The EPOCH time stamp for this would be 1483300800. And yes, limited to XSL1 – badsmell Apr 06 '17 at 18:13
  • Is this what you are looking for? http://stackoverflow.com/a/3468123/2710666 – NMGod Apr 07 '17 at 03:44

1 Answers1

2

Converting a date to UNIX timestamp in XSLT 1.0:

<xsl:template name="date-to-UnixTime">
    <xsl:param name="date"/>

    <xsl:variable name="year" select="substring($date, 1, 4)"/>
    <xsl:variable name="month" select="substring($date, 6, 2)"/>
    <xsl:variable name="day" select="substring($date, 9, 2)"/>

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
    <xsl:variable name="y" select="$year + 4800 - $a"/>
    <xsl:variable name="m" select="$month + 12*$a - 3"/>

    <xsl:variable name="d" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 2472633"/>
    <xsl:value-of select="86400*$d" />
</xsl:template> 

Example of call:

<Unix>
    <xsl:call-template name="date-to-UnixTime">
        <xsl:with-param name="date" select="'2017-01-01'" />
    </xsl:call-template>
</Unix>

Result:

<Unix>1483228800</Unix>

Note:

Supplied date is assumed to be in UTC. The time is assumed to be 0:00:00.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Hi Michael, i'm using your above(v1.0) and doing a comparison with v2.0 code like below: `code``code` against "2017-08-03T19:20:39.096Z" but i see the difference in result "1501718400"(v1.0) vs "1501788039"(v2.0)... what is v1.0 missing – R.C Aug 18 '17 at 13:29
  • @R.C This is difficult to follow. Why don't you post this as a question, with all the code necessary to reproduce the problem? – michael.hor257k Aug 18 '17 at 13:38
  • sorry about that, added here: https://stackoverflow.com/questions/45758289/xslt-timestamp-to-epoch-v1-0-vs-v2-0 – R.C Aug 18 '17 at 13:50