1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-05 15:55:57 +03:00

Implement a new algorithm for computing ISO week values in strftime() based

on the idea (from [forum/forumpost/3681cb1bcd|Nuno Cruces]) of shifting the
date being tested to the Thursday of the same week.

FossilOrigin-Name: b06ab46a9ee98719159ed3e05cdfbf26281353d781206f56ed7cb12859210fed
This commit is contained in:
drh
2024-01-20 00:31:44 +00:00
parent a4802720a1
commit f08287cc12
3 changed files with 24 additions and 53 deletions

View File

@@ -1,5 +1,5 @@
C Allow\slarge\shexadecimal\sliterals\sto\sbe\sused\sas\sDEFAULT\svalues. C Implement\sa\snew\salgorithm\sfor\scomputing\sISO\sweek\svalues\sin\sstrftime()\sbased\non\sthe\sidea\s(from\s[forum/forumpost/3681cb1bcd|Nuno\sCruces])\sof\sshifting\sthe\ndate\sbeing\stested\sto\sthe\sThursday\sof\sthe\ssame\sweek.
D 2024-01-19T16:51:34.042 D 2024-01-20T00:31:44.563
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -684,7 +684,7 @@ F src/build.c e7d9044592eeeea8e78d8ae53ca8d31fd6e92ca0d4f53e2f2e8ccf7352e0b04b
F src/callback.c db3a45e376deff6a16c0058163fe0ae2b73a2945f3f408ca32cf74960b28d490 F src/callback.c db3a45e376deff6a16c0058163fe0ae2b73a2945f3f408ca32cf74960b28d490
F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
F src/ctime.c 23331529e654be40ca97d171cbbffe9b3d4c71cc53b78fe5501230675952da8b F src/ctime.c 23331529e654be40ca97d171cbbffe9b3d4c71cc53b78fe5501230675952da8b
F src/date.c 7ed194fee09bc3aa2129cfe74ebf5daca6fc24c0995dd28cf2a2feacf4d8d44f F src/date.c 35ea61eb7ab5b918db7a96725346234bf8fb5bc161c7b868c20a7f24f351a2da
F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782 F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782
F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43 F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43
F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500 F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500
@@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 1481baf3d55effcc117f2097e2d49a6f60f5f74b21190c3be943fd785b8a4c5b P 8cccc1f27d7470d3cdd3c9c6d74f6a5ac49ec6eaa7002bcf96f4842fb8c79e1a
R 7a04f28ab4102a53adff1c3861de4c59 R 6109198c01da554c8b8cc1d558fbd53e
U dan U drh
Z de8bd7b1420ad71ea0d7cb25c6eae88b Z ae5f14e34bf5701b2021ec4920aa096b
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
8cccc1f27d7470d3cdd3c9c6d74f6a5ac49ec6eaa7002bcf96f4842fb8c79e1a b06ab46a9ee98719159ed3e05cdfbf26281353d781206f56ed7cb12859210fed

View File

@@ -1249,7 +1249,7 @@ static int dayOfYear(DateTime *pDate){
jan01.M = 1; jan01.M = 1;
jan01.D = 1; jan01.D = 1;
computeJD(&jan01); computeJD(&jan01);
return (int)((pDate->iJD-jan01.iJD+45300000)/86400000) + 1; return (int)((pDate->iJD-jan01.iJD+43200000)/86400000) + 1;
} }
/* /*
@@ -1263,41 +1263,6 @@ static int dayOfWeek(DateTime *pDate){
return w; return w;
} }
/*
** Compute the day-of-week (0=Sunday, 1=Monday, ..., 6=Saturday) for
** the last day of the calendar year Y.
*/
static int lastDayOfYear(int Y){
return (Y + (Y/4) - (Y/100) + (Y/400))%7;
}
/*
** Return the number of ISO weeks in calendar year Y. The answer is
** either 52 or 53.
*/
static int weeksInYear(int Y){
if( lastDayOfYear(Y)==4 || lastDayOfYear(Y-1)==3 ){
return 53;
}else{
return 52;
}
}
/*
** Compute the number days since the start of the ISO-week year for pDate.
** The ISO-week year starts on the first day of the week (always a Monday)
** that contains the first Thursday on or after January 1.
*/
static int isoWeekNumber(DateTime *pDate){
int wn = (10 + dayOfYear(pDate) - dayOfWeek(pDate))/7;
if( wn<1 ){
wn = weeksInYear(pDate->Y-1);
}else if( wn>weeksInYear(pDate->Y) ){
wn = 1;
}
return wn;
}
/* /*
** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
** **
@@ -1376,16 +1341,16 @@ static void strftimeFunc(
} }
case 'G': /* Fall thru */ case 'G': /* Fall thru */
case 'g': { case 'g': {
int Y = x.Y; DateTime y = x;
if( x.M==12 && isoWeekNumber(&x)==1 ){ assert( y.validJD );
Y++; /* Move y so that it is the Thursday in the same week as x */
}else if( x.M==1 && isoWeekNumber(&x)>=52 ){ y.iJD += (4 - dayOfWeek(&x))*86400000;
Y--; y.validYMD = 0;
} computeYMD(&y);
if( cf=='g' ){ if( cf=='g' ){
sqlite3_str_appendf(&sRes, "%02d", Y%100); sqlite3_str_appendf(&sRes, "%02d", y.Y%100);
}else{ }else{
sqlite3_str_appendf(&sRes, "%04d", Y); sqlite3_str_appendf(&sRes, "%04d", y.Y);
} }
break; break;
} }
@@ -1466,7 +1431,13 @@ static void strftimeFunc(
break; break;
} }
case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */
sqlite3_str_appendf(&sRes,"%02d", isoWeekNumber(&x)); 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.validYMD = 0;
computeYMD(&y);
sqlite3_str_appendf(&sRes,"%02d", (dayOfYear(&y)-1)/7+1);
break; break;
} }
case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */