Using proper timezone information with SuRF

A short note on how best to deal with date, time and datetime objects in SuRF.

Chances are that you are living in a timezone other than UTC (an imaginary timezone) and want to properly handle time. Coming from SQL systems people might not be used to having a databases store additional timezone information (at least MySQL doesn’t). RDF stores like AllegroGraph and Virtuoso however follow the XML and more precisely the ISO 8601 standard when storing date & time objects and make your life easier.

At least if you follow this short suggestion here.

As I tried to document in http://code.google.com/p/surfrdf/wiki/BackendPeculiarities, Virtuoso and AllegroGraph handle datetime objects differently. When presented with a timezone-less date, Virtuoso assumes the server’s timezone, while AllegroGraph uses UTC (“Z”). You are probably using Python and so you have to deal with this, as Python doesn’t use timezones out of the box. RDFLIB also ignores timezones for now which will hopefully change once http://code.google.com/p/rdflib/issues/detail?id=169 is implemented.

If you want to make sure the correct timezone is stored, look at the example below. This code uses pytz to get the UTC timezone and stores datetime.now() as UTC. This will make both Virtuoso & AGraph store the same date. If you don’t intend to store values as UTC, look into pytz which has brilliant support that Python is missing.

https://gist.github.com/1002887

A small fact on the side: AllegroGraph normalizes all timestamps to UTC – so the offset gets lost. It did give me some headaches, it might so give you.

Advertisements