]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Update to the date functions. (CVS 1110)
authordrh <drh@noemail.net>
Fri, 10 Oct 2003 02:09:56 +0000 (02:09 +0000)
committerdrh <drh@noemail.net>
Fri, 10 Oct 2003 02:09:56 +0000 (02:09 +0000)
FossilOrigin-Name: 06d4e88394217fb1390b069bad82d6ac71981f72

manifest
manifest.uuid
src/func.c
src/vdbeaux.c

index 65914e8b6ae8717dab36f9b5edf38ffcc8b13ef4..3988e9226fa27babfcf6209a59627077d6bdf603 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Allow\squoted\strigger\snames.\s\sTicket\s#468.\s(CVS\s1109)
-D 2003-10-03T00:13:39
+C Update\sto\sthe\sdate\sfunctions.\s(CVS\s1110)
+D 2003-10-10T02:09:57
 F Makefile.in ab585a91e34bc33928a1b6181fa2f6ebd4fb17e1
 F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@@ -31,7 +31,7 @@ F src/copy.c 9e47975ea96751c658bcf1a0c4f0bb7c6ee61e73
 F src/delete.c 0f81e6799c089487615d38e042a2de4d2d6192bc
 F src/encode.c 25ea901a9cefb3d93774afa4a06b57cb58acf544
 F src/expr.c d4d8eca6363a6e680361e5d2a934b78e5c7b7fa3
-F src/func.c 377ea94127351de27892a62a63f931e0fbaa33d4
+F src/func.c fce558b4c1d895e81091d6d5e7d86a192fc8e84c
 F src/hash.c 058f077c1f36f266581aa16f907a3903abf64aa3
 F src/hash.h cd0433998bc1a3759d244e1637fe5a3c13b53bf8
 F src/insert.c dc200ae04a36bd36e575272a069e20c528b7fbdf
@@ -64,7 +64,7 @@ F src/vacuum.c e4724eade07e4cf8897060a8cf632dbd92408eeb
 F src/vdbe.c a9923a38a24ee86dd2e237c9f7e9d0116e329394
 F src/vdbe.h 3957844e46fea71fd030e78f6a3bd2f7e320fb43
 F src/vdbeInt.h 2824bf88895b901b3a8c9e44527c67530e1c0dcb
-F src/vdbeaux.c 1145fa169021d7fb3962fab6e99f5f8fc2608f8a
+F src/vdbeaux.c 31abb8e3e57866913360381947e267a51fed92c6
 F src/where.c 6bd1d2a9c70af63a6e47b0ab0c181d501b12514f
 F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242
 F test/attach.test c26848402e7ac829e043e1fa5e0eb87032e5d81d
@@ -173,7 +173,7 @@ F www/speed.tcl 2f6b1155b99d39adb185f900456d1d592c4832b3
 F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604
 F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
 F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
-P 95b27ebd1322a877112dee514dffddb0937e45fa
-R d77f051e1f691ee16251e75ff2e4c416
+P 54aa0fb236d17b53b194a667d68c71007c8e7687
+R 78b719cb974e90d3bbfc839a1c83b68b
 U drh
-Z 5b735f186bb04845c510a554b61d2298
+Z 6362f853c8fee8f874044b89744bb342
index 0923ce583ce6772050d2682dbb54b4452a61dd8e..eae8b6bc94a346d9d6d8aa6f2d35bd0ff396d502 100644 (file)
@@ -1 +1 @@
-54aa0fb236d17b53b194a667d68c71007c8e7687
\ No newline at end of file
+06d4e88394217fb1390b069bad82d6ac71981f72
\ No newline at end of file
index 2e3c20728e27c609a8b7c5d166ebf8207575fe66..f3c4d1d853e027f907abcf8845f69c2acb1c237b 100644 (file)
@@ -16,7 +16,7 @@
 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 ** All other code has file scope.
 **
-** $Id: func.c,v 1.31 2003/09/06 01:10:47 drh Exp $
+** $Id: func.c,v 1.32 2003/10/10 02:09:57 drh Exp $
 */
 #include <ctype.h>
 #include <math.h>
