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

Code cleanup in the date and time functions. (CVS 1263)

FossilOrigin-Name: 9b3bcde1530091602e551435ffd7820eb79727d5
This commit is contained in:
drh
2004-02-22 17:49:32 +00:00
parent 2d3917da29
commit eb9a9e8888
8 changed files with 96 additions and 100 deletions

View File

@@ -1,5 +1,5 @@
C Rearrange\sthe\sgrammar\ssome\sso\sthat\stokens\sthat\sare\sused\stogether\sappear\ntogether\sin\sthe\sgrammar\sfile.\s\sThis\sreduces\sthe\ssize\sof\sthe\sparser\stables\nand\ssome\sof\sthe\sjump\stables\sin\sswitch\sstatements.\s(CVS\s1262) C Code\scleanup\sin\sthe\sdate\sand\stime\sfunctions.\s(CVS\s1263)
D 2004-02-22T16:27:00 D 2004-02-22T17:49:33
F Makefile.in cfd75c46b335881999333a9e4b982fa8491f200b F Makefile.in cfd75c46b335881999333a9e4b982fa8491f200b
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906 F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@@ -29,11 +29,11 @@ F src/btree.h 41cb3ff6ebc3f6da2d0a074e39ff8c7a2287469f
F src/btree_rb.c 32b2cb4285c0fbd53b89de021637b63d52257e54 F src/btree_rb.c 32b2cb4285c0fbd53b89de021637b63d52257e54
F src/build.c f6ef61a2b9524f5d1fd66a500747b6a5c114dd0f F src/build.c f6ef61a2b9524f5d1fd66a500747b6a5c114dd0f
F src/copy.c 391ce142f6b1faa093867ecee134f61a5028a9af F src/copy.c 391ce142f6b1faa093867ecee134f61a5028a9af
F src/date.c 6120c591cd905799318018cc67df53e9bdfaef28 F src/date.c 3025642cee50d5c41aef4a22cbc41aa7e543c922
F src/delete.c 8e2ff752bf485906effcc64f267cdd7227463567 F src/delete.c 8e2ff752bf485906effcc64f267cdd7227463567
F src/encode.c 9e70ea1e4e746f23f18180949e94f1bb1c2220d3 F src/encode.c 9e70ea1e4e746f23f18180949e94f1bb1c2220d3
F src/expr.c 7bb3a1ffbf3233b663f017d5296a718156f1f5fb F src/expr.c 7bb3a1ffbf3233b663f017d5296a718156f1f5fb
F src/func.c 36504a3458a5501ce960c46c74ead32aab9b306a F src/func.c a2265f29e6a286203c9dfeb835d9a50439617805
F src/hash.c 9b56ef3b291e25168f630d5643a4264ec011c70e F src/hash.c 9b56ef3b291e25168f630d5643a4264ec011c70e
F src/hash.h 3247573ab95b9dd90bcca0307a75d9a16da1ccc7 F src/hash.h 3247573ab95b9dd90bcca0307a75d9a16da1ccc7
F src/insert.c ad2902c171d23b92e2b350db2da86e36062e044b F src/insert.c ad2902c171d23b92e2b350db2da86e36062e044b
@@ -50,19 +50,19 @@ F src/random.c 775913e0b7fbd6295d21f12a7bd35b46387c44b2
F src/select.c 9a41dace754f0dab5e991e402c05fa3c24d04f19 F src/select.c 9a41dace754f0dab5e991e402c05fa3c24d04f19
F src/shell.c c3d3404fa82bb0808444fda9884d1bb572fd18b9 F src/shell.c c3d3404fa82bb0808444fda9884d1bb572fd18b9
F src/sqlite.h.in 64f016cd5ce190643a0f47760188fdf4e0b2227e F src/sqlite.h.in 64f016cd5ce190643a0f47760188fdf4e0b2227e
F src/sqliteInt.h de32ca5481b9ffc30c3ec0d9ff97b505eaa2f016 F src/sqliteInt.h a3e8f422803abf1e6ba07e15ffe5070a50f8b5e5
F src/table.c d845cb101b5afc1f7fea083c99e3d2fa7998d895 F src/table.c d845cb101b5afc1f7fea083c99e3d2fa7998d895
F src/tclsqlite.c b84dafe3a8532ff534c36e96bd38880e4b9cedf3 F src/tclsqlite.c b84dafe3a8532ff534c36e96bd38880e4b9cedf3
F src/test1.c bf07ff6666c97b3fb91732deba3d4e6373960259 F src/test1.c 9aa62b89d420e6763b5e7ae89a47f6cf87370477
F src/test2.c 75819b0f2c63c6a0fd6995445881f2eb94036996 F src/test2.c 75819b0f2c63c6a0fd6995445881f2eb94036996
F src/test3.c 30985ebdfaf3ee1462a9b0652d3efbdc8d9798f5 F src/test3.c 30985ebdfaf3ee1462a9b0652d3efbdc8d9798f5
F src/test4.c dcbbbb382626fd466a7c46907f74db35fc8bad64 F src/test4.c dcbbbb382626fd466a7c46907f74db35fc8bad64
F src/tokenize.c 6676b946fd8825b67ab52140af4fdc57a70bda48 F src/tokenize.c 6676b946fd8825b67ab52140af4fdc57a70bda48
F src/trigger.c a9927b57c865b6f3df3fb5e40c9824d722660ded F src/trigger.c a9927b57c865b6f3df3fb5e40c9824d722660ded
F src/update.c e6eed1a4a429cc28f57533365c72293794c904cf F src/update.c e6eed1a4a429cc28f57533365c72293794c904cf
F src/util.c 8fbed02d3d5d29d4b236772f779778599c911e21 F src/util.c 122bc174f6c8c2eb6a9127d9f13c4c74f83b85e4
F src/vacuum.c d9e80c2b36ee1f623dbf1bdf3cedad24a23f87ac F src/vacuum.c d9e80c2b36ee1f623dbf1bdf3cedad24a23f87ac
F src/vdbe.c f14e8e2ef82cb8480394697c40644d70195598e5 F src/vdbe.c af187d86cb1bcc1ead227245350d1693c28c77a2
F src/vdbe.h b1b22ffca48f593d375fd846c583679d49c2e5c9 F src/vdbe.h b1b22ffca48f593d375fd846c583679d49c2e5c9
F src/vdbeInt.h b40ff02ce39fd076e6ff3369e19c1bbfe1986682 F src/vdbeInt.h b40ff02ce39fd076e6ff3369e19c1bbfe1986682
F src/vdbeaux.c 88ebe3aa963478f3cee9fb6e3d8ca9db1bafacb2 F src/vdbeaux.c 88ebe3aa963478f3cee9fb6e3d8ca9db1bafacb2
@@ -189,7 +189,7 @@ F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604
F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1 F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4 F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
P f36b122d9767fa9e6dc5bcce04b5606d67cad3d9 P d372c16ec6621dbab371bff7f1803ca096862984
R 376533278e07e4ce9f2dde354b5969cb R 26dc1ce48ade981ea71375567947b9f1
U drh U drh
Z b87ef4aa1ac83c5f79b5688ea16271fe Z 69ec7e76ed86d8c90a49976c004def1a

