aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/formatting.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2010-04-07 21:41:53 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2010-04-07 21:41:53 +0000
commit2c0870ff7ab9bda44f39dd332836d0928afb2297 (patch)
treeb29685c89634a3ae441fee5cfaa7e7317bfb7394 /src/backend/utils/adt/formatting.c
parent0f11ed5886ce797d816177f5a915a22d85dd7400 (diff)
downloadpostgresql-2c0870ff7ab9bda44f39dd332836d0928afb2297.tar.gz
postgresql-2c0870ff7ab9bda44f39dd332836d0928afb2297.zip
Fix to_char YYY, YY, Y format codes so that FM zero-suppression really works,
rather than only sort-of working as the previous attempt had left it. Clean up some unnecessary differences between the way these were coded and the way the YYYY case was coded. Update the regression test cases that proved that it wasn't working.
Diffstat (limited to 'src/backend/utils/adt/formatting.c')
-rw-r--r--src/backend/utils/adt/formatting.c65
1 files changed, 26 insertions, 39 deletions
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index cc4f8137db0..e83a1d90141 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -1,7 +1,7 @@
/* -----------------------------------------------------------------------
* formatting.c
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.169 2010/03/03 22:28:42 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.170 2010/04/07 21:41:53 tgl Exp $
*
*
* Portions Copyright (c) 1999-2010, PostgreSQL Global Development Group
@@ -2044,7 +2044,6 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out)
FormatNode *n;
char *s;
struct pg_tm *tm = &in->tm;
- char buff[DCH_CACHE_SIZE];
int i;
/* cache localized days and months */
@@ -2394,67 +2393,55 @@ DCH_to_char(FormatNode *node, bool is_interval, TmToChar *in, char *out)
break;
case DCH_YYYY:
case DCH_IYYY:
- if (tm->tm_year <= 9999 && tm->tm_year >= -9998)
- sprintf(s, "%0*d",
- S_FM(n->suffix) ? 0 : 4,
- n->key->id == DCH_YYYY ?
- ADJUST_YEAR(tm->tm_year, is_interval) :
- ADJUST_YEAR(date2isoyear(
- tm->tm_year,
- tm->tm_mon,
- tm->tm_mday), is_interval));
- else
- sprintf(s, "%d",
- n->key->id == DCH_YYYY ?
- ADJUST_YEAR(tm->tm_year, is_interval) :
- ADJUST_YEAR(date2isoyear(
- tm->tm_year,
- tm->tm_mon,
- tm->tm_mday), is_interval));
+ sprintf(s, "%0*d",
+ S_FM(n->suffix) ? 0 : 4,
+ (n->key->id == DCH_YYYY ?
+ ADJUST_YEAR(tm->tm_year, is_interval) :
+ ADJUST_YEAR(date2isoyear(tm->tm_year,
+ tm->tm_mon,
+ tm->tm_mday),
+ is_interval)));
if (S_THth(n->suffix))
str_numth(s, s, S_TH_TYPE(n->suffix));
s += strlen(s);
break;
case DCH_YYY:
case DCH_IYY:
- snprintf(buff, sizeof(buff), "%0*d",
- S_FM(n->suffix) ? 0 : 3,
- n->key->id == DCH_YYY ?
+ sprintf(s, "%0*d",
+ S_FM(n->suffix) ? 0 : 3,
+ (n->key->id == DCH_YYY ?
ADJUST_YEAR(tm->tm_year, is_interval) :
ADJUST_YEAR(date2isoyear(tm->tm_year,
- tm->tm_mon, tm->tm_mday),
- is_interval));
- i = strlen(buff);
- strcpy(s, buff + (i > 3 ? i - 3 : 0));
+ tm->tm_mon,
+ tm->tm_mday),
+ is_interval)) % 1000);
if (S_THth(n->suffix))
str_numth(s, s, S_TH_TYPE(n->suffix));
s += strlen(s);
break;
case DCH_YY:
case DCH_IY:
- snprintf(buff, sizeof(buff), "%0*d",
- S_FM(n->suffix) ? 0 : 2,
- n->key->id == DCH_YY ?
+ sprintf(s, "%0*d",
+ S_FM(n->suffix) ? 0 : 2,
+ (n->key->id == DCH_YY ?
ADJUST_YEAR(tm->tm_year, is_interval) :
ADJUST_YEAR(date2isoyear(tm->tm_year,
- tm->tm_mon, tm->tm_mday),
- is_interval));
- i = strlen(buff);
- strcpy(s, buff + (i > 2 ? i - 2 : 0));
+ tm->tm_mon,
+ tm->tm_mday),
+ is_interval)) % 100);
if (S_THth(n->suffix))
str_numth(s, s, S_TH_TYPE(n->suffix));
s += strlen(s);
break;
case DCH_Y:
case DCH_I:
- snprintf(buff, sizeof(buff), "%1d",
- n->key->id == DCH_Y ?
+ sprintf(s, "%1d",
+ (n->key->id == DCH_Y ?
ADJUST_YEAR(tm->tm_year, is_interval) :
ADJUST_YEAR(date2isoyear(tm->tm_year,
- tm->tm_mon, tm->tm_mday),
- is_interval));
- i = strlen(buff);
- strcpy(s, buff + (i > 1 ? i - 1 : 0));
+ tm->tm_mon,
+ tm->tm_mday),
+ is_interval)) % 10);
if (S_THth(n->suffix))
str_numth(s, s, S_TH_TYPE(n->suffix));
s += strlen(s);