1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

Simplifications to the new timediff() logic.

FossilOrigin-Name: d6954259bd3f8475f3804fd02a1591d037efa9df54da45776863c024ff90e596
This commit is contained in:
drh
2023-05-30 14:46:30 +00:00
parent cc4bfd9623
commit 7d65f1bec0
3 changed files with 14 additions and 26 deletions

View File

@@ -887,7 +887,7 @@ static int parseModifier(
** the range 0-11 and DD is limited to 0-30.
*/
if( z[0]!='+' && z[0]!='-' ) break; /* Must start with +/- */
if( n!=5 ) break; /* Must be 4-digit YYYY */
if( NEVER(n!=5) ) break; /* Must be 4-digit YYYY */
if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break;
if( M>=12 ) break; /* M range 0..11 */
if( D>=31 ) break; /* D range 0..30 */
@@ -1400,18 +1400,12 @@ static void timediffFunc(
int argc,
sqlite3_value **argv
){
DateTime d1, d2;
sqlite3_str *pOut = 0;
char sign;
int rc;
int Y, M;
DateTime d1, d2;
sqlite3_str sRes;
if( isDate(context, 1, argv, &d1) ) return;
if( isDate(context, 1, &argv[1], &d2) ) return;
pOut = sqlite3_str_new(sqlite3_context_db_handle(context));
if( pOut==0 ){
sqlite3_result_error_nomem(context);
return;
}
computeYMD_HMS(&d1);
computeYMD_HMS(&d2);
if( d1.iJD>=d2.iJD ){
@@ -1439,7 +1433,7 @@ static void timediffFunc(
Y--;
}
d2.M--;
if( d2.M<0 ){
if( d2.M<1 ){
d2.M = 12;
d2.Y--;
}
@@ -1487,16 +1481,10 @@ static void timediffFunc(
d1.validHMS = 0;
d1.validTZ = 0;
computeYMD_HMS(&d1);
sqlite3_str_appendf(pOut, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
sign, Y, M, d1.D-1, d1.h, d1.m, d1.s);
rc = sqlite3_str_errcode(pOut);
if( rc ){
sqlite3_free(sqlite3_str_finish(pOut));
sqlite3_result_error_code(context, rc);
}else{
sqlite3_result_text(context,
sqlite3_str_finish(pOut), -1, sqlite3_free);
}
sqlite3ResultStrAccum(context, &sRes);
}