summaryrefslogtreecommitdiff
path: root/ext/sqlite/libsqlite/src/date.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/sqlite/libsqlite/src/date.c')
-rw-r--r--ext/sqlite/libsqlite/src/date.c72
1 files changed, 33 insertions, 39 deletions
diff --git a/ext/sqlite/libsqlite/src/date.c b/ext/sqlite/libsqlite/src/date.c
index 9a0df401ba..64fc3bccdf 100644
--- a/ext/sqlite/libsqlite/src/date.c
+++ b/ext/sqlite/libsqlite/src/date.c
@@ -221,7 +221,7 @@ static void computeJD(DateTime *p){
M = p->M;
D = p->D;
}else{
- Y = 2000;
+ Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
M = 1;
D = 1;
}
@@ -369,6 +369,23 @@ static void computeHMS(DateTime *p){
}
/*
+** Compute both YMD and HMS
+*/
+static void computeYMD_HMS(DateTime *p){
+ computeYMD(p);
+ computeHMS(p);
+}
+
+/*
+** Clear the YMD and HMS and the TZ
+*/
+static void clearYMD_HMS_TZ(DateTime *p){
+ p->validYMD = 0;
+ p->validHMS = 0;
+ p->validTZ = 0;
+}
+
+/*
** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
** for the time value p where p is in UTC.
*/
@@ -376,9 +393,8 @@ static double localtimeOffset(DateTime *p){
DateTime x, y;
time_t t;
struct tm *pTm;
- computeYMD(p);
- computeHMS(p);
x = *p;
+ computeYMD_HMS(&x);
if( x.Y<1971 || x.Y>=2038 ){
x.Y = 2000;
x.M = 1;
@@ -408,9 +424,6 @@ static double localtimeOffset(DateTime *p){
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;
}
@@ -454,9 +467,7 @@ static int parseModifier(const char *zMod, DateTime *p){
if( strcmp(z, "localtime")==0 ){
computeJD(p);
p->rJD += localtimeOffset(p);
- p->validYMD = 0;
- p->validHMS = 0;
- p->validTZ = 0;
+ clearYMD_HMS_TZ(p);
rc = 0;
}
break;
@@ -470,22 +481,15 @@ static int parseModifier(const char *zMod, DateTime *p){
*/
if( strcmp(z, "unixepoch")==0 && p->validJD ){
p->rJD = p->rJD/86400.0 + 2440587.5;
- p->validYMD = 0;
- p->validHMS = 0;
- p->validTZ = 0;
+ clearYMD_HMS_TZ(p);
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;
+ clearYMD_HMS_TZ(p);
p->rJD += c1 - localtimeOffset(p);
- p->validYMD = 0;
- p->validHMS = 0;
- p->validTZ = 0;
rc = 0;
}
break;
@@ -494,16 +498,14 @@ static int parseModifier(const char *zMod, DateTime *p){
/*
** weekday N
**
- ** Move the date to the beginning of the next occurrance of
+ ** Move the date to the same time on the next occurrance of
** weekday N where 0==Sunday, 1==Monday, and so forth. If the
- ** date is already on the appropriate weekday, this is equivalent
- ** to "start of day".
+ ** date is already on the appropriate weekday, this is a no-op.
*/
if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
&& (n=r)==r && n>=0 && r<7 ){
int Z;
- computeYMD(p);
- p->validHMS = 0;
+ computeYMD_HMS(p);
p->validTZ = 0;
p->validJD = 0;
computeJD(p);
@@ -511,8 +513,7 @@ static int parseModifier(const char *zMod, DateTime *p){
Z %= 7;
if( Z>n ) Z -= 7;
p->rJD += n - Z;
- p->validYMD = 0;
- p->validHMS = 0;
+ clearYMD_HMS_TZ(p);
rc = 0;
}
break;
@@ -570,17 +571,14 @@ static int parseModifier(const char *zMod, DateTime *p){
if( n==3 && strcmp(z,"day")==0 ){
p->rJD += r;
}else if( n==4 && strcmp(z,"hour")==0 ){
- computeJD(p);
p->rJD += r/24.0;
}else if( n==6 && strcmp(z,"minute")==0 ){
- computeJD(p);
p->rJD += r/(24.0*60.0);
}else if( n==6 && strcmp(z,"second")==0 ){
- computeJD(p);
p->rJD += r/(24.0*60.0*60.0);
}else if( n==5 && strcmp(z,"month")==0 ){
int x, y;
- computeYMD(p);
+ computeYMD_HMS(p);
p->M += r;
x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
p->Y += x;
@@ -592,16 +590,14 @@ static int parseModifier(const char *zMod, DateTime *p){
p->rJD += (r - y)*30.0;
}
}else if( n==4 && strcmp(z,"year")==0 ){
- computeYMD(p);
+ computeYMD_HMS(p);
p->Y += r;
p->validJD = 0;
computeJD(p);
}else{
rc = 1;
}
- p->validYMD = 0;
- p->validHMS = 0;
- p->validTZ = 0;
+ clearYMD_HMS_TZ(p);
break;
}
default: {
@@ -655,8 +651,7 @@ static void datetimeFunc(sqlite_func *context, int argc, const char **argv){
DateTime x;
if( isDate(argc, argv, &x)==0 ){
char zBuf[100];
- computeYMD(&x);
- computeHMS(&x);
+ computeYMD_HMS(&x);
sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
(int)(x.s));
sqlite_set_result_string(context, zBuf, -1);
@@ -759,8 +754,7 @@ static void strftimeFunc(sqlite_func *context, int argc, const char **argv){
if( z==0 ) return;
}
computeJD(&x);
- computeYMD(&x);
- computeHMS(&x);
+ computeYMD_HMS(&x);
for(i=j=0; zFmt[i]; i++){
if( zFmt[i]!='%' ){
z[j++] = zFmt[i];
@@ -798,11 +792,11 @@ static void strftimeFunc(sqlite_func *context, int argc, const char **argv){
case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break;
case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break;
case 's': {
- sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0));
+ sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5));
j += strlen(&z[j]);
break;
}
- case 'S': sprintf(&z[j],"%02d",(int)x.s); j+=2; break;
+ case 'S': sprintf(&z[j],"%02d",(int)(x.s+0.5)); j+=2; break;
case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break;
case '%': z[j++] = '%'; break;