I have a question concerning XML Schema's built-in type xsd:dateTime
.
What are the exact semantics of xsd:dateTime
without a timezone? Ex. 1970-01-01T00:00:00
.
I've read through a number of XML Schema spec documents but could not find out how should it be processed.
Specifically, I want to understand how to convert xsd:dateTime
to the Date (like java.util.Date
or JavaScript Date
) object correctly.
Side note: I am perfectly aware of Java util classes like DatatypeConverter
or DatatypeFactory
, I would like to find the XML Schema spec that defines how to do this conversion.
The problem with the Date
class (in Java as well in JavaScript) is that these classes do have timezones (defaulted to the local time zone). If I'm getting a xsd:dateTime
without time zone on input then I have to deside somehow, which time zone I should assume. Otherwise I just can't convert it to a timezoned value (like Date
).
Now the question is, what should I assume. I see following options here:
- Assume something default like UTC.
- Assume local timezone of the processor.
I don't really like the second option. It is entirely random! On my machine, if I run
System.out.println(DATATYPE_FACTORY
.newXMLGregorianCalendar("1970-01-01T00:00:00")
.toGregorianCalendar().getTime().getTime());
I'll get -3600000, 0, 3600000 for GMT+1, GMT or GMT-1 (and even more variants depending on summer time. This is so arbitrary, I'm really not getting this. Does this mean than when we have an XML document with an element like
<date-time>1970-01-01T00:00:00</date-time>
we have actually no idea, which exactly time instant was meant?
The first option (assuming UTC) seems more valid to me but this is apparently not what (at least) Java tools are doing.
So could please someone give me a pointer to a spec of some kind defining semantics of the timezoneless xsd:dateTime
?
Thank you.
Update:
Current findings are:
- Unspecified time zone has exactly the semantics of the "unspecified" time zone, that is, you can't blindly assume UTC or local time zone of the processor or whatever. This is some local time zone, but which one - you don't really know.
- This basically means that strictly speaking you can't convert
xsd:dateTime
to Date object which has a specific time zone - UNLESS an assumption about the absent time zone is somehow made. - As a tool provider, I can't really make an informed assumption of that kind. I have no background on data or its semantics.
- This leads me to the conclusion that the tool user has to provide such assumption - either explicitly or implicitly.
My solution will be as follows:
- In my library I have a so-called
context
object which provides the XML procession context (analog of JAXBJAXBContext
). I will extend this object with a methods likegetDefaultTimezoneOffset()
andsetDefaultTimezoneOffset(int timezoneOffset)
- By default, this method will return some default value. I prefer
0
(UTC) at the moment. However can be local time zone (like Java tools do) as well. - Library user is welcome to provide a different default time zone offset, but it is not strictly required ("implicit" assumption here)
- When parsing
xsd:dateTime
toDate
, if incoming value is missing a time zone, it will be assumed to be thecontext.getDefaultTimezoneOffset()
. - I will also note the incoming time zone (or lack thereof) in the parsed Date object. For instance in a property like
originalTimezoneOffset
or something like that. This will not modify the value of theDate
object but will provide some additional context information (for instance when the value should be printed again). - When printing a
Date
, the library would check for theoriginalTimezoneOffset
and if it is provided consider it when rendering the lexical value.