Re: mxDateTime timestamps with locale active

Karsten Hilbert <[email protected]> Tue, 25 May 2010 14:18:06 +0200
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Tue, May 25, 2010 at 11:32:26AM +0100, Daniele Varrazzo wrote:

> I preferred to avoid comments mostly because I don't know exactly the
> problem. I've not used mx.datetimes for ages and never in combination
> with timezones. You have presented an implementation, but I don't know
> exactly the problem it wanted to solve. For what I've seen fixing one
> of the latest bug mx doesn't support timezones, and I've not checked
> how timezones are handled by these objects.

It does support timezones somewhat:

	http://www.egenix.com/products/python/mxBase/mxDateTime/doc/#_Toc201421432

	http://www.egenix.com/products/python/mxBase/mxDateTime/doc/#_Toc201421445=
 (see .tz)

But timezones aren't part of the problem - I just failed to
remove non-relevant code from the typecaster.

> If there is a bug, the best thing is to fix it in psycopg, not to add
> a customized typecaster. So, can you provide an example where psycopg
> does the wrong thing and the expected result?

It used to be that this:

	import locale
	locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
	import mx.DateTime as mxDT
	now =3D mxDT.now()

	print now
	print '%s' % now
	print mxDT.ISO.str(now)

would show:

	2010-05-25 13:57:15,71
	2010-05-25 13:57:15,71
	2010-05-25 13:57:15,71+0200

rather than

	2010-05-25 13:57:15.71
	2010-05-25 13:57:15.71
	2010-05-25 13:57:15+0200

(note the "," in place of the ".")

Psycopg2 used to use a method of converting mxDateTime.DateTime
instances to strings that resulted in either "," or "." for the
decimal fraction of a second depending on the locale that was
in use (Germany uses "," to separate decimal fractions)

This is surely legal as per ISO8601 (look for "decimal
fraction" on http://en.wikipedia.org/wiki/ISO_8601).

However, PostgreSQL cannot handle "," as the separator:

	root@hermes:~/bin# psql -d gnumed_v14 -U gm-dbo
	psql (8.4.4)
	Geben Sie =BBhelp=AB f=FCr Hilfe ein.

	gnumed_v14=3D> SELECT '1999-01-01 13:45:15.15'::timestamp;
	       timestamp
	------------------------
	 1999-01-01 13:45:15.15
	(1 Zeile)

	gnumed_v14=3D> SELECT '1999-01-01 13:45:15,15'::timestamp;
	ERROR:  invalid input syntax for type timestamp: "1999-01-01 13:45:15,15"
	ZEILE 1: SELECT '1999-01-01 13:45:15,15'::timestamp;
	                ^

So, GNUmed used a custom typecaster to convert "," to "."
when adapting mxDateTimes. Note that we've been around using
psycopg2 before Python came with a usable standard lib
datetime module...

It seems the problem does not exist with psycopg2 anymore as
I cannot make it happen any longer.

> If you have other of these hacks, it would be nice to have the problem
> they address in the psycopg test suite :)

I guess that's it for now :-)

Karsten
-- =

GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346