Commit 07a56067 authored by Tom Lane's avatar Tom Lane

Make to_char()'s localized month/day names depend on LC_TIME, not LC_MESSAGES.

Euler Taveira de Oliveira
parent 63e98b55
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.178 2008/05/15 00:17:39 tgl Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.179 2008/05/19 18:08:15 tgl Exp $ -->
<chapter Id="runtime-config">
<title>Server Configuration</title>
......@@ -4184,9 +4184,9 @@ SET XML OPTION { DOCUMENT | CONTENT };
</indexterm>
<listitem>
<para>
Sets the locale to use for formatting date and time values.
(Currently, this setting does nothing, but it might in the
future.) Acceptable values are system-dependent; see <xref
Sets the locale to use for formatting dates and times, for example
with the <function>to_char</function> family of
functions. Acceptable values are system-dependent; see <xref
linkend="locale"> for more information. If this variable is
set to the empty string (which is the default) then the value
is inherited from the execution environment of the server in a
......
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.436 2008/05/04 23:19:23 tgl Exp $ -->
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.437 2008/05/19 18:08:15 tgl Exp $ -->
<chapter id="functions">
<title>Functions and Operators</title>
......@@ -5079,7 +5079,8 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
</row>
<row>
<entry><literal>TM</literal> prefix</entry>
<entry>translation mode (print localized day and month names based on <varname>lc_messages</>)</entry>
<entry>translation mode (print localized day and month names based on
<xref linkend="guc-lc-time">)</entry>
<entry><literal>TMMonth</literal></entry>
</row>
<row>
......
This diff is collapsed.
......@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.78 2008/03/25 22:42:44 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.79 2008/05/19 18:08:16 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -48,6 +48,8 @@
#define USE_WIDE_UPPER_LOWER
char *wstring_lower(char *str);
char *wstring_upper(char *str);
wchar_t *texttowcs(const text *txt);
text *wcstotext(const wchar_t *str, int ncodes);
#endif
static text *dotrim(const char *string, int stringlen,
......@@ -60,7 +62,7 @@ static text *dotrim(const char *string, int stringlen,
/*
* Convert a TEXT value into a palloc'd wchar string.
*/
static wchar_t *
wchar_t *
texttowcs(const text *txt)
{
int nbytes = VARSIZE_ANY_EXHDR(txt);
......@@ -112,7 +114,7 @@ texttowcs(const text *txt)
* must be zero-terminated, but we also require the caller to pass the string
* length, since it will know it anyway in current uses.
*/
static text *
text *
wcstotext(const wchar_t *str, int ncodes)
{
text *result;
......
......@@ -4,7 +4,7 @@
*
* Portions Copyright (c) 2002-2008, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/backend/utils/adt/pg_locale.c,v 1.40 2008/01/01 19:45:52 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/pg_locale.c,v 1.41 2008/05/19 18:08:16 tgl Exp $
*
*-----------------------------------------------------------------------
*/
......@@ -48,20 +48,31 @@
#include "postgres.h"
#include <locale.h>
#include <time.h>
#include "catalog/pg_control.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
/* GUC storage area */
#define MAX_L10N_DATA 80
/* GUC settings */
char *locale_messages;
char *locale_monetary;
char *locale_numeric;
char *locale_time;
/* lc_time localization cache */
char *localized_abbrev_days[7];
char *localized_full_days[7];
char *localized_abbrev_months[12];
char *localized_full_months[12];
/* indicates whether locale information cache is valid */
static bool CurrentLocaleConvValid = false;
static bool CurrentLCTimeValid = false;
/* Environment variable storage area */
......@@ -209,7 +220,10 @@ locale_xxx_assign(int category, const char *value, bool doit, GucSource source)
/* need to reload cache next time? */
if (doit && value != NULL)
{
CurrentLocaleConvValid = false;
CurrentLCTimeValid = false;
}
return value;
}
......@@ -424,3 +438,78 @@ PGLC_localeconv(void)
CurrentLocaleConvValid = true;
return &CurrentLocaleConv;
}
/*
* Update the lc_time localization cache variables if needed.
*/
void
cache_locale_time(void)
{
char *save_lc_time;
time_t timenow;
struct tm *timeinfo;
char buf[MAX_L10N_DATA];
char *ptr;
int i;
/* did we do this already? */
if (CurrentLCTimeValid)
return;
elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
/* set user's value of time locale */
save_lc_time = setlocale(LC_TIME, NULL);
if (save_lc_time)
save_lc_time = pstrdup(save_lc_time);
setlocale(LC_TIME, locale_time);
timenow = time(NULL);
timeinfo = localtime(&timenow);
/* localized days */
for (i = 0; i < 7; i++)
{
timeinfo->tm_wday = i;
strftime(buf, MAX_L10N_DATA, "%a", timeinfo);
ptr = MemoryContextStrdup(TopMemoryContext, buf);
if (localized_abbrev_days[i])
pfree(localized_abbrev_days[i]);
localized_abbrev_days[i] = ptr;
strftime(buf, MAX_L10N_DATA, "%A", timeinfo);
ptr = MemoryContextStrdup(TopMemoryContext, buf);
if (localized_full_days[i])
pfree(localized_full_days[i]);
localized_full_days[i] = ptr;
}
/* localized months */
for (i = 0; i < 12; i++)
{
timeinfo->tm_mon = i;
timeinfo->tm_mday = 1; /* make sure we don't have invalid date */
strftime(buf, MAX_L10N_DATA, "%b", timeinfo);
ptr = MemoryContextStrdup(TopMemoryContext, buf);
if (localized_abbrev_months[i])
pfree(localized_abbrev_months[i]);
localized_abbrev_months[i] = ptr;
strftime(buf, MAX_L10N_DATA, "%B", timeinfo);
ptr = MemoryContextStrdup(TopMemoryContext, buf);
if (localized_full_months[i])
pfree(localized_full_months[i]);
localized_full_months[i] = ptr;
}
/* try to restore internal settings */
if (save_lc_time)
{
setlocale(LC_TIME, save_lc_time);
pfree(save_lc_time);
}
CurrentLCTimeValid = true;
}
......@@ -2,7 +2,7 @@
*
* PostgreSQL locale utilities
*
* $PostgreSQL: pgsql/src/include/utils/pg_locale.h,v 1.24 2008/01/01 19:45:59 momjian Exp $
* $PostgreSQL: pgsql/src/include/utils/pg_locale.h,v 1.25 2008/05/19 18:08:16 tgl Exp $
*
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
*
......@@ -17,11 +17,19 @@
#include "utils/guc.h"
/* GUC settings */
extern char *locale_messages;
extern char *locale_monetary;
extern char *locale_numeric;
extern char *locale_time;
/* lc_time localization cache */
extern char *localized_abbrev_days[];
extern char *localized_full_days[];
extern char *localized_abbrev_months[];
extern char *localized_full_months[];
extern const char *locale_messages_assign(const char *value,
bool doit, GucSource source);
extern const char *locale_monetary_assign(const char *value,
......@@ -42,4 +50,6 @@ extern bool lc_ctype_is_c(void);
*/
extern struct lconv *PGLC_localeconv(void);
extern void cache_locale_time(void);
#endif /* _PG_LOCALE_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment