Hi,

One of my developers tripped across another weirdness around timezones today:

A test snippet looks like this:

---
import pyxb_generated

"""
$ pyxbgen --schema-root=. -u schema.xsd -m pyxb_generated

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
targetNamespace="http://test.com/test";
xmlns:zenkai="http://test.com/test";>
    <xsd:element name="timestamp" type="xsd:dateTime"/>
</xsd:schema>
"""

xml = '<?xml version="1.0" ?><ns1:timestamp
xmlns:ns1="http://test.com/test";>2012-05-14T16:36:02.157+10:00</ns1:timestamp>'
obj = pyxb_generated.CreateFromDocument(xml)
print "{} --> {} --> {}".format(xml, obj, obj.toxml())
---

(the original string in the variable "xml" was a timestamp generated
by JAXB, which we interface with PyXB in some of our apps)

The output looks like:
(pyenv-zktraining)nathanr@gionta:~/work/junkcode/pyxb-datetime-test>
python test2.py
<?xml version="1.0" ?><ns1:timestamp
xmlns:ns1="http://test.com/test";>2012-05-14T16:36:02.157+10:00</ns1:timestamp>
--> 2012-05-14 06:36:02.157000 --> <?xml version="1.0"
?><ns1:timestamp
xmlns:ns1="http://test.com/test";>2012-05-14T06:36:02.157Z</ns1:timestamp>
(pyenv-zktraining)nathanr@gionta:~/work/junkcode/pyxb-datetime-test>

The two XML fragments printed out look fine - the XML spec says that
they're equivalent, and they are. However, printing out the python
object prints the UTC time, not the local time (which is what I would
have expected, given that is the behaviour of datetime.datetime.now()
and friends in the base library). Reading through the PyXB source code
(pyxb.binding.datatypes.dateTime.__new__()), it looks like it's
intentional, and not a bug.

Now, of course this means that if we save a PyXB unmarshalled object
to a database table using an ORM layer it stores the time as 10 hours
off (PostgreSQL defaults the type TIMESTAMP to "TIMESTAMP WITHOUT
TIMEZONE", and we've got over 100 legacy tables with that datatype all
the way through them). Is there a way to make
pyxb.binding.datatypes.dateTime objects be symmetrical to
datetime.datetime.now() in terms of the way we deal with them?

Regards,
Nathan.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
pyxb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyxb-users

Reply via email to