*** ./src/backend/commands/variable.c.orig	2010-02-14 19:42:14.000000000 +0100
--- ./src/backend/commands/variable.c	2010-05-31 16:54:34.934729768 +0200
***************
*** 154,159 ****
--- 154,166 ----
  			/* when doit is true */
  			free((char *) subval);
  		}
+ 		else if (pg_strcasecmp(tok, "CUSTOM") == 0)
+ 		{
+ 			if (have_style && newDateStyle != USE_CUSTOM_DATES)
+ 				ok = false;		/* conflicting styles */
+ 			newDateStyle = USE_CUSTOM_DATES;
+ 			have_style = true;
+ 		}
  		else
  		{
  			ereport(GUC_complaint_elevel(source),
***************
*** 200,205 ****
--- 207,215 ----
  		case USE_GERMAN_DATES:
  			strcpy(result, "German");
  			break;
+ 		case USE_CUSTOM_DATES:
+ 			strcpy(result, "Custom");
+ 			break;
  		default:
  			strcpy(result, "Postgres");
  			break;
*** ./src/backend/utils/adt/date.c.orig	2010-02-26 03:01:07.000000000 +0100
--- ./src/backend/utils/adt/date.c	2010-06-02 11:59:16.260024688 +0200
***************
*** 27,32 ****
--- 27,33 ----
  #include "utils/array.h"
  #include "utils/builtins.h"
  #include "utils/date.h"
+ #include "utils/formatting.h"
  #include "utils/nabstime.h"
  
  /*
***************
*** 45,50 ****
--- 46,54 ----
  static int	tm2timetz(struct pg_tm * tm, fsec_t fsec, int tz, TimeTzADT *result);
  static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
  
+ extern char *custom_datestyle_format;
+ 
+ 
  
  /* common code for timetypmodin and timetztypmodin */
  static int32
***************
*** 183,198 ****
  			   *tm = &tt;
  	char		buf[MAXDATELEN + 1];
  
! 	if (DATE_NOT_FINITE(date))
! 		EncodeSpecialDate(date, buf);
  	else
  	{
! 		j2date(date + POSTGRES_EPOCH_JDATE,
! 			   &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
! 		EncodeDateOnly(tm, DateStyle, buf);
  	}
  
- 	result = pstrdup(buf);
  	PG_RETURN_CSTRING(result);
  }
  
--- 187,221 ----
  			   *tm = &tt;
  	char		buf[MAXDATELEN + 1];
  
! 	if (DateStyle == USE_CUSTOM_DATES)
! 	{
! 		Timestamp dt;
! 		text	*fmt;
! 
! 		if (custom_datestyle_format == NULL || strcmp(custom_datestyle_format,"") == 0)
! 			elog(ERROR, "custom_datestyle_format is empty");
! 		
! 		dt = DirectFunctionCall1(date_timestamp, DateADTGetDatum(date));
! 		fmt = cstring_to_text(custom_datestyle_format);
! 		result  = text_to_cstring(DatumGetTextP(DirectFunctionCall2(timestamp_to_char,
! 										TimestampGetDatum(dt),
! 										PointerGetDatum(fmt))));
! 	}
  	else
  	{
! 		/* use predefined style */
! 		if (DATE_NOT_FINITE(date))
! 			EncodeSpecialDate(date, buf);
! 		else
! 		{
! 			j2date(date + POSTGRES_EPOCH_JDATE,
! 				   &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
! 			EncodeDateOnly(tm, DateStyle, buf);
! 		}
! 		
! 		result = pstrdup(buf);
  	}
  
  	PG_RETURN_CSTRING(result);
  }
  
*** ./src/backend/utils/adt/datetime.c.orig	2010-05-09 04:15:59.000000000 +0200
--- ./src/backend/utils/adt/datetime.c	2010-05-31 17:42:42.384729881 +0200
***************
*** 52,57 ****
--- 52,58 ----
  static void AdjustFractDays(double frac, struct pg_tm * tm, fsec_t *fsec,
  				int scale);
  
+ char *custom_datestyle_format = NULL;
  
  const int	day_tab[2][13] =
  {
*** ./src/backend/utils/misc/guc.c.orig	2010-05-02 04:10:33.000000000 +0200
--- ./src/backend/utils/misc/guc.c	2010-05-31 17:42:37.016854897 +0200
***************
*** 118,123 ****
--- 118,124 ----
  extern bool fullPageWrites;
  extern int	vacuum_defer_cleanup_age;
  extern int	ssl_renegotiation_limit;
+ extern char *custom_datestyle_format;
  
  int			trace_recovery_messages = LOG;
  
***************
*** 2539,2544 ****
--- 2540,2554 ----
  	},
  
  	{
+ 		{"custom_datestyle_format", PGC_USERSET, CLIENT_CONN_STATEMENT,
+ 			gettext_noop("Sets custom format for date "),
+ 			NULL
+ 		},
+ 		&custom_datestyle_format,
+ 		"", NULL, NULL
+ 	},
+ 
+ 	{
  		{"custom_variable_classes", PGC_SIGHUP, CUSTOM_OPTIONS,
  			gettext_noop("Sets the list of known custom variable classes."),
  			NULL,
*** ./src/include/miscadmin.h.orig	2010-02-26 03:01:20.000000000 +0100
--- ./src/include/miscadmin.h	2010-05-31 16:45:54.299855276 +0200
***************
*** 184,189 ****
--- 184,190 ----
  #define USE_SQL_DATES			2
  #define USE_GERMAN_DATES		3
  #define USE_XSD_DATES			4
+ #define USE_CUSTOM_DATES		5
  
  /* valid DateOrder values */
  #define DATEORDER_YMD			0
