]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Simplifications to the strftime() logic.
authordrh <>
Sat, 20 Jan 2024 18:26:28 +0000 (18:26 +0000)
committerdrh <>
Sat, 20 Jan 2024 18:26:28 +0000 (18:26 +0000)
FossilOrigin-Name: aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff

manifest
manifest.uuid
src/date.c

index 165799a685caebde4d27b06baf1a559b1192caa4..821d38be63d43ea6548320627bec700bd32ff80f 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\smemory\sleak\sin\snew\smemdb1.test\stest\scases\sthat\swere\sadded\sby\n[e638d5e408ea2e18].\s\sNo\schanges\sto\sSQLite\sitself.
-D 2024-01-20T18:21:50.590
+C Simplifications\sto\sthe\sstrftime()\slogic.
+D 2024-01-20T18:26:28.712
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -684,7 +684,7 @@ F src/build.c e7d9044592eeeea8e78d8ae53ca8d31fd6e92ca0d4f53e2f2e8ccf7352e0b04b
 F src/callback.c db3a45e376deff6a16c0058163fe0ae2b73a2945f3f408ca32cf74960b28d490
 F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
 F src/ctime.c 23331529e654be40ca97d171cbbffe9b3d4c71cc53b78fe5501230675952da8b
-F src/date.c 35ea61eb7ab5b918db7a96725346234bf8fb5bc161c7b868c20a7f24f351a2da
+F src/date.c 48a110a2a267398ad9b04fe3db69dfa194f91719f94fa55cd560e03510f963e6
 F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782
 F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43
 F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500
@@ -2159,8 +2159,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 39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385
-R 74384040736e0977d7c5dfb4dce5d968
+P bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465
+R 8518c96f5a8faebc2a9945073340f70a
 U drh
-Z 312b96149522d237d108c8a3641b64b6
+Z f670e53a135b10311c59f478b3527211
 # Remove this line to create a well-formed Fossil manifest.
index 0ffeb5f5fed07f9165323d2b27a67fabd64b8426..e3a0693990b5498e7803ffcdc83a18fb87c154da 100644 (file)
@@ -1 +1 @@
-bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465
\ No newline at end of file
+aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff
\ No newline at end of file
index 76e1894f8d788d3e398143b7dabe2c3991845c27..e468925d5e72425ee2dedea294ed1c1f77bbb297 100644 (file)
@@ -1237,10 +1237,14 @@ static void dateFunc(
 }
 
 /*
-** Compute the one-based day of the year for the DateTime pDate.
-** Jan01 = 1,  Jan02 = 2, ... Dec31 = 356 or 366.
+** Compute the number of days after the most recent January 1.
+**
+** In other words, compute the zero-based day number for the
+** current year:
+**
+**   Jan01 = 0,  Jan02 = 2, ... Dec31 = 364 or 365.
 */
-static int dayOfYear(DateTime *pDate){
+static int daysAfterJan01(DateTime *pDate){
   DateTime jan01 = *pDate;
   assert( jan01.validYMD );
   assert( jan01.validHMS );
@@ -1249,18 +1253,33 @@ static int dayOfYear(DateTime *pDate){
   jan01.M = 1;
   jan01.D = 1;
   computeJD(&jan01);
-  return (int)((pDate->iJD-jan01.iJD+43200000)/86400000) + 1;
+  return (int)((pDate->iJD-jan01.iJD+43200000)/86400000);
 }
 
 /*
-** Return the day of the week.  1==Monday, 2=Tues, ..., 7=Sunday.
+** Return the number of days after the most recent Monday.
+**
+** In other words, return the day of the week according
+** to this code:
+**
+**   0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday.
+*/
+static int daysAfterMonday(DateTime *pDate){
+  assert( pDate->validJD );
+  return (int)((pDate->iJD+43200000)/86400000) % 7;
+}
+
+/*
+** Return the number of days after the most recent Sunday.
+**
+** In other words, return the day of the week according
+** to this code:
+**
+**   0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday
 */
-static int dayOfWeek(DateTime *pDate){
-  int w;
+static int daysAfterSunday(DateTime *pDate){
   assert( pDate->validJD );
-  w = ((pDate->iJD+129600000)/86400000) % 7;
-  if( w==0 ) w = 7;
-  return w;
+  return (int)((pDate->iJD+129600000)/86400000) % 7;
 }
 
 /*
@@ -1344,7 +1363,7 @@ static void strftimeFunc(
         DateTime y = x;
         assert( y.validJD );
         /* Move y so that it is the Thursday in the same week as x */
-        y.iJD += (4 - dayOfWeek(&x))*86400000;
+        y.iJD += (3 - daysAfterMonday(&x))*86400000;
         y.validYMD = 0;
         computeYMD(&y);
         if( cf=='g' ){
@@ -1368,8 +1387,7 @@ static void strftimeFunc(
         break;
       }
       case 'j': {  /* Day of year.  Jan01==1, Jan02==2, and so forth */
-        int nDay = dayOfYear(&x);
-        sqlite3_str_appendf(&sRes,"%03d",nDay);
+        sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1);
         break;
       }
       case 'J': {  /* Julian day number.  (Non-standard) */
@@ -1417,35 +1435,29 @@ static void strftimeFunc(
       }
       case 'u':    /* Day of week.  1 to 7.  Monday==1, Sunday==7 */
       case 'w': {  /* Day of week.  0 to 6.  Sunday==0, Monday==1 */
-        char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
+        char c = (char)daysAfterSunday(&x) + '0';
         if( c=='0' && cf=='u' ) c = '7';
         sqlite3_str_appendchar(&sRes, 1, c);
         break;
       }
       case 'U': {  /* Week num. 00-53. First Sun of the year is week 01 */
-        int wd;    /* 0=Sunday, 1=Monday, 2=Tuesday, ... 7=Saturday */
-        int nDay;  /* Day of the year.  0..364 or 0..365 for leapyears */
-        nDay = dayOfYear(&x);
-        wd = (int)(((x.iJD+43200000)/86400000 + 1)%7);
-        sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7);
+        sqlite3_str_appendf(&sRes,"%02d",
+              (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7);
         break;
       }
       case 'V': {  /* Week num. 01-53. First week with a Thur is week 01 */
         DateTime y = x;
         /* Adjust y so that is the Thursday in the same week as x */
         assert( y.validJD );
-        y.iJD += (4 - dayOfWeek(&x))*86400000;
+        y.iJD += (3 - daysAfterMonday(&x))*86400000;
         y.validYMD = 0;
         computeYMD(&y);
-        sqlite3_str_appendf(&sRes,"%02d", (dayOfYear(&y)-1)/7+1);
+        sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1);
         break;
       }
       case 'W': {  /* Week num. 00-53. First Mon of the year is week 01 */
-        int wd;    /* 0=Monday, 1=Tuesday, ... 6=Sunday */
-        int nDay;  /* Day of the year.  0..364 or 0..365 for leapyears */
-        nDay = dayOfYear(&x);
-        wd = (int)(((x.iJD+43200000)/86400000)%7);
-        sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7);
+        sqlite3_str_appendf(&sRes,"%02d",
+           (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7);
         break;
       }
       case 'Y': {