X-Original-To: pgsql-general-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id C82C89FA4C1 for ; Fri, 16 Jun 2006 16:58:23 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 76356-10 for ; Fri, 16 Jun 2006 16:58:12 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from candle.pha.pa.us (candle.pha.pa.us [70.90.9.53]) by postgresql.org (Postfix) with ESMTP id 8B3249FA10A for ; Fri, 16 Jun 2006 16:58:11 -0300 (ADT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.11.6) id k5GJvxr26933; Fri, 16 Jun 2006 15:57:59 -0400 (EDT) From: Bruce Momjian Message-Id: <200606161957.k5GJvxr26933@candle.pha.pa.us> Subject: Re: A real currency type In-Reply-To: <20050828170727.GC12498@svana.org> To: Martijn van Oosterhout Date: Fri, 16 Jun 2006 15:57:58 -0400 (EDT) CC: Postgresql General X-Mailer: ELM [version 2.4ME+ PL121 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/687 X-Sequence-Number: 97059 I have added this URL to the TODO list in case people want to move forward on this. --------------------------------------------------------------------------- Martijn van Oosterhout wrote: -- Start of PGP signed section. > For a while I've been wondering about making a type that was really a > shell around a base type that tagged the type in some way. For example, > associating a currency with a numeric and complaining about additions > between mismatches. > > Well, I did it and it's available here: > http://svana.org/kleptog/pgsql/taggedtypes.html > > Below some examples of it in action. Yes, that's a timestamp that > remembers the timezone. Neat huh? > > Tested on 7.4 and a recent 8.1devel so it should work for most people. > Installation reports welcome. Note, this is beta software, don't run it > on your production server. Thanks. > > Have a nice day, > > > test=# select '5.6 USD'::currency + '4.5 USD'::currency;; > ?column? > ----------- > 10.10 USD > (1 row) > > test=# select '5.6 USD'::currency + '4.5 AUD'::currency;; > ERROR: Using operator +(currency,currency) with incompatable tags (USD,AUD) > test=# select c1, print_currency(c1) from c; > c1 | print_currency > ------------+---------------- > 232.44 USD | US$ 232.44 > 21.20 EUR | ? 21.20 > -13.44 AUD | AU$ -13.44 > 0.01 USD | US$ 0.01 > 14.00 AUD | AU$ 14.00 > (5 rows) > > test=# select 5.4*c1 from c where tag(c1) = 'AUD'; > ?column? > ------------ > -72.58 AUD > 75.60 AUD > (2 rows) > > test=# select t, "timestamp"(t), date_part('hour',t) from c; > t | timestamp | date_part > -----------------------------------------+---------------------+----------- > 2005-08-14 02:00:00+02 Europe/Amsterdam | 2005-08-14 02:00:00 | 2 > 2005-08-14 02:00:00+02 Australia/Sydney | 2005-08-14 10:00:00 | 10 > 2005-08-14 02:00:00+02 Asia/Hong_Kong | 2005-08-14 08:00:00 | 8 > 2005-08-14 02:00:00+02 America/New_York | 2005-08-13 20:00:00 | 20 > 2005-08-14 02:00:00+02 Asia/Kuwait | 2005-08-14 03:00:00 | 3 > (5 rows) > > -- > Martijn van Oosterhout http://svana.org/kleptog/ > > Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a > > tool for doing 5% of the work and then sitting around waiting for someone > > else to do the other 95% so you can sue them. -- End of PGP section, PGP failed! -- Bruce Momjian http://candle.pha.pa.us EnterpriseDB http://www.enterprisedb.com + If your life is a hard drive, Christ can be your backup. +