Re: [evolution-patches] g_strftime



oops, forgot to save my emacs buffer before attaching. lets try this
again.

Jeff

On Fri, 2003-05-23 at 18:13, Jeffrey Stedfast wrote:
> an idea for implementing a workable strftime implementation that returns
> a malloc'd buffer. also works around the fact that strftime is Broken By
> Design (tm).
> 
> Jeff
-- 
Jeffrey Stedfast
Evolution Hacker - Ximian, Inc.
fejj ximian com  - www.ximian.com
char *
g_strftime (const char *fmt, const struct tm *tm)
{
	const char *start, *inptr;
	char *time_str, *buf;
	size_t max, n;
	GString *str;
	char fmt[4];
	
	fmt[0] = '%';
	fmt[2] = ' ';
	fmt[3] = '\0';
	
	max = 64;
	buf = g_malloc (buflen);
	
	str = g_string_new ("");
	
	inptr = format;
	while (*inptr) {
		start = inptr;
		
		while (*inptr != '%')
			inptr++;
		
		g_string_append_len (str, start, inptr - start);
		
		inptr++;
		if (*inptr == '%') {
			g_string_append_c (str, *inptr++);
		} else {
			fmt[1] = *inptr++;
			
		retry:
			if ((n = strftime (buf, max, fmt, tm)) != 0) {
				g_string_append_len (str, buf, n - 1);
			} else {
				max <<= 1;
				buf = g_realloc (buf, max);
				goto retry;
			}
		}
	}
	
	g_free (buf);
	
	time_str = str->str;
	g_string_free (str, FALSE);
	
	return time_str;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]