Skip to content

Commit f029932

Browse files
author
Michael Meskes
committed
Added PGTYPEStimestamp_add_interval written by Dave Cramer.
Fixed parsing of defines to make sure they used more than once.
1 parent f8ffb60 commit f029932

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

src/interfaces/ecpg/pgtypeslib/dt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,6 @@ extern char *pgtypes_date_weekdays_short[];
305305
extern char *pgtypes_date_months[];
306306
extern char *months[];
307307
extern char *days[];
308+
extern int day_tab[2][13];
308309

309310
#endif /* DT_H */

src/interfaces/ecpg/pgtypeslib/dt_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "dt.h"
99
#include "pgtypes_timestamp.h"
1010

11-
static int day_tab[2][13] = {
11+
int day_tab[2][13] = {
1212
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
1313
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}};
1414

src/interfaces/ecpg/pgtypeslib/timestamp.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "pgtypes_timestamp.h"
1313
#include "pgtypes_date.h"
1414

15+
1516
int PGTYPEStimestamp_defmt_scan(char **, char *, timestamp *, int *, int *, int *,
1617
int *, int *, int *, int *);
1718

@@ -844,3 +845,87 @@ PGTYPEStimestamp_defmt_asc(char *str, char *fmt, timestamp *d)
844845
free(mfmt);
845846
return i;
846847
}
848+
849+
/*
850+
* add an interval to a time stamp
851+
*
852+
* *tout = tin + span
853+
*
854+
* returns 0 if successful
855+
* returns -1 if it fails
856+
*
857+
*/
858+
859+
int
860+
PGTYPEStimestamp_add_interval(timestamp *tin, interval *span, timestamp *tout)
861+
{
862+
863+
864+
if (TIMESTAMP_NOT_FINITE(*tin))
865+
*tout = *tin;
866+
867+
868+
else
869+
{
870+
if (span->month != 0)
871+
{
872+
struct tm tt,
873+
*tm = &tt;
874+
fsec_t fsec;
875+
876+
877+
if (timestamp2tm(*tin, NULL, tm, &fsec, NULL) !=0)
878+
return -1;
879+
tm->tm_mon += span->month;
880+
if (tm->tm_mon > 12)
881+
{
882+
tm->tm_year += ((tm->tm_mon - 1) / 12);
883+
tm->tm_mon = (((tm->tm_mon - 1) % 12) + 1);
884+
}
885+
else if (tm->tm_mon < 1)
886+
{
887+
tm->tm_year += ((tm->tm_mon / 12) - 1);
888+
tm->tm_mon = ((tm->tm_mon % 12) + 12);
889+
}
890+
891+
892+
/* adjust for end of month boundary problems... */
893+
if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
894+
tm->tm_mday = (day_tab[isleap(tm->tm_year)][tm->tm_mon - 1]);
895+
896+
897+
if (tm2timestamp(tm, fsec, NULL, tin) !=0)
898+
return -1;
899+
}
900+
901+
902+
*tin +=span->time;
903+
*tout = *tin;
904+
}
905+
return 0;
906+
907+
}
908+
909+
910+
/*
911+
* subtract an interval from a time stamp
912+
*
913+
* *tout = tin - span
914+
*
915+
* returns 0 if successful
916+
* returns -1 if it fails
917+
*
918+
*/
919+
920+
int
921+
PGTYPEStimestamp_sub_interval(timestamp *tin, interval *span, timestamp *tout)
922+
{
923+
interval tspan;
924+
925+
tspan.month = -span->month;
926+
tspan.time = -span->time;
927+
928+
929+
return PGTYPEStimestamp_add_interval(tin, &tspan, tout );
930+
}
931+

src/interfaces/ecpg/preproc/pgc.l

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.132 2004/08/29 04:13:11 momjian Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.133 2004/12/23 10:46:10 meskes Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -611,9 +611,9 @@ cppline {space}*#(.*\\{space})+.*
611611
yb->buffer = YY_CURRENT_BUFFER;
612612
yb->lineno = yylineno;
613613
yb->filename = mm_strdup(input_filename);
614-
ptr->used = yb->next = yy_buffer;
614+
yb->next = yy_buffer;
615615

616-
yy_buffer = yb;
616+
ptr->used = yy_buffer = yb;
617617

618618
yy_scan_string(ptr->new);
619619
break;
@@ -712,9 +712,9 @@ cppline {space}*#(.*\\{space})+.*
712712
yb->buffer = YY_CURRENT_BUFFER;
713713
yb->lineno = yylineno;
714714
yb->filename = mm_strdup(input_filename);
715-
ptr->used = yb->next = yy_buffer;
715+
yb->next = yy_buffer;
716716

717-
yy_buffer = yb;
717+
ptr->used = yy_buffer = yb;
718718

719719
yy_scan_string(ptr->new);
720720
break;

0 commit comments

Comments
 (0)