From 58a92434a6a15892c646b9f928a2d067a9e2966a Mon Sep 17 00:00:00 2001 From: Dave Sharpe Date: Sat, 5 Dec 2026 22:37:26 +0000 Subject: [PATCH v1 2/2] ecpg: Round fractional Julian days to the nearest microsecond pgtypeslib's DecodeDateTime() has the same problem as the backend code fixed in the previous commit: for input such as 'J2451187.7', the day fraction times USECS_PER_DAY is converted to int64 by truncation inside dt2time(), so PGTYPEStimestamp_from_asc() returned 16:47:59.999999 rather than 16:48:00. Round with rint() before calling dt2time(). As in the backend, a fraction within half a microsecond of a whole day now yields midnight of the following day. Author: Dave Sharpe --- src/interfaces/ecpg/pgtypeslib/dt_common.c | 4 ++- .../ecpg/test/expected/pgtypeslib-dt_test2.c | 27 ++++++++++++++----- .../test/expected/pgtypeslib-dt_test2.stdout | 3 +++ .../ecpg/test/pgtypeslib/dt_test2.pgc | 15 +++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/interfaces/ecpg/pgtypeslib/dt_common.c b/src/interfaces/ecpg/pgtypeslib/dt_common.c index 8c1f485f70c..c2b848ff6d7 100644 --- a/src/interfaces/ecpg/pgtypeslib/dt_common.c +++ b/src/interfaces/ecpg/pgtypeslib/dt_common.c @@ -2071,7 +2071,9 @@ DecodeDateTime(char **field, int *ftype, int nf, return -1; tmask |= DTK_TIME_M; - dt2time((time * USECS_PER_DAY), &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec); + /* round to the nearest microsecond */ + time = rint(time * USECS_PER_DAY); + dt2time((timestamp) time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec); } break; diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c index b3e1e7519df..d788faa516c 100644 --- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c +++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.c @@ -73,6 +73,12 @@ char *intervals[] = { "1 minute", "1 year 59 mins foobar", NULL }; +/* fractional Julian days are rounded to the nearest microsecond */ +static char *julians[] = { "J2451187.7", + "J2451187.35", + "J2451187.999999999999", + NULL }; + int main(void) { @@ -83,22 +89,22 @@ main(void) -#line 62 "dt_test2.pgc" +#line 68 "dt_test2.pgc" date date1 ; -#line 63 "dt_test2.pgc" +#line 69 "dt_test2.pgc" timestamp ts1 , ts2 ; -#line 64 "dt_test2.pgc" +#line 70 "dt_test2.pgc" char * text ; -#line 65 "dt_test2.pgc" +#line 71 "dt_test2.pgc" interval * i1 ; -#line 66 "dt_test2.pgc" +#line 72 "dt_test2.pgc" date * dc ; /* exec sql end declare section */ -#line 67 "dt_test2.pgc" +#line 73 "dt_test2.pgc" int i, j; @@ -182,5 +188,14 @@ main(void) PGTYPESinterval_free(i1); } + for (i = 0; julians[i]; i++) + { + errno = 0; + ts1 = PGTYPEStimestamp_from_asc(julians[i], NULL); + text = PGTYPEStimestamp_to_asc(ts1); + printf("julian[%d]: %s\n", i, errno ? "-" : text); + PGTYPESchar_free(text); + } + return 0; } diff --git a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.stdout b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.stdout index 0fbcce67b68..c1f2d25cc41 100644 --- a/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.stdout +++ b/src/interfaces/ecpg/test/expected/pgtypeslib-dt_test2.stdout @@ -120,3 +120,6 @@ interval_copy[4]: @ 1 day 1 hour 1 min 1 sec interval[5]: @ 1 year 59 mins interval_copy[5]: @ 1 year 59 mins Error parsing interval 6 +julian[0]: 1999-01-08 16:48:00 +julian[1]: 1999-01-08 08:24:00 +julian[2]: 1999-01-09 00:00:00 diff --git a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc index 62b934b07ee..ebda4819bf8 100644 --- a/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc +++ b/src/interfaces/ecpg/test/pgtypeslib/dt_test2.pgc @@ -55,6 +55,12 @@ char *intervals[] = { "1 minute", "1 year 59 mins foobar", NULL }; +/* fractional Julian days are rounded to the nearest microsecond */ +static char *julians[] = { "J2451187.7", + "J2451187.35", + "J2451187.999999999999", + NULL }; + int main(void) { @@ -147,5 +153,14 @@ main(void) PGTYPESinterval_free(i1); } + for (i = 0; julians[i]; i++) + { + errno = 0; + ts1 = PGTYPEStimestamp_from_asc(julians[i], NULL); + text = PGTYPEStimestamp_to_asc(ts1); + printf("julian[%d]: %s\n", i, errno ? "-" : text); + PGTYPESchar_free(text); + } + return 0; } -- 2.43.0