]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add localtime<-->UTC conversions to the date functions. (CVS 1146)
authordrh <drh@noemail.net>
Tue, 23 Dec 2003 16:22:18 +0000 (16:22 +0000)
committerdrh <drh@noemail.net>
Tue, 23 Dec 2003 16:22:18 +0000 (16:22 +0000)
FossilOrigin-Name: 8482b8c44766e7f80fc449b3dbdd3f37396c332b

manifest
manifest.uuid
src/date.c
test/date.test

index 72d6fba62c2619f05fbb7b88e129720b6af16305..01f2cb404446f337406a38a021447e0f5991b453 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Test\sfunctions\stolerate\san\s"0x"\sbefore\sa\spointer\svalue.\s\sTicket\s#452.\s(CVS\s1145)
-D 2003-12-23T03:06:23
+C Add\slocaltime<-->UTC\sconversions\sto\sthe\sdate\sfunctions.\s(CVS\s1146)
+D 2003-12-23T16:22:18
 F Makefile.in 0515ff9218ad8d5a8f6220f0494b8ef94c67013b
 F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@@ -28,7 +28,7 @@ F src/btree.h 9b7c09f1e64274d7bb74a57bbfc63778f67b1048
 F src/btree_rb.c e4084b6a12270674b0cd7034655f55e6a2639c78
 F src/build.c a7493c433de5b552f9535d8fa7ed80aaf135491e
 F src/copy.c 9e47975ea96751c658bcf1a0c4f0bb7c6ee61e73
-F src/date.c ec36ce6f044fbc6c837c5bc52e71cdba0ea2746e
+F src/date.c 1e3318d8450088f235442ca1cddd2601c0c32d1a
 F src/delete.c 0f81e6799c089487615d38e042a2de4d2d6192bc
 F src/encode.c 25ea901a9cefb3d93774afa4a06b57cb58acf544
 F src/expr.c a14401a54e5923f3e52b6d04a83813d150f43f33
@@ -84,7 +84,7 @@ F test/btree4rb.test ae6f0438512edcb45cf483471cd6070a765963a9
 F test/capi2.test ec96e0e235d87b53cbaef3d8e3e0f8ccf32c71ca
 F test/conflict.test 0911bb2f079046914a6e9c3341b36658c4e2103e
 F test/copy.test 88dabd4e811b17644b726aa81d404e73b7635c84
-F test/date.test 17619ff81d5b813092915927c50923e265e85bd8
+F test/date.test 510cf3dbc0bc090fc28b2ae1bc290dcd920f5414
 F test/delete.test 92256384f1801760180ded129f7427884cf28886
 F test/expr.test c4cc292d601019c2f2ce95093caaa5d10284b105
 F test/fkey1.test d65c824459916249bee501532d6154ddab0b5db7
@@ -179,7 +179,7 @@ F www/speed.tcl 2f6b1155b99d39adb185f900456d1d592c4832b3
 F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604
 F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
 F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
-P 4d9edbc50f7dee64edbadad2e2dc4f93d8248b3b
-R e78cc5676ccc4eae1ef13e1cbefdf7e8
+P c6c5e07b65ae1c30117f0276a1002d5036697cf1
+R bb5987106040b561c6e78128774239d9
 U drh
-Z 2c02f4b435e2dadedef60f34ed1da32e
+Z cdbbb6875dbb4ed10b1b0f72076527c5
index ea629d6223d8bcc0caea2c4867d93454db43082e..ade2952c13f3b8accbe65d3fe87a0e653cc318fc 100644 (file)
@@ -1 +1 @@
-c6c5e07b65ae1c30117f0276a1002d5036697cf1
\ No newline at end of file
+8482b8c44766e7f80fc449b3dbdd3f37396c332b
\ No newline at end of file
index 26fc8a3f89bed20e2f39b2493ecf667806a8e822..f607ecbae896820c1470d1b301b77eb3090a30d7 100644 (file)
@@ -16,7 +16,7 @@
 ** sqliteRegisterDateTimeFunctions() found at the bottom of the file.
 ** All other code has file scope.
 **
-** $Id: date.c,v 1.2 2003/12/23 02:17:35 drh Exp $
+** $Id: date.c,v 1.3 2003/12/23 16:22:18 drh Exp $
 **
 ** NOTES:
 **
@@ -51,6 +51,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <assert.h>
+#include <time.h>
 #include "sqliteInt.h"
 #include "os.h"
 
@@ -367,6 +368,52 @@ static void computeHMS(DateTime *p){
   p->validHMS = 1;
 }
 
