Skip to content

Commit 625d4b3

Browse files
committed
Let initdb detect the date order of the lc_time locale and initialize the
datestyle parameter of the new cluster accordingly.
1 parent cd8f3ec commit 625d4b3

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

doc/TODO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ SQL Commands
566566
Clients
567567
=======
568568

569-
* Have initdb set the input DateStyle (MDY or DMY) based on locale?
569+
* -Have initdb set the input DateStyle (MDY or DMY) based on locale
570570
* Have pg_ctl look at PGHOST in case it is a socket directory?
571571
* Allow pg_ctl to work properly with configuration files located outside
572572
the PGDATA directory

doc/src/sgml/config.sgml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.37 2005/11/17 22:14:50 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.38 2005/12/09 15:51:13 petere Exp $
33
-->
44
<chapter Id="runtime-config">
55
<title>Server Configuration</title>
@@ -32,7 +32,7 @@ $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.37 2005/11/17 22:14:50 tgl Exp $
3232
<para>
3333
One way to set these parameters is to edit the file
3434
<filename>postgresql.conf</><indexterm><primary>postgresql.conf</></>,
35-
which is normally kept in the data directory. (<command>initdb</>
35+
which is normally kept in the data directory. (<application>initdb</>
3636
installs a default copy there.) An example of what this file might look
3737
like is:
3838
<programlisting>
@@ -3300,7 +3300,10 @@ SELECT * FROM parent WHERE key = 2400;
33003300
keywords <literal>US</>, <literal>NonEuro</>, and
33013301
<literal>NonEuropean</> are synonyms for <literal>MDY</>. See
33023302
<xref linkend="datatype-datetime"> for more information. The
3303-
default is <literal>ISO, MDY</>.
3303+
built-in default is <literal>ISO, MDY</>, but
3304+
<application>initdb</application> will initialize the
3305+
configuration file with a setting that corresponds to the
3306+
behavior of the chosen <varname>lc_time</varname> locale.
33043307
</para>
33053308
</listitem>
33063309
</varlistentry>

src/bin/initdb/initdb.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
* Portions taken from FreeBSD.
4444
*
45-
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.100 2005/11/22 18:17:28 momjian Exp $
45+
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.101 2005/12/09 15:51:14 petere Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -57,11 +57,13 @@
5757
#ifdef HAVE_LANGINFO_H
5858
#include <langinfo.h>
5959
#endif
60+
#include <time.h>
6061

6162
#include "libpq/pqsignal.h"
6263
#include "mb/pg_wchar.h"
6364
#include "getaddrinfo.h"
6465
#include "getopt_long.h"
66+
#include "miscadmin.h"
6567

6668
#ifndef HAVE_INT_OPTRESET
6769
int optreset;
@@ -186,6 +188,7 @@ static void make_postgres(void);
186188
static void trapsig(int signum);
187189
static void check_ok(void);
188190
static char *escape_quotes(const char *src);
191+
static int locale_date_order(const char *locale);
189192
static bool chklocale(const char *locale);
190193
static void setlocales(void);
191194
static void usage(const char *progname);
@@ -1195,6 +1198,20 @@ setup_config(void)
11951198
snprintf(repltok, sizeof(repltok), "lc_time = '%s'", lc_time);
11961199
conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
11971200

1201+
switch (locale_date_order(lc_time)) {
1202+
case DATEORDER_YMD:
1203+
strcpy(repltok, "datestyle = 'iso, ymd'");
1204+
break;
1205+
case DATEORDER_DMY:
1206+
strcpy(repltok, "datestyle = 'iso, dmy'");
1207+
break;
1208+
case DATEORDER_MDY:
1209+
default:
1210+
strcpy(repltok, "datestyle = 'iso, mdy'");
1211+
break;
1212+
}
1213+
conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1214+
11981215
snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
11991216

12001217
writefile(path, conflines);
@@ -2052,6 +2069,60 @@ escape_quotes(const char *src)
20522069
return result;
20532070
}
20542071

2072+
/*
2073+
* Determine likely date order from locale
2074+
*/
2075+
static int
2076+
locale_date_order(const char *locale)
2077+
{
2078+
struct tm testtime;
2079+
char buf[128];
2080+
char *posD;
2081+
char *posM;
2082+
char *posY;
2083+
char *save;
2084+
size_t res;
2085+
int result;
2086+
2087+
result = DATEORDER_MDY; /* default */
2088+
2089+
save = setlocale(LC_TIME, NULL);
2090+
if (!save)
2091+
return result;
2092+
save = xstrdup(save);
2093+
2094+
setlocale(LC_TIME, locale);
2095+
2096+
memset(&testtime, 0, sizeof(testtime));
2097+
testtime.tm_mday = 22;
2098+
testtime.tm_mon = 10; /* November, should come out as "11" */
2099+
testtime.tm_year = 133; /* 2033 */
2100+
2101+
res = strftime(buf, sizeof(buf), "%x", &testtime);
2102+
2103+
setlocale(LC_TIME, save);
2104+
free(save);
2105+
2106+
if (res == 0)
2107+
return result;
2108+
2109+
posM = strstr(buf, "11");
2110+
posD = strstr(buf, "22");
2111+
posY = strstr(buf, "33");
2112+
2113+
if (!posM || !posD || !posY)
2114+
return result;
2115+
2116+
if (posY < posM && posM < posD)
2117+
result = DATEORDER_YMD;
2118+
else if (posD < posM)
2119+
result = DATEORDER_DMY;
2120+
else
2121+
result = DATEORDER_MDY;
2122+
2123+
return result;
2124+
}
2125+
20552126
/*
20562127
* check if given string is a valid locale specifier
20572128
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy