Let initdb detect the date order of the lc_time locale and initialize the
authorPeter Eisentraut
Fri, 9 Dec 2005 15:51:14 +0000 (15:51 +0000)
committerPeter Eisentraut
Fri, 9 Dec 2005 15:51:14 +0000 (15:51 +0000)
datestyle parameter of the new cluster accordingly.

doc/TODO
doc/src/sgml/config.sgml
src/bin/initdb/initdb.c

index 5e8c8ebff3c246a7a6a943d22c7995750ffeb6cb..a18f2e67e47e8bd20bdbe06de61ccacf7204ea96 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -566,7 +566,7 @@ SQL Commands
 Clients
 =======
 
-* Have initdb set the input DateStyle (MDY or DMY) based on locale?
+* -Have initdb set the input DateStyle (MDY or DMY) based on locale
 * Have pg_ctl look at PGHOST in case it is a socket directory?
 * Allow pg_ctl to work properly with configuration files located outside
   the PGDATA directory
index 89dc122327cd2f37a3970bab9cc807c0155361d9..2d1e50818346cd54c5a0fa92a54b6f47de7dbc92 100644 (file)
@@ -1,5 +1,5 @@
 
 
   Server Configuration
@@ -32,7 +32,7 @@ $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.37 2005/11/17 22:14:50 tgl Exp $
    
     One way to set these parameters is to edit the file
     postgresql.confpostgresql.conf,
-    which is normally kept in the data directory. (<command>initdb
+    which is normally kept in the data directory. (<application>initdb
     installs a default copy there.) An example of what this file might look
     like is:
 
@@ -3300,7 +3300,10 @@ SELECT * FROM parent WHERE key = 2400;
         keywords US, NonEuro, and
         NonEuropean are synonyms for MDY. See
          for more information. The
-        default is ISO, MDY.
+        built-in default is ISO, MDY, but
+        initdb will initialize the
+        configuration file with a setting that corresponds to the
+        behavior of the chosen lc_time locale.
        
       
      
index ad9e4b6027c28f1b01276201242b48c371fc3ac7..41885184136a0f59693f83251d24b6f794bc47f7 100644 (file)
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.100 2005/11/22 18:17:28 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.101 2005/12/09 15:51:14 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
 #ifdef HAVE_LANGINFO_H
 #include 
 #endif
+#include 
 
 #include "libpq/pqsignal.h"
 #include "mb/pg_wchar.h"
 #include "getaddrinfo.h"
 #include "getopt_long.h"
+#include "miscadmin.h"
 
 #ifndef HAVE_INT_OPTRESET
 int            optreset;
@@ -186,6 +188,7 @@ static void make_postgres(void);
 static void trapsig(int signum);
 static void check_ok(void);
 static char *escape_quotes(const char *src);
+static int locale_date_order(const char *locale);
 static bool chklocale(const char *locale);
 static void setlocales(void);
 static void usage(const char *progname);
@@ -1195,6 +1198,20 @@ setup_config(void)
    snprintf(repltok, sizeof(repltok), "lc_time = '%s'", lc_time);
    conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
 
+   switch (locale_date_order(lc_time)) {
+       case DATEORDER_YMD:
+           strcpy(repltok, "datestyle = 'iso, ymd'");
+           break;
+       case DATEORDER_DMY:
+           strcpy(repltok, "datestyle = 'iso, dmy'");
+           break;
+       case DATEORDER_MDY:
+       default:
+           strcpy(repltok, "datestyle = 'iso, mdy'");
+           break;
+   }
+   conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
+
    snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
 
    writefile(path, conflines);
@@ -2052,6 +2069,60 @@ escape_quotes(const char *src)
    return result;
 }
 
+/*
+ * Determine likely date order from locale
+ */
+static int
+locale_date_order(const char *locale)
+{
+   struct tm   testtime;
+   char        buf[128];
+   char       *posD;
+   char       *posM;
+   char       *posY;
+   char       *save;
+   size_t      res;
+   int         result;
+
+   result = DATEORDER_MDY;     /* default */
+
+   save = setlocale(LC_TIME, NULL);
+   if (!save)
+       return result;
+   save = xstrdup(save);
+
+   setlocale(LC_TIME, locale);
+
+   memset(&testtime, 0, sizeof(testtime));
+   testtime.tm_mday = 22;
+   testtime.tm_mon = 10;       /* November, should come out as "11" */
+   testtime.tm_year = 133;     /* 2033 */
+
+   res = strftime(buf, sizeof(buf), "%x", &testtime);
+
+   setlocale(LC_TIME, save);
+   free(save);
+
+   if (res == 0)
+       return result;
+
+   posM = strstr(buf, "11");
+   posD = strstr(buf, "22");
+   posY = strstr(buf, "33");
+
+   if (!posM || !posD || !posY)
+       return result;
+
+   if (posY < posM && posM < posD)
+       result = DATEORDER_YMD;
+   else if (posD < posM)
+       result = DATEORDER_DMY;
+   else
+       result = DATEORDER_MDY;
+
+   return result;
+}
+
 /*
  * check if given string is a valid locale specifier
  */