]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In date/time functions, fix the rendering of "subsecond" and the
authordrh <>
Sat, 17 Jun 2023 15:22:00 +0000 (15:22 +0000)
committerdrh <>
Sat, 17 Jun 2023 15:22:00 +0000 (15:22 +0000)
computeHMS() routine to better deal with floating point
rounding errors that arise on some 32-bit systems.

FossilOrigin-Name: e46a00ae880dd12df090105498a85017d1367f88bf27d86f0b0200cf0536a906

manifest
manifest.uuid
src/date.c

index 4fe93f1d2a49e20ec32313b85806fdc2a44da015..97e84a413e3e490f429f98697139ca8d17b0e3ae 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Address\svarious\sharmless\scompiler\swarnings\sfrom\n[forum:/forumpost/d526da8ee4|forum\spost\sd526da8ee4].
-D 2023-06-16T14:39:21.991
+C In\sdate/time\sfunctions,\sfix\sthe\srendering\sof\s"subsecond"\sand\sthe\ncomputeHMS()\sroutine\sto\sbetter\sdeal\swith\sfloating\spoint\nrounding\serrors\sthat\sarise\son\ssome\s32-bit\ssystems.
+D 2023-06-17T15:22:00.451
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -582,7 +582,7 @@ F src/build.c cb54df6fd018a18e940a251c5e31780ffba8bc6c7a01e670b96a489adcbfb3b4
 F src/callback.c db3a45e376deff6a16c0058163fe0ae2b73a2945f3f408ca32cf74960b28d490
 F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
 F src/ctime.c 20507cc0b0a6c19cd882fcd0eaeda32ae6a4229fb4b024cfdf3183043d9b703d
-F src/date.c 36dfd2525c5544dbb246084e93839b839366e2204e7fb4a1a68a50f4344d32bc
+F src/date.c 62e7936bc31a6a78ad93d36e5261c41113a4c1cad7f043c77790a5ffc8c63e2d
 F src/dbpage.c f3eea5f7ec47e09ee7da40f42b25092ecbe961fc59566b8e5f705f34335b2387
 F src/dbstat.c ec92074baa61d883de58c945162d9e666c13cd7cf3a23bc38b4d1c4d0b2c2bef
 F src/delete.c 092d745aa08969204a351711193e7f34de449ea57755eacbd11f1c316e9f7673
@@ -2040,8 +2040,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P da1252b29852191eccbea98e0314408c75bb83a51f9d68d589705d4971a23850
-R bd866507ab2ad9c1d16b6b41135b6999
+P 365caf2f97e8f15842f52536e8d05d359e9d6e863182e020ce14a9a9f27ee057
+R 73503eeccae824219370676594eff77d
 U drh
-Z 3cd810c8278f3d496fd739b853fdc856
+Z bc326b4113337a90f4cbc022d068432e
 # Remove this line to create a well-formed Fossil manifest.
index 8ea190e188e062f0f50e15381f25f1c71d150b02..fedacc3621da824048c7ab80a7a7ab09109f25dc 100644 (file)
@@ -1 +1 @@
-365caf2f97e8f15842f52536e8d05d359e9d6e863182e020ce14a9a9f27ee057
\ No newline at end of file
+e46a00ae880dd12df090105498a85017d1367f88bf27d86f0b0200cf0536a906
\ No newline at end of file
index d01cf12a8551b4118478a6d360538f24e42828da..07e0a1c322d0f9c345349ba311da60958b9ac9ec 100644 (file)
@@ -448,56 +448,22 @@ static void computeYMD(DateTime *p){
   p->validYMD = 1;
 }
 
-/* GCC (and sometimes Clang too) will sometimes be off by 1 millisecond
-** in computeHMS() on 32-bit platforms with optimization enabled.
-** I don't know if this is a compiler bug or a bug in the code.  It is hard
-** to debug because with optimization enabled, gdb skips around so much
-** you cannot see what is happening, and if you try adding debug printf()s
-** to the code, the problem goes away.
-**
-** For now, I will work around the problem by disabling optimizations in
-** the computeHMS() routine.
-**
-** Problem seen using gcc-5.4.0, gcc-9.4.0, clang-10.  Works ok on
-** MSVC, clang-3.4, and clang-6.0.
-**
-** Whatever the problem is, it causes some answers to be off by one
-** millisecond.  So we get results like "2021-03-05 03:04:05.599" instead
-** of the desired result of "2021-03-05 03:04:05.600".
-**
-** To reproduce the problem using TH3:
-**
-**    ./th3make debug.rc -O1 -m32 cfg/c1.cfg cov1/date8.test
-*/
-#if SQLITE_PTRSIZE==4 && GCC_VERSION>0
-#pragma GCC push_options
-#pragma GCC optimize ("O0")
-#endif
-
 /*
 ** Compute the Hour, Minute, and Seconds from the julian day number.
 */
 static void computeHMS(DateTime *p){
-  int s;
+  int day_ms, day_min; /* milliseconds, minutes into the day */
   if( p->validHMS ) return;
   computeJD(p);
-  s = (int)((p->iJD + 43200000) % 86400000);
-  p->s = s/1000.0;
-  s = (int)p->s;
-  p->s -= s;
-  p->h = s/3600;
-  s -= p->h*3600;
-  p->m = s/60;
-  p->s += s - p->m*60;
+  day_ms = (int)((p->iJD + 43200000) % 86400000);
+  p->s = (day_ms % 60000)/1000.0;
+  day_min = day_ms/60000;
+  p->m = day_min % 60;
+  p->h = day_min / 60;
   p->rawS = 0;
   p->validHMS = 1;
 }
 
-/* Reactivate GCC optimization */
-#if SQLITE_PTRSIZE==4 && GCC_VERSION>0
-#pragma GCC pop_options
-#endif
-
 /*
 ** Compute both YMD and HMS
 */
@@ -1160,7 +1126,7 @@ static void datetimeFunc(
     zBuf[16] = '0' + (x.m)%10;
     zBuf[17] = ':';
     if( x.useSubsec ){
-      s = (int)1000.0*x.s;
+      s = (int)(1000.0*x.s + 0.5);
       zBuf[18] = '0' + (s/10000)%10;
       zBuf[19] = '0' + (s/1000)%10;
       zBuf[20] = '.';
@@ -1207,7 +1173,7 @@ static void timeFunc(
     zBuf[4] = '0' + (x.m)%10;
     zBuf[5] = ':';
     if( x.useSubsec ){
-      s = (int)1000.0*x.s;
+      s = (int)(1000.0*x.s + 0.5);
       zBuf[6] = '0' + (s/10000)%10;
       zBuf[7] = '0' + (s/1000)%10;
       zBuf[8] = '.';