@@ -586,13 +586,59 @@ static int getDigits(const char *zDate, int N){
 }
 
 /*
-** Parse times of the form HH:MM:SS or HH:MM.  Store the
-** result (in days) in *prJD.
+** Parse a timezone extension on the end of a datetime stamp.
+** The extension is of the form:
+**
+**        (+/-)HH:MM
+**
+** If the parse is successful, write the number of minutes
+** of change in *pnMin and return 0.  If a parser error occurs,
+** return 0.
+**
+** A missing specifier is not considered an error.
+*/
+static int parseTimezone(const char *zDate, int *pnMin){
+  int sgn = 0;
+  int nHr, nMn;
+  while( isspace(*zDate) ){ zDate++; }
+  *pnMin = 0;
+  if( *zDate=='-' ){
+    sgn = -1;
+  }else if( *zDate=='+' ){
+    sgn = +1;
+  }else{
+    return *zDate!=0;
+  }
+  zDate++;
+  nHr = getDigits(zDate, 2);
+  if( nHr<0 || nHr>14 ) return 1;
+  zDate += 2;
+  if( zDate[0]!=':' ) return 1;
+  zDate++;
+  nMn = getDigits(zDate, 2);
+  if( nMn<0 || nMn>59 ) return 1;
+  zDate += 2;
+  *pnMin = sgn*(nMn + nHr*60);
+  while( isspace(*zDate) ){ *zDate++; }
+  return *zDate!=0;
+}
+
+/*
+** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
+** The HH, MM, and SS must each be exactly 2 digits.  The
+** fractional seconds FFFF can be one or more digits.
+**
+** The time string can be followed by an optional timezone specifier
+** of the following form:  (+/-)HH:MM.
+**
+** Whatever the format, the string is converted into a julian
+** day number and stored in *prJD.
 **
 ** Return 1 if there is a parsing error and 0 on success.
 */
 static int parseHhMmSs(const char *zDate, double *prJD){
-  int h, m, s;
+  int h, m, s, tz;
+  double ms = 0.0;
   h = getDigits(zDate, 2);
   if( h<0 || zDate[2]!=':' ) return 1;
   zDate += 3;
@@ -603,11 +649,20 @@ static int parseHhMmSs(const char *zDate, double *prJD){
     s = getDigits(&zDate[1], 2);
     if( s<0 || s>59 ) return 1;
     zDate += 3;
+    if( *zDate=='.' && isdigit(zDate[1]) ){
+      double rScale = 1.0/864000.0;
+      zDate++;
+      while( isdigit(*zDate) ){
+        ms += rScale * (*zDate - '0');
+        rScale *= 0.1;
+        zDate++;
+      }
+    }
   }else{
     s = 0;
   }
-  while( isspace(*zDate) ){ zDate++; }
-  *prJD = (h*3600.0 + m*60.0 + s)/86400.0;
+  if( parseTimezone(zDate, &tz) ) return 1;
+  *prJD = (h*3600.0 + (m+tz)*60.0 + s)/86400.0 + ms;
   return 0;
 }
 