+/*
+** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
+** for the time value p where p is in UTC.
+*/
+static double localtimeOffset(DateTime *p){
+  DateTime x, y;
+  time_t t;
+  struct tm *pTm;
+  computeYMD(p);
+  computeHMS(p);
+  x = *p;
+  if( x.Y<1971 || x.Y>=2038 ){
+    x.Y = 2000;
+    x.M = 1;
+    x.D = 1;
+    x.h = 0;
+    x.m = 0;
+    x.s = 0.0;
+  } else {
+    int s = x.s + 0.5;
+    x.s = s;
+  }
+  x.tz = 0;
+  x.validJD = 0;
+  computeJD(&x);
+  t = (x.rJD-2440587.5)*86400.0 + 0.5;
+  sqliteOsEnterMutex();
+  pTm = localtime(&t);
+  y.Y = pTm->tm_year + 1900;
+  y.M = pTm->tm_mon + 1;
+  y.D = pTm->tm_mday;
+  y.h = pTm->tm_hour;
+  y.m = pTm->tm_min;
+  y.s = pTm->tm_sec;
+  sqliteOsLeaveMutex();
+  y.validYMD = 1;
+  y.validHMS = 1;
+  y.validJD = 0;
+  y.validTZ = 0;
+  computeJD(&y);
+  /* printf("x=%d-%02d-%02d %02d:%02d:%02d\n",x.Y,x.M,x.D,x.h,x.m,(int)x.s); */
+  /* printf("y=%d-%02d-%02d %02d:%02d:%02d\n",y.Y,y.M,y.D,y.h,y.m,(int)y.s); */
+  /* printf("diff=%.17g\n", y.rJD - x.rJD); */
+  return y.rJD - x.rJD;
+}
+
 /*
 ** Process a modifier to a date-time stamp.  The modifiers are
 ** as follows:
@@ -383,6 +430,8 @@ static void computeHMS(DateTime *p){
 **     start of day
 **     weekday N
 **     unixepoch
+**     localtime
+**     utc
 **
 ** Return 0 on success and 1 if there is any kind of error.
 */
@@ -396,6 +445,22 @@ static int parseModifier(const char *zMod, DateTime *p){
   }
   z[n] = 0;
   switch( z[0] ){
+    case 'l': {
+      /*    localtime
+      **
+      ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
+      ** show local time.
+      */
+      if( strcmp(z, "localtime")==0 ){
+        computeJD(p);
+        p->rJD += localtimeOffset(p);
+        p->validYMD = 0;
+        p->validHMS = 0;
+        p->validTZ = 0;
+        rc = 0;
+      }
+      break;
+    }
     case 'u': {
       /*
       **    unixepoch
@@ -409,6 +474,19 @@ static int parseModifier(const char *zMod, DateTime *p){
         p->validHMS = 0;
         p->validTZ = 0;
         rc = 0;
+      }else if( strcmp(z, "utc")==0 ){
+        double c1;
+        computeJD(p);
+        c1 = localtimeOffset(p);
+        p->rJD -= c1;
+        p->validYMD = 0;
+        p->validHMS = 0;
+        p->validTZ = 0;
+        p->rJD += c1 - localtimeOffset(p);
+        p->validYMD = 0;
+        p->validHMS = 0;
+        p->validTZ = 0;
+        rc = 0;
       }
       break;
     }
index e1cc16069914439fb2e166b197b01e8d6134f2a0..9a554cf5c9bb4e2bfdd7fdecc258c5801a3449c5 100644 (file)
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this file is testing date and time functions.
 #
-# $Id: date.test,v 1.1 2003/11/01 01:53:54 drh Exp $
+# $Id: date.test,v 1.2 2003/12/23 16:22:18 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -120,4 +120,36 @@ datetest 5.2 {datetime('1994-04-16 14:00:00 +05:15')} {1994-04-16 19:15:00}
 datetest 5.3 {datetime('1994-04-16 05:00:00 -08:30')} {1994-04-15 20:30:00}
 datetest 5.4 {datetime('1994-04-16 14:00:00 +11:55')} {1994-04-17 01:55:00}
 
+# localtime->utc and utc->localtime conversions.  These tests only work
+# if the localtime is in the US Eastern Time (the time in Charlotte, NC
+# and in New York.)
+#
+if {[clock format 0 -format %Z]=="EST"} {
+  datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\
+      {2000-10-29 01:59:00}
+  datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\
+      {2000-10-29 01:00:00}
+  datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\
+      {2000-04-02 01:59:00}
+  datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\
+      {2000-04-02 03:00:00}
+  datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00}
+  datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00}
+  datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00}
+  datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 06:00:00}
+
+  datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \
+      {2000-01-01 07:00:00}
+  datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \
+      {1969-01-01 07:00:00}
+  datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \
+      {2039-01-01 07:00:00}
+  datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \
+      {2000-07-01 08:00:00}
+  datetest 6.14 {datetime('1969-07-01 12:00:00','localtime')} \
+      {1969-07-01 07:00:00}
+  datetest 6.15 {datetime('2039-07-01 12:00:00','localtime')} \
+      {2039-07-01 07:00:00}
+}
+
 finish_test