我正在尝试将UTC格式的XML中的日期转换为多个时区。我可以使用下面的行来获得,但我在计算夏季/冬季的差异时遇到了困难。有什么办法让我得到一个月以来的第一个星期日/最后一个星期日吗?
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT1H')
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT2H')
XML的位置:
<root>
<field myField="2002-12-24T12:00:12Z"/>
</root>
谢谢!
以下是计算给定日期当月第一个星期日的方法:
XML
<input>
<event date="2020-01-20"/>
<event date="2020-02-03"/>
<event date="2020-03-17"/>
<event date="2020-04-29"/>
<event date="2020-05-09"/>
<event date="2020-06-11"/>
<event date="2020-07-06"/>
<event date="2020-08-15"/>
<event date="2020-09-28"/>
<event date="2020-10-01"/>
<event date="2020-11-30"/>
<event date="2020-12-17"/>
</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/input">
<results>
<xsl:for-each select="event">
<xsl:variable name="date" select="xs:date(@date)"/>
<xsl:variable name="day1" select="$date - (day-from-date($date) - 1) * xs:dayTimeDuration('P1D')"/>
<xsl:variable name="weekday" select="xs:integer(format-date($day1, '[F1]'))"/>
<first-sunday>
<xsl:value-of select="$day1 + (7 - $weekday) * xs:dayTimeDuration('P1D')"/>
</first-sunday>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="utf-8"?>
<results>
<first-sunday>2020-01-05</first-sunday>
<first-sunday>2020-02-02</first-sunday>
<first-sunday>2020-03-01</first-sunday>
<first-sunday>2020-04-05</first-sunday>
<first-sunday>2020-05-03</first-sunday>
<first-sunday>2020-06-07</first-sunday>
<first-sunday>2020-07-05</first-sunday>
<first-sunday>2020-08-02</first-sunday>
<first-sunday>2020-09-06</first-sunday>
<first-sunday>2020-10-04</first-sunday>
<first-sunday>2020-11-01</first-sunday>
<first-sunday>2020-12-06</first-sunday>
</results>