Format of timezone in datetime…

356 views
Skip to first unread message

Viktor Hedefalk

unread,
Dec 28, 2011, 4:38:20 PM12/28/11
to sca...@googlegroups.com
Is it possible to define the format of a datetime? I guess it is
decided by the locale of the system at runtime, but my client is
having problems consuming my service running in production which emits
dates like:

<ProjectStartDate>2012-02-17T00:00:00.000Z</ProjectStartDate>

Localhost I have:

<ProjectStartDate>2012-02-07T00:00:00.000+01:00</ProjectStartDate>

Is it the locale of the server that decides the format? Is it possible
to programmatically override the behaviour?

Thanks,
Viktor

Viktor Hedefalk

unread,
Dec 28, 2011, 5:22:22 PM12/28/11
to sca...@googlegroups.com
Oh, the issue seems to be something totally different. I had actually
used a xsd:date in the schema:

<xsd:element name="ProjectStartDate" type="xsd:date"/>

but the xml contains time stuff:

<ProjectStartDate>2012-02-17T00:00:00.000Z</ProjectStartDate>

or


<ProjectStartDate>2012-02-07T00:00:00.000+01:00</ProjectStartDate>

That's not correct, is it?

Thanks,
Viktor

eugene yokota

unread,
Dec 28, 2011, 5:36:20 PM12/28/11
to sca...@googlegroups.com
Hi Viktor,

How was the date object created?
I wrote a quick test to respond to your first question, but

import javax.xml.datatype.{DatatypeFactory, XMLGregorianCalendar}
DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2012, 02, 17, 0)

seems to output without time component out of the box in my environment.

As per customizing the marshaling protocol, here's how it looks:

object DateTest extends App {
import javax.xml.datatype.{DatatypeFactory, XMLGregorianCalendar}

val date =
se.hedefalk.test.Element1(DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2012,
02, 17, 0))

def testDefault() {
val s = scalaxb.toXML(date, "element1", scalaxb.toScope())
println(s)
}

def testCusomization() {
val xmlprotocol = new se.hedefalk.test.XMLProtocol {
override lazy val __CalendarXMLFormat:
scalaxb.XMLFormat[XMLGregorianCalendar] = new
scalaxb.XMLFormat[XMLGregorianCalendar] {
import scalaxb._
def reads(seq: scala.xml.NodeSeq, stack: List[ElemName]):
Either[String, XMLGregorianCalendar] = try {
Right(XMLCalendar(seq.text)) } catch { case e: Exception
=> Left(e.toString) }

def writes(obj: XMLGregorianCalendar, namespace:
Option[String], elementLabel: Option[String],
scope: scala.xml.NamespaceBinding, typeAttribute:
Boolean): scala.xml.NodeSeq =
Helper.stringToXML(obj.toXMLFormat + "foo", namespace,
elementLabel, scope)
}
}
import xmlprotocol._

val s = scalaxb.toXML(date, "element1", scalaxb.toScope())
println(s)
}


testDefault()
testCusomization()
}

The code is up [here][1].

-eugene

[1]: https://github.com/eed3si9n/scalaxb-sample/tree/hedefalk/hedefalk

Viktor Hedefalk

unread,
Dec 30, 2011, 3:40:53 PM12/30/11
to sca...@googlegroups.com
Ah, thanks!

> How was the date object created?

I had so many dates I did an implicit:

implicit def date2XmlGregorianCal(date: Date) = {
val cal = new GregorianCalendar()
cal.setTime(date)
datatypeFactory.newXMLGregorianCalendar(cal)
}


and then forgot about it :)

I guess I figured that the xsd-type would decide how it was formatted,
like defined here:
http://www.w3schools.com/schema/schema_dtypes_date.asp…?

If I override the __CalendarXMLFormat, does that override the
behaviour of all date-types? Like the lot defined in XsTypeSymbol:

object XsDateTime extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsTime extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsDate extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsGYearMonth extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsGYear extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsGMonthDay extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsGDay extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}
object XsGMonth extends
BuiltInSimpleTypeSymbol("javax.xml.datatype.XMLGregorianCalendar") {}


I have the need for xsd:datetime too. Maybe I'm misunderstanding…?

Thanks,
Viktor

eugene yokota

unread,
Dec 30, 2011, 5:28:44 PM12/30/11
to sca...@googlegroups.com


On Friday, December 30, 2011 3:40:53 PM UTC-5, Viktor Hedefalk wrote:
Ah, thanks!

> How was the date object created?

I had so many dates I did an implicit:

implicit def date2XmlGregorianCal(date: Date) = {
    val cal = new GregorianCalendar()
    cal.setTime(date)
    datatypeFactory.newXMLGregorianCalendar(cal)
  }

Since both Date class and Calendar class can store date and time, newXMLGregorianCalendar probably making xs:dateTime.
Try newXMLGregorianCalendarDate.

-eugene

Viktor Hedefalk

unread,
Jan 1, 2012, 10:28:35 AM1/1/12
to sca...@googlegroups.com
Ah, thanks!

I did it like this:

def xmlDate(date: Date) = {


val cal = new GregorianCalendar()
cal.setTime(date)

val month = cal.get(Calendar.MONTH) + 1;
val day = cal.get(Calendar.DAY_OF_MONTH);
val year = cal.get(Calendar.YEAR);
datatypeFactory.newXMLGregorianCalendarDate(year, month, day,
DatatypeConstants.FIELD_UNDEFINED)
}

which gives me just the dates like I wanted. Are there any utils for
this? Feels hacky to write that code…

Thanks,
Viktor

eugene yokota

unread,
Jan 2, 2012, 2:41:01 PM1/2/12
to sca...@googlegroups.com

On Sunday, January 1, 2012 10:28:35 AM UTC-5, Viktor Hedefalk wrote:

Are there any utils for
this? Feels hacky to write that code…

I don't know of any. The problem is XMLGregorianCalendar is able to distinguish different types of instances
such as xs:date, xs:dateTime and xs:gYear, and persist it to a uniquely distinguishable string representation (for example, "1999" is gYear).
I'm not sure any of the date time types are able to do that.

-eugene

Reply all
Reply to author
Forward
0 new messages