/* -----------------------------------------------------------------------
* formatting.c
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.114 2006/11/24 15:26:18 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.115 2006/11/24 22:25:56 momjian Exp $
*
*
* Portions Copyright (c) 1999-2006, PostgreSQL Global Development Group
#include
#include
#include
-#include <time.h>
+#include <locale.h>
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/numeric.h"
#include "utils/pg_locale.h"
+#define _(x) gettext((x))
+
/* ----------
* Routines type
* ----------
/* ----------
* Full months
+ * This needs to be NLS-localized someday.
* ----------
*/
static char *months_full[] = {
static NUMCacheEntry *NUM_cache_getnew(char *str);
static void NUM_cache_remove(NUMCacheEntry *ent);
+static char *localize_month_full(int index);
+static char *localize_month(int index);
+static char *localize_day_full(int index);
+static char *localize_day(int index);
/* ----------
* Fast sequential search, use index for data selection which
struct pg_tm *tm = NULL;
TmFromChar *tmfc = NULL;
TmToChar *tmtc = NULL;
- char *save_loc = NULL;
-
- /*
- * Set the LC_TIME only to do some operation (strftime) and then
- * set it back. See pg_locale.c for explanations.
- */
- if (S_TM(suf))
- {
- save_loc = setlocale(LC_TIME, NULL);
- setlocale(LC_TIME, locale_time);
- }
if (is_to_char)
{
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(workbuff, sizeof(workbuff), "%B", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(workbuff, localize_month_full(tm->tm_mon - 1));
else
- {
strcpy(workbuff, months_full[tm->tm_mon - 1]);
- }
sprintf(inout, "%*s", (S_FM(suf) || S_TM(suf)) ? 0 : -9, str_toupper(workbuff));
return strlen(p_inout);
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(inout, 32, "%B", (struct tm *) tm);
- /* capitalize output */
- inout[0] = pg_toupper((unsigned char) inout[0]);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ sprintf(inout, "%*s", 0, localize_month_full(tm->tm_mon - 1));
else
- {
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, months_full[tm->tm_mon - 1]);
- }
return strlen(p_inout);
case DCH_month:
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(inout, 32, "%B", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ sprintf(inout, "%*s", 0, localize_month_full(tm->tm_mon - 1));
else
- {
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, months_full[tm->tm_mon - 1]);
- }
*inout = pg_tolower((unsigned char) *inout);
return strlen(p_inout);
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(inout, 32, "%b", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_month(tm->tm_mon - 1));
else
- {
strcpy(inout, months[tm->tm_mon - 1]);
- }
str_toupper(inout);
return strlen(p_inout);
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(inout, 32, "%b", (struct tm *) tm);
- /* capitalize output */
- inout[0] = pg_toupper((unsigned char) inout[0]);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_month(tm->tm_mon - 1));
else
- {
strcpy(inout, months[tm->tm_mon - 1]);
- }
return strlen(p_inout);
case DCH_mon:
if (!tm->tm_mon)
return -1;
if (S_TM(suf))
- {
- /*
- * tm_mon in 'pg_tm struct' based on one, but rather POSIX 'tm struct' based on zero.
- * See notes at the top of this file.
- */
- tm->tm_mon = tm->tm_mon - 1;
- strftime(inout, 32, "%b", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_month(tm->tm_mon - 1));
else
- {
strcpy(inout, months[tm->tm_mon - 1]);
- }
*inout = pg_tolower((unsigned char) *inout);
return strlen(p_inout);
case DCH_DAY:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(workbuff, sizeof(workbuff), "%A", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(workbuff, localize_day_full(tm->tm_wday));
else
- {
strcpy(workbuff, days[tm->tm_wday]);
- }
sprintf(inout, "%*s", (S_FM(suf) || S_TM(suf)) ? 0 : -9, str_toupper(workbuff));
return strlen(p_inout);
case DCH_Day:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(inout, 32, "%A", (struct tm *) tm);
- /* capitalize output */
- inout[0] = pg_toupper((unsigned char) inout[0]);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ sprintf(inout, "%*s", 0, localize_day_full(tm->tm_wday));
else
- {
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, days[tm->tm_wday]);
- }
return strlen(p_inout);
case DCH_day:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(inout, 32, "%A", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ sprintf(inout, "%*s", 0, localize_day_full(tm->tm_wday));
else
- {
sprintf(inout, "%*s", S_FM(suf) ? 0 : -9, days[tm->tm_wday]);
- }
*inout = pg_tolower((unsigned char) *inout);
return strlen(p_inout);
case DCH_DY:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(inout, 32, "%a", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_day(tm->tm_wday));
else
- {
strcpy(inout, days_short[tm->tm_wday]);
- }
str_toupper(inout);
return strlen(p_inout);
case DCH_Dy:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(inout, 32, "%a", (struct tm *) tm);
- /* capitalize output */
- inout[0] = pg_toupper((unsigned char) inout[0]);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_day(tm->tm_wday));
else
- {
strcpy(inout, days_short[tm->tm_wday]);
- }
return strlen(p_inout);
case DCH_dy:
INVALID_FOR_INTERVAL;
if (S_TM(suf))
- {
- strftime(inout, 32, "%a", (struct tm *) tm);
- /* set it back; see comments in pg_locale.c */
- setlocale(LC_TIME, save_loc);
- }
+ strcpy(inout, localize_day(tm->tm_wday));
else
- {
strcpy(inout, days_short[tm->tm_wday]);
- }
*inout = pg_tolower((unsigned char) *inout);
return strlen(p_inout);
return res;
}
+static char *
+localize_month_full(int index)
+{
+ char *m = NULL;
+
+ switch (index)
+ {
+ case 0:
+ m = _("January");
+ break;
+ case 1:
+ m = _("February");
+ break;
+ case 2:
+ m = _("March");
+ break;
+ case 3:
+ m = _("April");
+ break;
+ case 4:
+ m = _("May");
+ break;
+ case 5:
+ m = _("June");
+ break;
+ case 6:
+ m = _("July");
+ break;
+ case 7:
+ m = _("August");
+ break;
+ case 8:
+ m = _("September");
+ break;
+ case 9:
+ m = _("October");
+ break;
+ case 10:
+ m = _("November");
+ break;
+ case 11:
+ m = _("December");
+ break;
+ }
+
+ return m;
+}
+
+static char *
+localize_month(int index)
+{
+ char *m = NULL;
+
+ switch (index)
+ {
+ case 0:
+ m = _("Jan");
+ break;
+ case 1:
+ m = _("Feb");
+ break;
+ case 2:
+ m = _("Mar");
+ break;
+ case 3:
+ m = _("Apr");
+ break;
+ case 4:
+ m = _("May");
+ break;
+ case 5:
+ m = _("Jun");
+ break;
+ case 6:
+ m = _("Jul");
+ break;
+ case 7:
+ m = _("Aug");
+ break;
+ case 8:
+ m = _("Sep");
+ break;
+ case 9:
+ m = _("Oct");
+ break;
+ case 10:
+ m = _("Nov");
+ break;
+ case 11:
+ m = _("Dec");
+ break;
+ }
+
+ return m;
+}
+
+static char *
+localize_day_full(int index)
+{
+ char *d = NULL;
+
+ switch (index)
+ {
+ case 0:
+ d = _("Sunday");
+ break;
+ case 1:
+ d = _("Monday");
+ break;
+ case 2:
+ d = _("Tuesday");
+ break;
+ case 3:
+ d = _("Wednesday");
+ break;
+ case 4:
+ d = _("Thursday");
+ break;
+ case 5:
+ d = _("Friday");
+ break;
+ case 6:
+ d = _("Saturday");
+ break;
+ }
+
+ return d;
+}
+
+static char *
+localize_day(int index)
+{
+ char *d = NULL;
+
+ switch (index)
+ {
+ case 0:
+ d = _("Sun");
+ break;
+ case 1:
+ d = _("Mon");
+ break;
+ case 2:
+ d = _("Tue");
+ break;
+ case 3:
+ d = _("Wed");
+ break;
+ case 4:
+ d = _("Thu");
+ break;
+ case 5:
+ d = _("Fri");
+ break;
+ case 6:
+ d = _("Sat");
+ break;
+ }
+
+ return d;
+}
/****************************************************************************
* Public routines