#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; }