pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedAn strftime function, and function name question
3+ messages / 2 participants
[nested] [flat]
* An strftime function, and function name question
@ 2001-02-07 16:00 Christopher Masto <chris@netmonger.net>
2001-02-12 15:38 ` Re: An strftime function, and function name question Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 3+ messages in thread
From: Christopher Masto @ 2001-02-07 16:00 UTC (permalink / raw)
To: pgsql-interfaces
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/
Attachments:
strftime.c (text/plain, 1.0K) download
#include <string.h>
#include <time.h>
#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;
}
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: An strftime function, and function name question
2001-02-07 16:00 An strftime function, and function name question Christopher Masto <chris@netmonger.net>
@ 2001-02-12 15:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-12 16:04 ` Re: An strftime function, and function name question Christopher Masto <chris@netmonger.net>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2001-02-12 15:38 UTC (permalink / raw)
To: Christopher Masto <chris@netmonger.net>; +Cc: pgsql-interfaces
Christopher Masto <chris@netmonger.net> writes:
> 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".
See the CREATE FUNCTION reference page,
http://www.postgresql.org/users-lounge/docs/7.0/postgres/sql-createfunction.htm
concerning link symbols that are different from the SQL name of the
function.
If you're using something older than 7.0, time to upgrade ...
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: An strftime function, and function name question
2001-02-07 16:00 An strftime function, and function name question Christopher Masto <chris@netmonger.net>
2001-02-12 15:38 ` Re: An strftime function, and function name question Tom Lane <tgl@sss.pgh.pa.us>
@ 2001-02-12 16:04 ` Christopher Masto <chris@netmonger.net>
0 siblings, 0 replies; 3+ messages in thread
From: Christopher Masto @ 2001-02-12 16:04 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-interfaces
On Mon, Feb 12, 2001 at 10:38:26AM -0500, Tom Lane wrote:
> Christopher Masto <chris@netmonger.net> writes:
> > 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".
>
> See the CREATE FUNCTION reference page,
> http://www.postgresql.org/users-lounge/docs/7.0/postgres/sql-createfunction.htm
> concerning link symbols that are different from the SQL name of the
> function.
Thank you. That was exactly what I was looking for.. I don't know
how I missed it. I must have been reading the wrong manual.
--
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/
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-02-12 16:04 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-07 16:00 An strftime function, and function name question Christopher Masto <chris@netmonger.net>
2001-02-12 15:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-12 16:04 ` Christopher Masto <chris@netmonger.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox