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

Simplifications to the strftime() logic.

FossilOrigin-Name: aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff
This commit is contained in:
drh
2024-01-20 18:26:28 +00:00
parent 6c64e24f9d
commit 514bf99b6a
3 changed files with 45 additions and 33 deletions

View 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 dayOfWeek(DateTime *pDate){
int w;
static int daysAfterMonday(DateTime *pDate){
assert( pDate->validJD );
w = ((pDate->iJD+129600000)/86400000) % 7;
if( w==0 ) w = 7;
return w;
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 daysAfterSunday(DateTime *pDate){
assert( pDate->validJD );
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': {