如何在 XPath 中使用不同时区标准化日期存储?
我在 XML 文档中存储了以下时间,这些时间对应于文档创建和更新的时间:
<create-time>2010-11-04T03:13:35.212Z</create-time>
<update-time>2010-11-03T20:18:26.331-07:00</update-time>
该文档是在晚上 8:13 创建的,然后在 5 分钟后(晚上 8:18)更新,但是当我用 format-dateTime(xs:dateTime(.), '[M]/[D]/[Y]')
显示创建日期,我得到 11/4/2010 和 11/3 /2010,就好像文档在创建前一天更新过一样,但事实显然并非如此。我该如何解决这个问题?
I have the following times stored in an XML document, which correspond to the time when the document was created and then updated:
<create-time>2010-11-04T03:13:35.212Z</create-time>
<update-time>2010-11-03T20:18:26.331-07:00</update-time>
The document was created at 8:13 pm, and then updated 5 minutes later, at 8:18 pm, but when I show the creation dates with format-dateTime(xs:dateTime(.), '[M]/[D]/[Y]')
, I get 11/4/2010 and 11/3/2010, as if the document was updated one day before it was had been created, which is obviously not the case. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 文档中的
create-time
和update-time
是正确的,但它们使用不同的时区:create-time
位于 UTC(也称为祖鲁时间,因此是 Z)。update-time
采用太平洋时间。如果不同的代码片段设置了这个时间,或者甚至来自使用不同库或函数的相同代码,则可能会发生这种情况。例如,如果您使用 XForms 中的 XPath:
current -dateTime()
使用动态上下文中的时区,这通常是运行代码的计算机的当前时区。now()
始终返回 UTC 时间。XPath 中的解决方案是使用
调整-dateTime-to-timezone()
函数。这将使您的日期时间标准化,以便它们位于相同的时区。例如,在 XForms 输出中,要仅显示create-time
的日期部分,您可以使用:The
create-time
andupdate-time
in your XML document are correct, but they use different timezones:create-time
is in UTC (also called Zulu time, hence the Z).update-time
is in Pacific time.This can happen if different pieces of code set this the time, or even from the same code using different libraries or functions. For instance, if you are using XPath from XForms:
current-dateTime()
uses a timezone from the dynamic context, which is often the current timezone for the machine on which the code is running.now()
always returns a UTC time.The solution in XPath is to use the
adjust-dateTime-to-timezone()
function. This will normalize your dateTimes so they are in the same timezones. As an example, in an XForms output, to show just the date part ofcreate-time
you would use: