Convert Date / Time from 02/13/2013 23:59:59 to UNIX Milliseconds (1360817999000)

那年仲夏 提交于 2021-02-11 16:51:35

问题


Here is my beginning XML:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

I am converting it to the format I need using this XSLT:

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xtt="urn:com.workday/xtt"
    xmlns:etv="urn:com.workday/etv"
    xmlns:wd="urn:com.workday.report/CR-INT486-Kuali_Trojan_Time-Absence_Balances-Outbound" 
    exclude-result-prefixes="wd xsl"
    >

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <document>
            <xsl:for-each select="wd:Report_Data/wd:Report_Entry/wd:All_Eligible_Time_Off_Plans_for_Worker">
                <leaveBalanceTotal>
                    <employeeId><xsl:value-of select="parent::node()/wd:employeeId"/></employeeId>
                    <uscId><xsl:value-of select="parent::node()/wd:uscId"/></uscId>
                    <leaveCodeId><xsl:value-of select="wd:leaveCodeStr"/></leaveCodeId>
                    <balanceAmount><xsl:value-of select="wd:balanceAmount"/></balanceAmount>
                    <effectiveDate><xsl:value-of select="parent::node()/wd:effectiveDate"/></effectiveDate>
                    <totalDaysService><xsl:value-of select="parent::node()/wd:totalDaysService"/></totalDaysService>
                    <lastPayEndDate><xsl:value-of select="parent::node()/wd:lastPayEndDate"/></lastPayEndDate>
                </leaveBalanceTotal>
            </xsl:for-each>
        </document>
    </xsl:template>
</xsl:stylesheet>

This is the output I get:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>03/11/2013 23:59:59</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>03/11/2013 23:59:59</lastPayEndDate>

</leaveBalanceTotal>

However, I need the two date fields converted to UNIX milliseconds like this with the dates converted to UNIX milliseconds:

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Sick</leaveDescription>

<leaveCodeStr>SS</leaveCodeStr>

<balanceAmount>90.22</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Vacation</leaveDescription>

<leaveCodeStr>VA</leaveCodeStr>

<balanceAmount>187.11</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

<leaveBalanceTotal>

<employeeId>0001234</employeeId>

<uscId>1234567894654</uscId>

<leaveDescription>Winter Recess</leaveDescription>

<leaveCodeStr>WR</leaveCodeStr>

<balanceAmount>30</balanceAmount>

<effectiveDate>1363060799000</effectiveDate>

<totalDaysService>6062</totalDaysService>

<lastPayEndDate>1363060799000</lastPayEndDate>

</leaveBalanceTotal>

Basically I am hoping someone can show me how to convert these dates. Any help would be appreciated.


回答1:


You can get milliseconds since the Unix epoch by dividing the duration since 1970-01-01 by one millisecond.

For example, prefix xs: being bound to namespace URI http://www.w3.org/2001/XMLSchema, the following XPath 2.0 expression

(xs:dateTime('2013-02-14T23:59:59') - xs:dateTime('1970-01-01T00:00:00')) 
div xs:dayTimeDuration('PT0.001S')

evaluates to

1360886399000


Edited to add:

For parsing your dates, you might want to look into regular expression matching in XSLT 2.0. For example, you could write a function which allows you to parse your dates into xs:dateTime values.

<!-- parses a dateTime value from a string of format "mm/dd/yyyy hh:mm:ss" -->
<xsl:function name="foo:parseDateTime" as="xs:dateTime">
  <xsl:param name="input" as="xs:string"/>
  <xsl:variable name="dateTimeLiteral">
    <xsl:analyze-string select="normalize-space($input)" 
                        regex="(\d\d)/(\d\d)/(\d\d\d\d)\s(\d\d):(\d\d):(\d\d)">
      <xsl:matching-substring>
        <xsl:number value="regex-group(3)" format="0001"/>          
        <xsl:text>-</xsl:text>
        <xsl:number value="regex-group(2)" format="01"/>          
        <xsl:text>-</xsl:text>
        <xsl:number value="regex-group(1)" format="01"/>
        <xsl:text>T</xsl:text>
        <xsl:number value="regex-group(4)" format="01"/>
        <xsl:text>:</xsl:text>
        <xsl:number value="regex-group(5)" format="01"/>
        <xsl:text>:</xsl:text>
        <xsl:number value="regex-group(6)" format="01"/>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:variable>
  <xsl:value-of select="xs:dateTime($dateTimeLiteral)"/>
</xsl:function>

You could write another function which encapsulates the above example of converting into milliseconds:

<!-- converts a dateTime into milliseconds since the beginning of 1970" -->
<xsl:function name="foo:toMilliseconds" as="xs:integer">
  <xsl:param name="input" as="xs:dateTime"/>
  <xsl:value-of select="(xs:dateTime($input)-xs:dateTime('1970-01-01T00:00:00'))
                        div xs:dayTimeDuration('PT0.001S')"/>
</xsl:function>

Then you could combine these to write your dates as milliseconds, like this:

<xsl:value-of select="foo:toMilliseconds(foo:parseDateTime(lastPayEndDate))"/>


来源:https://stackoverflow.com/questions/15345457/convert-date-time-from-02-13-2013-235959-to-unix-milliseconds-1360817999000

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!