@@ -666,21 +721,30 @@ static int parseYyyyMmDd(const char *zDate, double *prJD){
 **
 ** The following are acceptable forms for the input string:
 **
-**      YYYY-MM-DD
-**      YYYY-MM-DD HH:MM
-**      YYYY-MM-DD HH:MM:SS
-**      HH:MM
-**      HH:MM:SS
+**      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
 **      DDDD.DD 
 **      now
+**
+** In the first form, the +/-HH:MM is always optional.  The fractional
+** seconds extension (the ".FFF") is optional.  The seconds portion
+** (":SS.FFF") is option.  The year and date can be omitted as long
+** as there is a time string.  The time string can be omitted as long
+** as there is a year and date.
+**
+** If the bRelative flag is set and the format is HH:MM or HH:MM:SS then
+** make the result is relative to midnight instead of noon.  In other words,
+** if bRelative is true, "00:00:00" parses to 0.0 but if bRelative is
+** false, "00:00:00" parses to 0.5.
 */
-static int parseDateOrTime(const char *zDate, double *prJD){
+static int parseDateOrTime(const char *zDate, int bRelative, double *prJD){
   int i;
   for(i=0; isdigit(zDate[i]); i++){}
   if( i==4 && zDate[i]=='-' ){
     return parseYyyyMmDd(zDate, prJD);
   }else if( i==2 && zDate[i]==':' ){
-    return parseHhMmSs(zDate, prJD);
+    if( parseHhMmSs(zDate, prJD) ) return 1;
+    if( !bRelative ) *prJD += 2451544.5;
+    return 0;
   }else if( i==0 && sqliteStrICmp(zDate,"now")==0 ){
     return sqliteOsCurrentTime(prJD);
   }else if( sqliteIsNumber(zDate) ){
@@ -697,7 +761,8 @@ typedef struct DateTime DateTime;
 struct DateTime {
   double rJD;    /* The julian day number */
   int Y, M, D;   /* Year, month, and day */
-  int h, m, s;   /* Hour minute and second */
+  int h, m;      /* Hour and minutes */
+  double s;      /* Seconds */
 };
 
 /*
@@ -728,11 +793,14 @@ static void decomposeDate(DateTime *p, int mode){
     p->Y = p->M>2 ? C - 4716 : C - 4715;
   }
   if( mode & 2 ){
-    p->s = (p->rJD + 0.5 - Z)*86400.0;
-    p->h = p->s/3600;
-    p->s -= p->h*3600;
-    p->m = p->s/60;
-    p->s -= p->m*60;
+    int s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
+    p->s = 0.001*s;
+    s = p->s;
+    p->s -= s;
+    p->h = s/3600;
+    s -= p->h*3600;
+    p->m = s/60;
+    p->s += s - p->m*60;
   }
 }
 
@@ -754,7 +822,7 @@ static int isDate(int argc, const char **argv, DateTime *p, int mode){
   p->rJD = 0.0;
   for(i=0; i<argc; i++){
     if( argv[i]==0 ) return 0;
-    if( parseDateOrTime(argv[i], &r) ) return 0;
+    if( parseDateOrTime(argv[i], i, &r) ) return 0;
     p->rJD += r;
   }
   decomposeDate(p, mode);
@@ -776,7 +844,8 @@ static void timestampFunc(sqlite_func *context, int argc, const char **argv){
   DateTime x;
   if( isDate(argc, argv, &x, 3) ){
     char zBuf[100];
-    sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m, x.s);
+    sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
+           (int)(x.s+0.5));
     sqlite_set_result_string(context, zBuf, -1);
   }
 }
@@ -784,7 +853,7 @@ static void timeFunc(sqlite_func *context, int argc, const char **argv){
   DateTime x;
   if( isDate(argc, argv, &x, 2) ){
     char zBuf[100];
-    sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, x.s);
+    sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)(x.s+0.5));
     sqlite_set_result_string(context, zBuf, -1);
   }
 }
@@ -824,7 +893,7 @@ static void dayofmonthFunc(sqlite_func *context, int argc, const char **argv){
 static void secondFunc(sqlite_func *context, int argc, const char **argv){
   DateTime x;
   if( isDate(argc, argv, &x, 2) ){
-    sqlite_set_result_int(context, x.s);
+    sqlite_set_result_double(context, x.s);
   }
 }
 static void minuteFunc(sqlite_func *context, int argc, const char **argv){
@@ -839,6 +908,16 @@ static void hourFunc(sqlite_func *context, int argc, const char **argv){
     sqlite_set_result_int(context, x.h);
   }
 }
+static void unixToJdFunc(sqlite_func *context, int argc, const char **argv){
+  sqlite_set_result_double(context, atof(argv[0])/(24.0*3600.0)+2440587.5);
+}
+static void unixtimeFunc(sqlite_func *context, int argc, const char **argv){
+  DateTime x;
+  if( isDate(argc, argv, &x, 0) ){
+    sqlite_set_result_double(context, (x.rJD-2440587.5)*24.0*3600.0);
+  }
+}
+
 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
 /***************************************************************************/
 
@@ -877,6 +956,8 @@ void sqliteRegisterBuiltinFunctions(sqlite *db){
     { "quote",      1, SQLITE_ARGS,    quoteFunc  },
 #ifndef SQLITE_OMIT_DATETIME_FUNCS
     { "julianday", -1, SQLITE_NUMERIC, juliandayFunc   },
+    { "unixtime",  -1, SQLITE_NUMERIC, unixtimeFunc    },
+    { "unix_to_jd", 1, SQLITE_NUMERIC, unixToJdFunc    },
     { "timestamp", -1, SQLITE_TEXT,    timestampFunc   },
     { "time",      -1, SQLITE_TEXT,    timeFunc        },
     { "date",      -1, SQLITE_TEXT,    dateFunc        },
index fe9e210d751779d008f2c4793d1c0f54d151b64b..beeedd474e83a3687ba2d234b95ef5dbba613acd 100644 (file)
@@ -423,10 +423,6 @@ void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){
 /*
 ** Extract the user data from a sqlite_func structure and return a
 ** pointer to it.
-**
-** This routine is defined here in vdbe.c because it depends on knowing
-** the internals of the sqlite_func structure which is only defined in 
-** this source file.
 */
 void *sqlite_user_data(sqlite_func *p){
   assert( p && p->pFunc );