1
1
/* -----------------------------------------------------------------------
2
2
* formatting.c
3
3
*
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 $
5
5
*
6
6
*
7
7
* Portions Copyright (c) 1999-2000, PostgreSQL, Inc
@@ -1513,6 +1513,8 @@ dch_global(int arg, char *inout, int suf, int flag, FormatNode *node)
1513
1513
return -1 ;
1514
1514
}
1515
1515
1516
+ #define AMPM_ERROR elog(ERROR, "to_timestamp(): bad AM/PM string")
1517
+
1516
1518
/* ----------
1517
1519
* Master function of TIME for:
1518
1520
* TO_CHAR- write (inout) formated string
@@ -1531,59 +1533,115 @@ dch_time(int arg, char *inout, int suf, int flag, FormatNode *node)
1531
1533
case DCH_P_M :
1532
1534
if (flag == TO_CHAR )
1533
1535
{
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 ));
1535
1538
return 3 ;
1536
1539
1537
1540
}
1538
1541
else if (flag == FROM_CHAR )
1539
1542
{
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 ;
1542
1558
return 3 ;
1543
1559
}
1544
1560
1545
1561
case DCH_AM :
1546
1562
case DCH_PM :
1547
1563
if (flag == TO_CHAR )
1548
1564
{
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 ));
1550
1567
return 1 ;
1551
1568
1552
1569
}
1553
1570
else if (flag == FROM_CHAR )
1554
1571
{
1555
- if (strncmp (inout ,PM_STR ,2 )== 0 && tm -> tm_hour < 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 ;
1557
1587
return 1 ;
1558
1588
}
1559
1589
1560
1590
case DCH_a_m :
1561
1591
case DCH_p_m :
1562
1592
if (flag == TO_CHAR )
1563
1593
{
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 ));
1565
1596
return 3 ;
1566
1597
1567
1598
}
1568
1599
else if (flag == FROM_CHAR )
1569
1600
{
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 ;
1572
1616
return 3 ;
1573
1617
}
1574
1618
1575
1619
case DCH_am :
1576
1620
case DCH_pm :
1577
1621
if (flag == TO_CHAR )
1578
1622
{
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 ));
1580
1625
return 1 ;
1581
1626
1582
1627
}
1583
1628
else if (flag == FROM_CHAR )
1584
1629
{
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 ;
1587
1645
return 1 ;
1588
1646
}
1589
1647