Skip site navigation (1) Skip section navigation (2)

Re: [HACKERS] my strftime func doesn't work. please help.

From: dg(at)illustra(dot)com (David Gould)
To: redax(at)agria(dot)hu (Zsolt Varga)
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] my strftime func doesn't work. please help.
Date: 1998-06-13 20:49:50
Message-ID: 9806132049.AA07001@hawk.illustra.com (view raw or flat)
Thread:
Lists: pgsql-hackers
Zsolt Varga writes:
> I tried to make a function, like strftime in C
> called pstrtime( format, date )
> 
> here's the source... but when I compile it and create the function
> under 6.3.2 first time it gives back an emty string.. after the backend 
> stops ;(
> 
> please correct my faults,
> ----------------------------------------------------------------------
> #include <stdio.h>                              /* for sprintf() */
> #include <string.h>
> #include <limits.h>
> #include "postgres.h"
> #include "miscadmin.h"
> #include "utils/builtins.h"
> #include "utils/nabstime.h"
> #include "utils/datetime.h"
> #include "access/xact.h"
> 
> #define JDATE_2000      2451545
> 
> char       *pstrtime(char *format, DateADT val);
> 
> 
> char *
> pstrtime( char *format , DateADT val)
> {
>         int                     year,
>                                 month,
>                                 day;
>         struct tm * time1;
>         char *sometext;
>         sometext = malloc(100);
>         time1 = malloc(sizeof(struct tm));
> 
>         j2date(val + JDATE_2000, &year, &month, &day);
> 
>         time1->tm_year=year;
>         time1->tm_mon=month-1;
>         time1->tm_mday=day;
> 
>         strftime(sometext,90,format,time1 );
>         free(time1);
>         return( sometext );
> }


I don't see what is causing your failure, perhaps you might want to test
it under gdb. But, you are using malloc() way too much which will make this
quite slow and wastful of memory. Also, you probably should be using palloc()
not malloc() or it will cause the backend to leak memory. For example:

char *
pstrtime( char *format , DateADT val)
{
	int 	year,
		month,
		day;
	struct tm time1;
	char	buf[256];
	char	*result;

	j2date(val + JDATE_2000, &year, &month, &day);
	memset(time1, 0,  sizeof(time1));
	time1.tm_year = year;
	time1.tm_mon = month-1;
	time1.tm_mday = day;

	strftime(buf, 90, format, &time1);

	result = palloc(1 + strlen(buf));
	if (result)
	   strcpy(result, buf, sizeof(buf))
	return result;
}

-dg

David Gould           dg(at)illustra(dot)com            510.628.3783 or 510.305.9468
Informix Software                      300 Lakeside Drive   Oakland, CA 94612
 - A child of five could understand this!  Fetch me a child of five.

In response to

pgsql-hackers by date

Next:From: Edwin S. RamirezDate: 1998-06-13 21:30:19
Subject: PL/Perl
Previous:From: Bruce MomjianDate: 1998-06-13 20:26:30
Subject: Re: [HACKERS] [QUESTIONS] builtin lo_unlink(int4)? why int4 not oid?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group