8000 Back-patch fix for AM/PM boundary problem in to_char(). · tomdcc/postgres@4093838 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4093838

Browse files
committed
Back-patch fix for AM/PM boundary problem in to_char().
Fix from Karel Zak, 10/18/00.
1 parent 1dfc377 commit 4093838

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

src/backend/utils/adt/formatting.c

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* -----------------------------------------------------------------------
22
* formatting.c
33
*
4-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.8 2000/04/12 17:15:49 momjian Exp $
4+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/formatting.c,v 1.8.2.1 2000/10/19 18:39:03 tgl Exp $
55
*
66
*
77
* Portions Copyright (c) 1999-2000, PostgreSQL, Inc
@@ -1513,6 +1513,8 @@ dch_global(int arg, char *inout, int suf, int flag, FormatNode *node)
15131513
return -1;
15141514
}
15151515

1516+
#define AMPM_ERROR elog(ERROR, "to_timestamp(): bad AM/PM string")
1517+
15161518
/* ----------
15171519
* Master function of TIME for:
15181520
* TO_CHAR - write (inout) formated string
@@ -1531,59 +1533,115 @@ dch_time(int arg, char *inout, int suf, int flag, FormatNode *node)
15311533
case DCH_P_M:
15321534
if (flag == TO_CHAR)
15331535
{
1534-
strcpy(inout, (tm->tm_hour > 13 ? P_M_STR : A_M_STR));
1536+
strcpy(inout, ((tm->tm_hour > 11
1537+
&& tm->tm_hour < 24) ? P_M_STR : A_M_STR));
15351538
return 3;
15361539

15371540
}
15381541
else if (flag == FROM_CHAR)
15391542
{
1540-
if (strncmp(inout, P_M_STR, 4) == 0 && tm->tm_hour < 13)
1541-
tm->tm_hour += 12;
1543+
if (tm->tm_hour < 1 || tm->tm_hour > 12)
1544+
elog(ERROR, "to_timestamp(): AM/PM hour must be between 1 and 12");
1545+
1546+
if (strncmp(inout, P_M_STR, 4) == 0)
1547+
{
1548+
if (tm->tm_hour < 12)
1549+
tm->tm_hour += 12;
1550+
}
1551+
else if (strncmp(inout, A_M_STR, 4) == 0)
1552+
{
1553+
if (tm->tm_hour == 12)
1554+
tm->tm_hour = 0;
1555+
}
1556+
else
1557+
AMPM_ERROR;
15421558
return 3;
15431559
}
15441560

15451561
case DCH_AM:
15461562
case DCH_PM:
15471563
if (flag == TO_CHAR)
15481564
{
1549-
strcpy(inout, (tm->tm_hour > 13 ? PM_STR : AM_STR));
1565+
strcpy(inout, ((tm->tm_hour > 11
1566+
&& tm->tm_hour < 24) ? PM_STR : AM_STR));
15501567
return 1;
15511568

15521569
}
15531570
else if (flag == FROM_CHAR)
15541571
{
1555-
if (strncmp(inout, PM_STR, 2) == 0 && tm->tm_hour < 10000 13)
1556-
tm->tm_hour += 12;
1572+
if (tm->tm_hour < 1 || tm->tm_hour > 12)
1573+
elog(ERROR, "to_timestamp(): AM/PM hour must be between 1 and 12");
1574+
1575+
if (strncmp(inout, PM_STR, 4) == 0)
1576+
{
1577+
if (tm->tm_hour < 12)
1578+
tm->tm_hour += 12;
1579+
}
1580+
else if (strncmp(inout, AM_STR, 4) == 0)
1581+
{
1582+
if (tm->tm_hour == 12)
1583+
tm->tm_hour = 0;
1584+
}
1585+
else
1586+
AMPM_ERROR;
15571587
return 1;
15581588
}
15591589

15601590
case DCH_a_m:
15611591
case DCH_p_m:
15621592
if (flag == TO_CHAR)
15631593
{
1564-
strcpy(inout, (tm->tm_hour > 13 ? p_m_STR : a_m_STR));
1594+
strcpy(inout, ((tm->tm_hour > 11
1595+
&& tm->tm_hour < 24) ? p_m_STR : a_m_STR));
15651596
return 3;
15661597

15671598
}
15681599
else if (flag == FROM_CHAR)
15691600
{
1570-
if (strncmp(inout, p_m_STR, 4) == 0 && tm->tm_hour < 13)
1571-
tm->tm_hour += 12;
1601+
if (tm->tm_hour < 1 || tm->tm_hour > 12)
1602+
elog(ERROR, "to_timestamp(): AM/PM hour must be between 1 and 12");
1603+
1604+
if (strncmp(inout, p_m_STR, 4) == 0)
1605+
{
1606+
if (tm->tm_hour < 12)
1607+
tm->tm_hour += 12;
1608+
}
1609+
else if (strncmp(inout, a_m_STR, 4) == 0)
1610+
{
1611+
if (tm->tm_hour == 12)
1612+
tm->tm_hour = 0;
1613+
}
1614+
else
1615+
AMPM_ERROR;
15721616
return 3;
15731617
}
15741618

15751619
case DCH_am:
15761620
case DCH_pm:
15771621
if (flag == TO_CHAR)
15781622
{
1579-
strcpy(inout, (tm->tm_hour > 13 ? pm_STR : am_STR));
1623+
strcpy(inout, ((tm->tm_hour > 11
1624+
&& tm->tm_hour < 24) ? pm_STR : am_STR));
15801625
return 1;
15811626

15821627
}
15831628
else if (flag == FROM_CHAR)
15841629
{
1585-
if (strncmp(inout, pm_STR, 2) == 0 && tm->tm_hour < 13)
1586-
tm->tm_hour += 12;
1630+
if (tm->tm_hour < 1 || tm->tm_hour > 12)
1631+
elog(ERROR, "to_timestamp(): AM/PM hour must be between 1 and 12");
1632+
1633+
if (strncmp(inout, pm_STR, 4) == 0)
1634+
{
1635+
if (tm->tm_hour < 12)
1636+
tm->tm_hour += 12;
1637+
}
1638+
else if (strncmp(inout, am_STR, 4) == 0)
1639+
{
1640+
if (tm->tm_hour == 12)
1641+
tm->tm_hour = 0;
1642+
}
1643+
else
1644+
AMPM_ERROR;
15871645
return 1;
15881646
}
15891647

0 commit comments

Comments
 (0)
0