How to code GMTIME in XSLT 1.0 - procedural vs. functional programming

Question

Using XSLT 1.0 with NO EXTENSIONS so a browser would be able to transform xml by using an associated xsl file.

I was wondering if it's possible to transform a Unix-timestamp (1480873681) into a date-array as done by the gmtime_r function (PHP 7 example):

CONST _lpdays = [-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
CONST _days = [-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364];

function gmtime_r(int $timestamp): array
{
    $isleapyear = false;
    $tmptimer = (int) ($timestamp / _FOUR_YEAR_SEC);
    $timer = $timestamp - ($tmptimer * _FOUR_YEAR_SEC);
    $tmptimer = ($tmptimer * 4) + 70;
    if ($timer >= _YEAR_SEC) {
        $tmptimer = $tmptimer + 1;
        $timer = $timer - _YEAR_SEC;
        if ($timer >= _YEAR_SEC) {
            $tmptimer = $tmptimer + 1;
            $timer = $timer - _YEAR_SEC;
            if ($timer >= (_YEAR_SEC + _DAY_SEC)) {
                $tmptimer = $tmptimer + 1;
                $timer = $timer - (_YEAR_SEC + _DAY_SEC);
            } else {
                $isleapyear = true; 
            }
        }
    }
    $ptm['year'] = $tmptimer;
    $ptm['yday'] = (int) ($timer / _DAY_SEC);
    $timer = $timer - ($ptm['yday'] * _DAY_SEC);
    if ($isleapyear) {
        $mdays = _lpdays;
    }
    else {
        $mdays = _days;
    }
    for ($tmptimer = 1; $mdays[$tmptimer] < $ptm['yday']; $tmptimer++);
    $tmptimer = $tmptimer - 1;
    $ptm['mon'] = $tmptimer;
    $ptm['mday'] = $ptm['yday'] - $mdays[$tmptimer];
    $ptm['wday'] = ((int) ($timestamp / _DAY_SEC) + _BASE_DOW) % 7;
    $ptm['hours'] = (int) ($timer / _HOUR_SEC);
    $timer = $timer - ($ptm['hours'] * _HOUR_SEC);
    $ptm['minutes'] = (int) ($timer / _MIN_SEC);
    $ptm['seconds'] = ($timer - $ptm['minutes'] * _MIN_SEC);
//    $ptm['isdst'] = 0;
    return $ptm;
}

I replaced the array by a more xsl-style structure:

<my:mmonth>
    <d l="0">-1</d><d l="0">30</d><d l="0">59</d><d l="0">90</d><d l="0">120</d><d l="0">151</d><d l="0">181</d><d l="0">212</d><d l="0">243</d><d l="0">273</d><d l="0">304</d><d l="0">334</d><d l="0">365</d>
    <d l="1">-1</d><d l="1">30</d><d l="1">58</d><d l="1">89</d><d l="1">119</d><d l="1">150</d><d l="1">180</d><d l="1">211</d><d l="1">242</d><d l="1">272</d><d l="1">303</d><d l="1">333</d><d l="1">364</d>
</my:mmonth>

so the for-loop could be replaced by

<xsl:variable name="mon" select="count(document('')/xsl:stylesheet/my:mmonth/d[(@l=$isleapyear) and (. &lt; $yday)]) - 1"/>
<xsl:variable name="mday" select="$yday - (document('')/xsl:stylesheet/my:mmonth/d[(@l=$isleapyear)][$mon])"/>

Because of the rigid variable-scope of XSLT the nested if-structure gives me a headache...

if ($timer >= _YEAR_SEC) {
    $tmptimer = $tmptimer + 1;
    $timer = $timer - _YEAR_SEC;
    if ($timer >= (_YEAR_SEC + _DAY_SEC)) {
        $tmptimer = $tmptimer + 1;
        $timer = $timer - (_YEAR_SEC + _DAY_SEC);
    } else {
        $isleapyear = true; 
    }
}

Any help would be appreciated!


Show source
| php   | xml   | xslt   | unix-timestamp   2016-12-04 14:12 0 Answers

Answers to How to code GMTIME in XSLT 1.0 - procedural vs. functional programming ( 0 )

Leave a reply to - How to code GMTIME in XSLT 1.0 - procedural vs. functional programming

◀ Go back