Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can some help me,
I need to convert datetime to epoch, i use this script but on the new server i am not allowed to use document('') function.

So how get it back to work, can someone covert it for me.

This is the code i use:

XML
<pre><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="https://github.com/ilyakharlamov/pure-xsl/date"
    version="1.0">
    <xsl:output method="text"/>
    <xsl:decimal-format NaN="0"/>
    <xsl:template match="/">
        <xsl:variable name="time_as_timestamp" select="1365599995640"/>
        <xsl:text>time_as_timestamp:</xsl:text><xsl:value-of select="$time_as_timestamp"/><xsl:text></xsl:text>
        <xsl:variable name="time_as_xsdatetime">
            <xsl:call-template name="date:date-time">
                <xsl:with-param name="timestamp" select="$time_as_timestamp"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:text>time_as_xsdatetime:</xsl:text><xsl:value-of select="$time_as_xsdatetime"/><xsl:text></xsl:text>
        <xsl:text>back_as_timestamp:</xsl:text>
        <xsl:call-template name="date:timestamp">
            <xsl:with-param name="date-time" select="$time_as_xsdatetime"/>
        </xsl:call-template>
    </xsl:template>
    
    <date:month>
        <january>31</january>
        <february>28</february>
        <march>31</march>
        <april>30</april>
        <may>31</may>
        <june>30</june>
        <july>31</july>
        <august>31</august>
        <september>30</september>
        <october>31</october>
        <november>30</november>
        <december>31</december>
    </date:month>
    <xsl:variable name="date:month"
        select="document('')//date:month"/>
    
    
    <xsl:template name="date:timestamp">
        <xsl:param name="date-time"/>
       
        <xsl:variable name="compact"
            select="
            normalize-space(
            translate($date-time,'TZ ',''))"/>
        <xsl:variable name="year"
            select="
            translate(
            substring($compact,1,
            4+(starts-with($compact,'+') or
            starts-with($compact,'-'))),
            '+','')"/>
        <xsl:variable name="date"
            select="substring-after($compact,$year)"/>
        <xsl:variable name="time"
            select="substring($date,7)"/>
        <xsl:variable name="month"
            select="format-number(substring($date,2,2)-1,0)"/>
        <xsl:variable name="utc-offset">
            <xsl:variable name="raw"
                select="
                concat(
                substring-after($time,'+'),
                substring-after($time,'-'))"/>
            <xsl:value-of select="
                format-number(
                (contains($time,'-')-.5)
                *2*(substring($raw,1,2)*60
                +substring($raw,4,2)),0)"/>
        </xsl:variable>
        <xsl:value-of select="
            format-number(
            1000*(
            24*3600*(
            $year*365-719527
            +floor($year div 4)
            -floor($year div 100)
            +floor($year div 400)
            +sum($date:month/*[$month>=position()])
            +format-number(substring($date,5,2)-1,0)
            -(2>$month and (($year mod 4=0 and
            $year mod 100!=0) or
            $year mod 400=0)))
            +format-number(
            concat(0,substring($time,7,
            (substring($time,6,1)=':')*2))
            +substring($time,1,2)*3600
            +substring($time,4,2)*60,0)
            +$utc-offset*60)
            +format-number(
            round(
            (substring($time,9,1)='.')
            *1000*substring-before(
            translate(
            concat('0.',substring-after($time,'.'),'_'),
            '+-','__'),'_')),0),0)"/>
    </xsl:template>
    <xsl:template name="date:date-time">
        <xsl:param name="utctimestampmilliseconds"/><!-- UTC -->
        <xsl:param name="shifttzinminutes" select="0"/>
        <xsl:param name="is_include_milliseconds" select="true()"/>
        <xsl:param name="keepz" select="true()"/>
        <xsl:variable name="utctimestampseconds" select="$utctimestampmilliseconds div 1000 + $shifttzinminutes*60"/>
        <xsl:variable name="s" select="$utctimestampseconds mod 86400"></xsl:variable>
        <xsl:variable name="h" select="floor($s div 3600)"></xsl:variable>
        <xsl:variable name="m" select="floor($s div 60 mod 60)"></xsl:variable>
        <xsl:variable name="seconds" select="floor($s mod 60)"/>
        <xsl:variable name="ts" select="floor($utctimestampseconds div 86400)"/>
        <xsl:variable name="x" select="floor(($ts*4+102032) div 146097+15)"></xsl:variable>
        <xsl:variable name="b" select="floor(($x div -4)+$ts+2442113+$x)"/>
        <xsl:variable name="c" select="floor(($b*20 - 2442) div 7305)"/>
        <xsl:variable name="d" select="ceiling(($b) - (365*$c) - ($c div 4))"/>
        <xsl:variable name="e" select="floor($d*(1000 div 30601))"/>
        <xsl:variable name="f" select="ceiling($d - ($e*30) - ($e*601 div 1000))"/>
        
        <!--xsl:value-of select="concat('utctimestampseconds', $utctimestampseconds,' ts', $ts, ' x', $x, ' b', $b,' c',$c,' d', $d,' e', $e,' f', $f,' s:',$s,'h:',$h,'m:',$m,'seconds', $seconds)"/>
        <xsl:text></xsl:text-->
        
        <xsl:variable name="timezonepart">
            <xsl:choose>
                <xsl:when test="$shifttzinminutes">
                    <xsl:variable name="shifttzsign">
                        <xsl:choose>
                            <xsl:when test="$shifttzinminutes > 0">
                                <xsl:value-of select="'+'"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="'-'"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:variable name="shifttzhrsabs">
                        <xsl:variable name="vNum" select="$shifttzinminutes div 60"/>
                        <xsl:value-of select="$vNum*($vNum >=0) - $vNum*($vNum < 0)"/>
                    </xsl:variable>
                    <xsl:variable name="shifttzminabs"><!-- abs number of minues -->
                        <xsl:variable name="vNum" select="$shifttzinminutes mod 60"/>
                        <xsl:value-of select="$vNum*($vNum >=0) - $vNum*($vNum < 0)"/>
                    </xsl:variable>
                    <xsl:value-of select="concat($shifttzsign,format-number($shifttzhrsabs, '00'),':',format-number($shifttzminabs, '00'))"/>
                </xsl:when>
                <xsl:when test="$keepz"><xsl:value-of select="'Z'"/></xsl:when>
            </xsl:choose>
        </xsl:variable>
        
        <xsl:choose><!-- YEAR-MO -->
            <xsl:when test="$e < 14">
                <xsl:value-of select="$c - 4716"/>
                <xsl:value-of select="'-'"/>
                <xsl:value-of select="format-number($e - 1,'00')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$c - 4715"/>
                <xsl:value-of select="'-'"/>
                <xsl:value-of select="format-number($e - 13,'00')"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="'-'"/>
        <xsl:value-of select="format-number($f,'00')"/>
        <xsl:value-of select="'T'"/>
        <xsl:value-of select="concat(
            format-number($h,'00'),':',
            format-number($m,'00'),':',
            format-number($seconds,'00'))
            "/>
        <xsl:if test="$is_include_milliseconds">
            <xsl:value-of select="concat('.',format-number($utctimestampmilliseconds mod 1000,'000'))"/>
        </xsl:if>
        <xsl:value-of select="$timezonepart"/>        
        
    </xsl:template>

</xsl:stylesheet>


What I have tried:

i have tried stripping the document('') part but now the month is always januari in the epoch time
Posted
Updated 22-Jul-23 23:27pm
Comments
Graeme_Grant 22-Jul-23 20:25pm    
[moved]

Where?
How?
Where is the code?
What is the exact error that you are seeing and which line of code?


Help us help you. We can't see your screen. You need to use the green "Improve question" and post your code with you data. You need to tag your question as well with the programming language that you are using.

To quote the help in the yellow box where you post your question:

Quote:
A few simple rules when posting your question.

1. Be courteous. Everyone here helps because they enjoy helping, not because it's their job.
2. Have you searched or Googled for a solution?
3. Be specific! eg "How do I change the dialog colour?" instead of "My code doesn't work. Help?"
4. Tag your question appropriately.
5. If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
6. Do not remove or empty a message if others have replied.
7. Your question may be edited or retagged by others. Anything inappropriate will be removed.
 
Share this answer
 
I have added a small template in your code that should convert the DateTimes based on the tutorial at - Formatting Dates and Times[^] -

XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="text" />
    <xsl:decimal-format NaN="0"/>

    <xsl:template match="/">
        <xsl:variable name="time_as_timestamp" select="1365599995640"/>
        <xsl:text>time_as_timestamp:</xsl:text>
        <xsl:value-of select="$time_as_timestamp"/>
        <xsl:text></xsl:text>
        <xsl:text>time_as_epoch:</xsl:text>
        <!--Call the existing date:timestamp template to get the epoch time -->
        <xsl:call-template name="date:timestamp">
            <xsl:with-param name="date-time" select="custom:dateTimeToXSDateTime($time_as_timestamp)"/>
        </xsl:call-template>
    </xsl:template>

    <!--The template converts the given timestamp to xs:dateTime format -->
    <xsl:template name="custom:dateTimeToXSDateTime">
        <xsl:param name="timestamp" />
        <xsl:variable name="timezone" select="'Z'" />
        <xsl:variable name="seconds" select="floor($timestamp div 1000)" />
        <xsl:variable name="milliseconds" select="$timestamp mod 1000" />
        <xsl:variable name="datePart" select="floor($seconds div (24 * 60 * 60)) + 719528" />
        <xsl:variable name="timePart" select="format-number($seconds mod (24 * 60 * 60), '00'):format-number(($seconds mod (60 * 60)) div 60, '00'):format-number($seconds mod 60, '00')" />
        <xsl:value-of select="concat(format-number($datePart div 365, '0000'), '-', format-number($datePart mod 365 div 30 + 1, '00'), '-', format-number($datePart mod 365 mod 30 + 1, '00'), 'T', $timePart, '.', format-number($milliseconds, '000'), $timezone)" />
    </xsl:template>

    <!-- The rest of your code... -->    
</xsl:stylesheet>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900