View File

@@ -1 +1 @@
d372c16ec6621dbab371bff7f1803ca096862984 9b3bcde1530091602e551435ffd7820eb79727d5

View File

@@ -16,7 +16,7 @@
** sqliteRegisterDateTimeFunctions() found at the bottom of the file. ** sqliteRegisterDateTimeFunctions() found at the bottom of the file.
** All other code has file scope. ** All other code has file scope.
** **
** $Id: date.c,v 1.11 2004/02/21 03:28:18 drh Exp $ ** $Id: date.c,v 1.12 2004/02/22 17:49:33 drh Exp $
** **
** NOTES: ** NOTES:
** **
@@ -74,17 +74,50 @@ struct DateTime {
/* /*
** Convert N digits from zDate into an integer. Return ** Convert zDate into one or more integers. Additional arguments
** -1 if zDate does not begin with N digits. ** come in groups of 5 as follows:
**
** N number of digits in the integer
** min minimum allowed value of the integer
** max maximum allowed value of the integer
** nextC first character after the integer
** pVal where to write the integers value.
**
** Conversions continue until one with nextC==0 is encountered.
** The function returns the number of successful conversions.
*/ */
static int getDigits(const char *zDate, int N){ static int getDigits(const char *zDate, ...){
int val = 0; va_list ap;
while( N-- ){ int val;
if( !isdigit(*zDate) ) return -1; int N;
val = val*10 + *zDate - '0'; int min;
int max;
int nextC;
int *pVal;
int cnt = 0;
va_start(ap, zDate);
do{
N = va_arg(ap, int);
min = va_arg(ap, int);
max = va_arg(ap, int);
nextC = va_arg(ap, int);
pVal = va_arg(ap, int*);
val = 0;
while( N-- ){
if( !isdigit(*zDate) ){
return cnt;
}
val = val*10 + *zDate - '0';
zDate++;
}
if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
return cnt;
}
*pVal = val;
zDate++; zDate++;
} cnt++;
return val; }while( nextC );
return cnt;
} }
/* /*
@@ -92,38 +125,9 @@ static int getDigits(const char *zDate, int N){
** the number of digits converted. ** the number of digits converted.
*/ */
static int getValue(const char *z, double *pR){ static int getValue(const char *z, double *pR){
double r = 0.0; const char *zEnd;
double rDivide = 1.0; *pR = sqliteAtoF(z, &zEnd);
int isNeg = 0; return zEnd - z;
int nChar = 0;
if( *z=='+' ){
z++;
nChar++;
}else if( *z=='-' ){
z++;
isNeg = 1;
nChar++;
}
if( !isdigit(*z) ) return 0;
while( isdigit(*z) ){
r = r*10.0 + *z - '0';
nChar++;
z++;
}
if( *z=='.' && isdigit(z[1]) ){
z++;
nChar++;
while( isdigit(*z) ){
r = r*10.0 + *z - '0';
rDivide *= 10.0;
nChar++;
z++;
}
r /= rDivide;
}
if( *z!=0 && !isspace(*z) ) return 0;
*pR = isNeg ? -r : r;
return nChar;
} }
/* /*
@@ -151,14 +155,10 @@ static int parseTimezone(const char *zDate, DateTime *p){
return *zDate!=0; return *zDate!=0;
} }
zDate++; zDate++;
nHr = getDigits(zDate, 2); if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
if( nHr<0 || nHr>14 ) return 1; return 1;
zDate += 2; }
if( zDate[0]!=':' ) return 1; zDate += 5;
zDate++;
nMn = getDigits(zDate, 2);
if( nMn<0 || nMn>59 ) return 1;
zDate += 2;
p->tz = sgn*(nMn + nHr*60); p->tz = sgn*(nMn + nHr*60);
while( isspace(*zDate) ){ zDate++; } while( isspace(*zDate) ){ zDate++; }
return *zDate!=0; return *zDate!=0;
@@ -174,16 +174,16 @@ static int parseTimezone(const char *zDate, DateTime *p){
static int parseHhMmSs(const char *zDate, DateTime *p){ static int parseHhMmSs(const char *zDate, DateTime *p){
int h, m, s; int h, m, s;
double ms = 0.0; double ms = 0.0;
h = getDigits(zDate, 2); if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
if( h<0 || zDate[2]!=':' ) return 1; return 1;
zDate += 3; }
m = getDigits(zDate, 2); zDate += 5;
if( m<0 || m>59 ) return 1;
zDate += 2;
if( *zDate==':' ){ if( *zDate==':' ){
s = getDigits(&zDate[1], 2); zDate++;
if( s<0 || s>59 ) return 1; if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
zDate += 3; return 1;
}
zDate += 2;
if( *zDate=='.' && isdigit(zDate[1]) ){ if( *zDate=='.' && isdigit(zDate[1]) ){
double rScale = 1.0; double rScale = 1.0;
zDate++; zDate++;
@@ -268,18 +268,13 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){
}else{ }else{
neg = 0; neg = 0;
} }
Y = getDigits(zDate, 4); if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
if( Y<0 || zDate[4]!='-' ) return 1; return 1;
zDate += 5; }
M = getDigits(zDate, 2); zDate += 10;
if( M<=0 || M>12 || zDate[2]!='-' ) return 1;
zDate += 3;
D = getDigits(zDate, 2);
if( D<=0 || D>31 ) return 1;
zDate += 2;
while( isspace(*zDate) ){ zDate++; } while( isspace(*zDate) ){ zDate++; }
if( isdigit(*zDate) ){ if( parseHhMmSs(zDate, p)==0 ){
if( parseHhMmSs(zDate, p) ) return 1; /* We got the time */
}else if( *zDate==0 ){ }else if( *zDate==0 ){
p->validHMS = 0; p->validHMS = 0;
}else{ }else{
@@ -327,7 +322,7 @@ static int parseDateOrTime(const char *zDate, DateTime *p){
} }
return 1; return 1;
}else if( sqliteIsNumber(zDate) ){ }else if( sqliteIsNumber(zDate) ){
p->rJD = sqliteAtoF(zDate); p->rJD = sqliteAtoF(zDate, 0);
p->validJD = 1; p->validJD = 1;
return 0; return 0;
} }

