Received: from mail.postgresql.org (webmail.postgresql.org [216.126.85.28]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f1CEhbx05686 for ; Mon, 12 Feb 2001 09:43:37 -0500 (EST) (envelope-from pgsql-interfaces-owner@postgresql.org) Received: from lion-around.at.yiff.net (lion-around.at.yiff.net [209.54.21.199]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f17G10x69539 for ; Wed, 7 Feb 2001 11:01:00 -0500 (EST) (envelope-from chris@netmonger.net) Received: (from chris@localhost) by lion-around.at.yiff.net (8.11.0/8.11.0) id f17G0ct28466 for pgsql-interfaces@postgresql.org; Wed, 7 Feb 2001 11:00:38 -0500 (EST) (envelope-from chris@netmonger.net) X-Authentication-Warning: lion-around.at.yiff.net: chris set sender to chris@netmonger.net using -f Date: Wed, 7 Feb 2001 11:00:38 -0500 From: Christopher Masto To: pgsql-interfaces@postgresql.org Subject: An strftime function, and function name question Message-ID: <20010207110038.A28372@netmonger.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="sdtB3X0nJg68CQEu" Content-Disposition: inline User-Agent: Mutt/1.2.5i X-Archive-Number: 200102/90 X-Sequence-Number: 1043 --sdtB3X0nJg68CQEu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline First of all, I have attached a simple-minded library I created to wrap the C strftime() function, since we needed some better date formatting and it seemed less cumbersome to do it on the server end. I gave up C for Perl a couple of years ago, so I may be a bit rusty, but I tried to be careful with the buffers and strings. In any case, it works for my purposes. I did also create a new typecast: CREATE FUNCTION int4 (timestamp) RETURNS int4 AS 'SELECT date_part(''epoch'', $1)::int4;' LANGUAGE 'sql'; because I wanted to be able to pass the Unix epoch time in, but I didn't know what timestamps are internally. My question is about naming. I would like to be able to call this function 'strftime'. I've never created libraries before so it may just be a lack of linker knowledge, but I couldn't see how to do that. If I call it strftime, it obviously conflicts with the real strftime(). It would be nice if there were something like: CREATE FUNCTION strftime(int4, varchar) RETURNS varchar AS '/usr/local/pgsql/lib/strftime.so' LANGUAGE 'c' USING C FUNCTION 'c_strftime'; I.e. some way to say "here's the name of the function in the library". If there is, I didn't find it. -- Christopher Masto Senior Network Monkey NetMonger Communications chris@netmonger.net info@netmonger.net http://www.netmonger.net Free yourself, free your machine, free the daemon -- http://www.freebsd.org/ --sdtB3X0nJg68CQEu Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="strftime.c" #include #include #include "postgres.h" #include "utils/palloc.h" typedef struct varlena varchar; varchar *c_strftime(int now, varchar *fmt_in) { char tmpbuf[128]; char *fmt; struct tm *mytm; int len; int32 new_text_size; varchar *new_text; /* Copy incoming format string to null-terminated C string */ fmt = (char *)palloc(VARSIZE(fmt_in) - VARHDRSZ + 1); memcpy((void *)fmt, (void *)VARDATA(fmt_in), VARSIZE(fmt_in) - VARHDRSZ); fmt[VARSIZE(fmt_in) - VARHDRSZ] = '\0'; /* Call strftime(). XXX portability and buffer complications; see Perl source POSIX.xs for ideas. */ mytm = localtime((const time_t *)&now); len = strftime(tmpbuf, sizeof tmpbuf, fmt, mytm); pfree(fmt); /* Copy the output into a Postgres varchar */ new_text_size = len + VARHDRSZ; new_text = (varchar *)palloc(new_text_size); memset((void *)new_text, 0, new_text_size); VARSIZE(new_text) = new_text_size; if (len) { memcpy((void *)VARDATA(new_text), (void *)tmpbuf, len); } return new_text; } --sdtB3X0nJg68CQEu--