View File

@@ -16,7 +16,7 @@
** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope. ** All other code has file scope.
** **
** $Id: func.c,v 1.40 2004/02/20 22:53:39 rdc Exp $ ** $Id: func.c,v 1.41 2004/02/22 17:49:34 drh Exp $
*/ */
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
@@ -147,7 +147,7 @@ static void roundFunc(sqlite_func *context, int argc, const char **argv){
n = argc==2 ? atoi(argv[1]) : 0; n = argc==2 ? atoi(argv[1]) : 0;
if( n>30 ) n = 30; if( n>30 ) n = 30;
if( n<0 ) n = 0; if( n<0 ) n = 0;
r = sqliteAtoF(argv[0]); r = sqliteAtoF(argv[0], 0);
sprintf(zBuf,"%.*f",n,r); sprintf(zBuf,"%.*f",n,r);
sqlite_set_result_string(context, zBuf, -1); sqlite_set_result_string(context, zBuf, -1);
} }
@@ -408,7 +408,7 @@ static void sumStep(sqlite_func *context, int argc, const char **argv){
if( argc<1 ) return; if( argc<1 ) return;
p = sqlite_aggregate_context(context, sizeof(*p)); p = sqlite_aggregate_context(context, sizeof(*p));
if( p && argv[0] ){ if( p && argv[0] ){
p->sum += sqliteAtoF(argv[0]); p->sum += sqliteAtoF(argv[0], 0);
p->cnt++; p->cnt++;
} }
} }
@@ -446,7 +446,7 @@ static void stdDevStep(sqlite_func *context, int argc, const char **argv){
if( argc<1 ) return; if( argc<1 ) return;
p = sqlite_aggregate_context(context, sizeof(*p)); p = sqlite_aggregate_context(context, sizeof(*p));
if( p && argv[0] ){ if( p && argv[0] ){
x = sqliteAtoF(argv[0]); x = sqliteAtoF(argv[0], 0);
p->sum += x; p->sum += x;
p->sum2 += x*x; p->sum2 += x*x;
p->cnt++; p->cnt++;

View File

@@ -11,7 +11,7 @@
************************************************************************* *************************************************************************
** Internal interface definitions for SQLite. ** Internal interface definitions for SQLite.
** **
** @(#) $Id: sqliteInt.h,v 1.217 2004/02/21 19:02:30 drh Exp $ ** @(#) $Id: sqliteInt.h,v 1.218 2004/02/22 17:49:34 drh Exp $
*/ */
#include "config.h" #include "config.h"
#include "sqlite.h" #include "sqlite.h"
@@ -1257,6 +1257,6 @@ int sqliteFixSelect(DbFixer*, Select*);
int sqliteFixExpr(DbFixer*, Expr*); int sqliteFixExpr(DbFixer*, Expr*);
int sqliteFixExprList(DbFixer*, ExprList*); int sqliteFixExprList(DbFixer*, ExprList*);
int sqliteFixTriggerStep(DbFixer*, TriggerStep*); int sqliteFixTriggerStep(DbFixer*, TriggerStep*);
double sqliteAtoF(const char *z); double sqliteAtoF(const char *z, const char **);
char *sqlite_snprintf(int,char*,const char*,...); char *sqlite_snprintf(int,char*,const char*,...);
int sqliteFitsIn32Bits(const char *); int sqliteFitsIn32Bits(const char *);

View File

@@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated ** is not included in the SQLite library. It is used for automated
** testing of the SQLite library. ** testing of the SQLite library.
** **
** $Id: test1.c,v 1.35 2004/02/21 19:41:04 drh Exp $ ** $Id: test1.c,v 1.36 2004/02/22 17:49:34 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "tcl.h" #include "tcl.h"
@@ -640,7 +640,7 @@ static void testFunc(sqlite_func *context, int argc, const char **argv){
}else if( sqliteStrICmp(argv[0],"int")==0 ){ }else if( sqliteStrICmp(argv[0],"int")==0 ){
sqlite_set_result_int(context, atoi(argv[1])); sqlite_set_result_int(context, atoi(argv[1]));
}else if( sqliteStrICmp(argv[0],"double")==0 ){ }else if( sqliteStrICmp(argv[0],"double")==0 ){
sqlite_set_result_double(context, sqliteAtoF(argv[1])); sqlite_set_result_double(context, sqliteAtoF(argv[1], 0));
}else{ }else{
sqlite_set_result_error(context,"first argument should be one of: " sqlite_set_result_error(context,"first argument should be one of: "
"string int double", -1); "string int double", -1);

View File

@@ -14,7 +14,7 @@
** This file contains functions for allocating memory, comparing ** This file contains functions for allocating memory, comparing
** strings, and stuff like that. ** strings, and stuff like that.
** **
** $Id: util.c,v 1.73 2004/02/21 19:02:31 drh Exp $ ** $Id: util.c,v 1.74 2004/02/22 17:49:34 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include <stdarg.h> #include <stdarg.h>
@@ -553,7 +553,7 @@ int sqliteIsNumber(const char *z){
** of "." depending on how locale is set. But that would cause problems ** of "." depending on how locale is set. But that would cause problems
** for SQL. So this routine always uses "." regardless of locale. ** for SQL. So this routine always uses "." regardless of locale.
*/ */
double sqliteAtoF(const char *z){ double sqliteAtoF(const char *z, const char **pzEnd){
int sign = 1; int sign = 1;
LONGDOUBLE_TYPE v1 = 0.0; LONGDOUBLE_TYPE v1 = 0.0;
if( *z=='-' ){ if( *z=='-' ){
@@ -601,6 +601,7 @@ double sqliteAtoF(const char *z){
v1 *= scale; v1 *= scale;
} }
} }
if( pzEnd ) *pzEnd = z;
return sign<0 ? -v1 : v1; return sign<0 ? -v1 : v1;
} }
@@ -650,8 +651,8 @@ int sqliteCompare(const char *atext, const char *btext){
result = -1; result = -1;
}else{ }else{
double rA, rB; double rA, rB;
rA = sqliteAtoF(atext); rA = sqliteAtoF(atext, 0);
rB = sqliteAtoF(btext); rB = sqliteAtoF(btext, 0);
if( rA<rB ){ if( rA<rB ){
result = -1; result = -1;
}else if( rA>rB ){ }else if( rA>rB ){
@@ -743,8 +744,8 @@ int sqliteSortCompare(const char *a, const char *b){
res = -1; res = -1;
break; break;
} }
rA = sqliteAtoF(&a[1]); rA = sqliteAtoF(&a[1], 0);
rB = sqliteAtoF(&b[1]); rB = sqliteAtoF(&b[1], 0);
if( rA<rB ){ if( rA<rB ){
res = -1; res = -1;
break; break;

View File

@@ -43,7 +43,7 @@
** in this file for details. If in doubt, do not deviate from existing ** in this file for details. If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code. ** commenting and indentation practices when changing or adding code.
** **
** $Id: vdbe.c,v 1.266 2004/02/20 22:53:39 rdc Exp $ ** $Id: vdbe.c,v 1.267 2004/02/22 17:49:34 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include "os.h" #include "os.h"
@@ -335,7 +335,7 @@ static void hardIntegerify(Mem *pStack){
#define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); } #define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); }
static void hardRealify(Mem *pStack){ static void hardRealify(Mem *pStack){
if( pStack->flags & MEM_Str ){ if( pStack->flags & MEM_Str ){
pStack->r = sqliteAtoF(pStack->z); pStack->r = sqliteAtoF(pStack->z, 0);
}else if( pStack->flags & MEM_Int ){ }else if( pStack->flags & MEM_Int ){
pStack->r = pStack->i; pStack->r = pStack->i;
}else{ }else{
@@ -2039,7 +2039,7 @@ case OP_MakeKey: {
if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){ if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){
pRec->r = pRec->i; pRec->r = pRec->i;
}else if( (flags & (MEM_Real|MEM_Int))==0 ){ }else if( (flags & (MEM_Real|MEM_Int))==0 ){
pRec->r = sqliteAtoF(pRec->z); pRec->r = sqliteAtoF(pRec->z, 0);
} }
Release(pRec); Release(pRec);
z = pRec->zShort; z = pRec->zShort;