| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2003 October 31 | 2 ** 2003 October 31 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 ** Willmann-Bell, Inc | 43 ** Willmann-Bell, Inc |
| 44 ** Richmond, Virginia (USA) | 44 ** Richmond, Virginia (USA) |
| 45 */ | 45 */ |
| 46 #include "sqliteInt.h" | 46 #include "sqliteInt.h" |
| 47 #include <stdlib.h> | 47 #include <stdlib.h> |
| 48 #include <assert.h> | 48 #include <assert.h> |
| 49 #include <time.h> | 49 #include <time.h> |
| 50 | 50 |
| 51 #ifndef SQLITE_OMIT_DATETIME_FUNCS | 51 #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 52 | 52 |
| 53 /* |
| 54 ** The MSVC CRT on Windows CE may not have a localtime() function. |
| 55 ** So declare a substitute. The substitute function itself is |
| 56 ** defined in "os_win.c". |
| 57 */ |
| 58 #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ |
| 59 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) |
| 60 struct tm *__cdecl localtime(const time_t *); |
| 61 #endif |
| 53 | 62 |
| 54 /* | 63 /* |
| 55 ** A structure for holding a single date and time. | 64 ** A structure for holding a single date and time. |
| 56 */ | 65 */ |
| 57 typedef struct DateTime DateTime; | 66 typedef struct DateTime DateTime; |
| 58 struct DateTime { | 67 struct DateTime { |
| 59 sqlite3_int64 iJD; /* The julian day number times 86400000 */ | 68 sqlite3_int64 iJD; /* The julian day number times 86400000 */ |
| 60 int Y, M, D; /* Year, month, and day */ | 69 int Y, M, D; /* Year, month, and day */ |
| 61 int h, m; /* Hour and minutes */ | 70 int h, m; /* Hour and minutes */ |
| 62 int tz; /* Timezone offset in minutes */ | 71 int tz; /* Timezone offset in minutes */ |
| 63 double s; /* Seconds */ | 72 double s; /* Seconds */ |
| 64 char validYMD; /* True (1) if Y,M,D are valid */ | 73 char validJD; /* True (1) if iJD is valid */ |
| 65 char validHMS; /* True (1) if h,m,s are valid */ | 74 char rawS; /* Raw numeric value stored in s */ |
| 66 char validJD; /* True (1) if iJD is valid */ | 75 char validYMD; /* True (1) if Y,M,D are valid */ |
| 67 char validTZ; /* True (1) if tz is valid */ | 76 char validHMS; /* True (1) if h,m,s are valid */ |
| 68 char tzSet; /* Timezone was set explicitly */ | 77 char validTZ; /* True (1) if tz is valid */ |
| 78 char tzSet; /* Timezone was set explicitly */ |
| 79 char isError; /* An overflow has occurred */ |
| 69 }; | 80 }; |
| 70 | 81 |
| 71 | 82 |
| 72 /* | 83 /* |
| 73 ** Convert zDate into one or more integers. Additional arguments | 84 ** Convert zDate into one or more integers according to the conversion |
| 74 ** come in groups of 5 as follows: | 85 ** specifier zFormat. |
| 75 ** | 86 ** |
| 76 ** N number of digits in the integer | 87 ** zFormat[] contains 4 characters for each integer converted, except for |
| 77 ** min minimum allowed value of the integer | 88 ** the last integer which is specified by three characters. The meaning |
| 78 ** max maximum allowed value of the integer | 89 ** of a four-character format specifiers ABCD is: |
| 79 ** nextC first character after the integer | |
| 80 ** pVal where to write the integers value. | |
| 81 ** | 90 ** |
| 82 ** Conversions continue until one with nextC==0 is encountered. | 91 ** A: number of digits to convert. Always "2" or "4". |
| 92 ** B: minimum value. Always "0" or "1". |
| 93 ** C: maximum value, decoded as: |
| 94 ** a: 12 |
| 95 ** b: 14 |
| 96 ** c: 24 |
| 97 ** d: 31 |
| 98 ** e: 59 |
| 99 ** f: 9999 |
| 100 ** D: the separator character, or \000 to indicate this is the |
| 101 ** last number to convert. |
| 102 ** |
| 103 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would |
| 104 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-". |
| 105 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates |
| 106 ** the 2-digit day which is the last integer in the set. |
| 107 ** |
| 83 ** The function returns the number of successful conversions. | 108 ** The function returns the number of successful conversions. |
| 84 */ | 109 */ |
| 85 static int getDigits(const char *zDate, ...){ | 110 static int getDigits(const char *zDate, const char *zFormat, ...){ |
| 111 /* The aMx[] array translates the 3rd character of each format |
| 112 ** spec into a max size: a b c d e f */ |
| 113 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 }; |
| 86 va_list ap; | 114 va_list ap; |
| 87 int val; | |
| 88 int N; | |
| 89 int min; | |
| 90 int max; | |
| 91 int nextC; | |
| 92 int *pVal; | |
| 93 int cnt = 0; | 115 int cnt = 0; |
| 94 va_start(ap, zDate); | 116 char nextC; |
| 117 va_start(ap, zFormat); |
| 95 do{ | 118 do{ |
| 96 N = va_arg(ap, int); | 119 char N = zFormat[0] - '0'; |
| 97 min = va_arg(ap, int); | 120 char min = zFormat[1] - '0'; |
| 98 max = va_arg(ap, int); | 121 int val = 0; |
| 99 nextC = va_arg(ap, int); | 122 u16 max; |
| 100 pVal = va_arg(ap, int*); | 123 |
| 124 assert( zFormat[2]>='a' && zFormat[2]<='f' ); |
| 125 max = aMx[zFormat[2] - 'a']; |
| 126 nextC = zFormat[3]; |
| 101 val = 0; | 127 val = 0; |
| 102 while( N-- ){ | 128 while( N-- ){ |
| 103 if( !sqlite3Isdigit(*zDate) ){ | 129 if( !sqlite3Isdigit(*zDate) ){ |
| 104 goto end_getDigits; | 130 goto end_getDigits; |
| 105 } | 131 } |
| 106 val = val*10 + *zDate - '0'; | 132 val = val*10 + *zDate - '0'; |
| 107 zDate++; | 133 zDate++; |
| 108 } | 134 } |
| 109 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ | 135 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){ |
| 110 goto end_getDigits; | 136 goto end_getDigits; |
| 111 } | 137 } |
| 112 *pVal = val; | 138 *va_arg(ap,int*) = val; |
| 113 zDate++; | 139 zDate++; |
| 114 cnt++; | 140 cnt++; |
| 141 zFormat += 4; |
| 115 }while( nextC ); | 142 }while( nextC ); |
| 116 end_getDigits: | 143 end_getDigits: |
| 117 va_end(ap); | 144 va_end(ap); |
| 118 return cnt; | 145 return cnt; |
| 119 } | 146 } |
| 120 | 147 |
| 121 /* | 148 /* |
| 122 ** Parse a timezone extension on the end of a date-time. | 149 ** Parse a timezone extension on the end of a date-time. |
| 123 ** The extension is of the form: | 150 ** The extension is of the form: |
| 124 ** | 151 ** |
| (...skipping 20 matching lines...) Expand all Loading... |
| 145 sgn = -1; | 172 sgn = -1; |
| 146 }else if( c=='+' ){ | 173 }else if( c=='+' ){ |
| 147 sgn = +1; | 174 sgn = +1; |
| 148 }else if( c=='Z' || c=='z' ){ | 175 }else if( c=='Z' || c=='z' ){ |
| 149 zDate++; | 176 zDate++; |
| 150 goto zulu_time; | 177 goto zulu_time; |
| 151 }else{ | 178 }else{ |
| 152 return c!=0; | 179 return c!=0; |
| 153 } | 180 } |
| 154 zDate++; | 181 zDate++; |
| 155 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ | 182 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){ |
| 156 return 1; | 183 return 1; |
| 157 } | 184 } |
| 158 zDate += 5; | 185 zDate += 5; |
| 159 p->tz = sgn*(nMn + nHr*60); | 186 p->tz = sgn*(nMn + nHr*60); |
| 160 zulu_time: | 187 zulu_time: |
| 161 while( sqlite3Isspace(*zDate) ){ zDate++; } | 188 while( sqlite3Isspace(*zDate) ){ zDate++; } |
| 162 p->tzSet = 1; | 189 p->tzSet = 1; |
| 163 return *zDate!=0; | 190 return *zDate!=0; |
| 164 } | 191 } |
| 165 | 192 |
| 166 /* | 193 /* |
| 167 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. | 194 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. |
| 168 ** The HH, MM, and SS must each be exactly 2 digits. The | 195 ** The HH, MM, and SS must each be exactly 2 digits. The |
| 169 ** fractional seconds FFFF can be one or more digits. | 196 ** fractional seconds FFFF can be one or more digits. |
| 170 ** | 197 ** |
| 171 ** Return 1 if there is a parsing error and 0 on success. | 198 ** Return 1 if there is a parsing error and 0 on success. |
| 172 */ | 199 */ |
| 173 static int parseHhMmSs(const char *zDate, DateTime *p){ | 200 static int parseHhMmSs(const char *zDate, DateTime *p){ |
| 174 int h, m, s; | 201 int h, m, s; |
| 175 double ms = 0.0; | 202 double ms = 0.0; |
| 176 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ | 203 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){ |
| 177 return 1; | 204 return 1; |
| 178 } | 205 } |
| 179 zDate += 5; | 206 zDate += 5; |
| 180 if( *zDate==':' ){ | 207 if( *zDate==':' ){ |
| 181 zDate++; | 208 zDate++; |
| 182 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ | 209 if( getDigits(zDate, "20e", &s)!=1 ){ |
| 183 return 1; | 210 return 1; |
| 184 } | 211 } |
| 185 zDate += 2; | 212 zDate += 2; |
| 186 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){ | 213 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){ |
| 187 double rScale = 1.0; | 214 double rScale = 1.0; |
| 188 zDate++; | 215 zDate++; |
| 189 while( sqlite3Isdigit(*zDate) ){ | 216 while( sqlite3Isdigit(*zDate) ){ |
| 190 ms = ms*10.0 + *zDate - '0'; | 217 ms = ms*10.0 + *zDate - '0'; |
| 191 rScale *= 10.0; | 218 rScale *= 10.0; |
| 192 zDate++; | 219 zDate++; |
| 193 } | 220 } |
| 194 ms /= rScale; | 221 ms /= rScale; |
| 195 } | 222 } |
| 196 }else{ | 223 }else{ |
| 197 s = 0; | 224 s = 0; |
| 198 } | 225 } |
| 199 p->validJD = 0; | 226 p->validJD = 0; |
| 227 p->rawS = 0; |
| 200 p->validHMS = 1; | 228 p->validHMS = 1; |
| 201 p->h = h; | 229 p->h = h; |
| 202 p->m = m; | 230 p->m = m; |
| 203 p->s = s + ms; | 231 p->s = s + ms; |
| 204 if( parseTimezone(zDate, p) ) return 1; | 232 if( parseTimezone(zDate, p) ) return 1; |
| 205 p->validTZ = (p->tz!=0)?1:0; | 233 p->validTZ = (p->tz!=0)?1:0; |
| 206 return 0; | 234 return 0; |
| 207 } | 235 } |
| 208 | 236 |
| 209 /* | 237 /* |
| 238 ** Put the DateTime object into its error state. |
| 239 */ |
| 240 static void datetimeError(DateTime *p){ |
| 241 memset(p, 0, sizeof(*p)); |
| 242 p->isError = 1; |
| 243 } |
| 244 |
| 245 /* |
| 210 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume | 246 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume |
| 211 ** that the YYYY-MM-DD is according to the Gregorian calendar. | 247 ** that the YYYY-MM-DD is according to the Gregorian calendar. |
| 212 ** | 248 ** |
| 213 ** Reference: Meeus page 61 | 249 ** Reference: Meeus page 61 |
| 214 */ | 250 */ |
| 215 static void computeJD(DateTime *p){ | 251 static void computeJD(DateTime *p){ |
| 216 int Y, M, D, A, B, X1, X2; | 252 int Y, M, D, A, B, X1, X2; |
| 217 | 253 |
| 218 if( p->validJD ) return; | 254 if( p->validJD ) return; |
| 219 if( p->validYMD ){ | 255 if( p->validYMD ){ |
| 220 Y = p->Y; | 256 Y = p->Y; |
| 221 M = p->M; | 257 M = p->M; |
| 222 D = p->D; | 258 D = p->D; |
| 223 }else{ | 259 }else{ |
| 224 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ | 260 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ |
| 225 M = 1; | 261 M = 1; |
| 226 D = 1; | 262 D = 1; |
| 227 } | 263 } |
| 264 if( Y<-4713 || Y>9999 || p->rawS ){ |
| 265 datetimeError(p); |
| 266 return; |
| 267 } |
| 228 if( M<=2 ){ | 268 if( M<=2 ){ |
| 229 Y--; | 269 Y--; |
| 230 M += 12; | 270 M += 12; |
| 231 } | 271 } |
| 232 A = Y/100; | 272 A = Y/100; |
| 233 B = 2 - A + (A/4); | 273 B = 2 - A + (A/4); |
| 234 X1 = 36525*(Y+4716)/100; | 274 X1 = 36525*(Y+4716)/100; |
| 235 X2 = 306001*(M+1)/10000; | 275 X2 = 306001*(M+1)/10000; |
| 236 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); | 276 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); |
| 237 p->validJD = 1; | 277 p->validJD = 1; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 260 */ | 300 */ |
| 261 static int parseYyyyMmDd(const char *zDate, DateTime *p){ | 301 static int parseYyyyMmDd(const char *zDate, DateTime *p){ |
| 262 int Y, M, D, neg; | 302 int Y, M, D, neg; |
| 263 | 303 |
| 264 if( zDate[0]=='-' ){ | 304 if( zDate[0]=='-' ){ |
| 265 zDate++; | 305 zDate++; |
| 266 neg = 1; | 306 neg = 1; |
| 267 }else{ | 307 }else{ |
| 268 neg = 0; | 308 neg = 0; |
| 269 } | 309 } |
| 270 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ | 310 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ |
| 271 return 1; | 311 return 1; |
| 272 } | 312 } |
| 273 zDate += 10; | 313 zDate += 10; |
| 274 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; } | 314 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; } |
| 275 if( parseHhMmSs(zDate, p)==0 ){ | 315 if( parseHhMmSs(zDate, p)==0 ){ |
| 276 /* We got the time */ | 316 /* We got the time */ |
| 277 }else if( *zDate==0 ){ | 317 }else if( *zDate==0 ){ |
| 278 p->validHMS = 0; | 318 p->validHMS = 0; |
| 279 }else{ | 319 }else{ |
| 280 return 1; | 320 return 1; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 299 p->iJD = sqlite3StmtCurrentTime(context); | 339 p->iJD = sqlite3StmtCurrentTime(context); |
| 300 if( p->iJD>0 ){ | 340 if( p->iJD>0 ){ |
| 301 p->validJD = 1; | 341 p->validJD = 1; |
| 302 return 0; | 342 return 0; |
| 303 }else{ | 343 }else{ |
| 304 return 1; | 344 return 1; |
| 305 } | 345 } |
| 306 } | 346 } |
| 307 | 347 |
| 308 /* | 348 /* |
| 349 ** Input "r" is a numeric quantity which might be a julian day number, |
| 350 ** or the number of seconds since 1970. If the value if r is within |
| 351 ** range of a julian day number, install it as such and set validJD. |
| 352 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS. |
| 353 */ |
| 354 static void setRawDateNumber(DateTime *p, double r){ |
| 355 p->s = r; |
| 356 p->rawS = 1; |
| 357 if( r>=0.0 && r<5373484.5 ){ |
| 358 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); |
| 359 p->validJD = 1; |
| 360 } |
| 361 } |
| 362 |
| 363 /* |
| 309 ** Attempt to parse the given string into a julian day number. Return | 364 ** Attempt to parse the given string into a julian day number. Return |
| 310 ** the number of errors. | 365 ** the number of errors. |
| 311 ** | 366 ** |
| 312 ** The following are acceptable forms for the input string: | 367 ** The following are acceptable forms for the input string: |
| 313 ** | 368 ** |
| 314 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM | 369 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| 315 ** DDDD.DD | 370 ** DDDD.DD |
| 316 ** now | 371 ** now |
| 317 ** | 372 ** |
| 318 ** In the first form, the +/-HH:MM is always optional. The fractional | 373 ** In the first form, the +/-HH:MM is always optional. The fractional |
| 319 ** seconds extension (the ".FFF") is optional. The seconds portion | 374 ** seconds extension (the ".FFF") is optional. The seconds portion |
| 320 ** (":SS.FFF") is option. The year and date can be omitted as long | 375 ** (":SS.FFF") is option. The year and date can be omitted as long |
| 321 ** as there is a time string. The time string can be omitted as long | 376 ** as there is a time string. The time string can be omitted as long |
| 322 ** as there is a year and date. | 377 ** as there is a year and date. |
| 323 */ | 378 */ |
| 324 static int parseDateOrTime( | 379 static int parseDateOrTime( |
| 325 sqlite3_context *context, | 380 sqlite3_context *context, |
| 326 const char *zDate, | 381 const char *zDate, |
| 327 DateTime *p | 382 DateTime *p |
| 328 ){ | 383 ){ |
| 329 double r; | 384 double r; |
| 330 if( parseYyyyMmDd(zDate,p)==0 ){ | 385 if( parseYyyyMmDd(zDate,p)==0 ){ |
| 331 return 0; | 386 return 0; |
| 332 }else if( parseHhMmSs(zDate, p)==0 ){ | 387 }else if( parseHhMmSs(zDate, p)==0 ){ |
| 333 return 0; | 388 return 0; |
| 334 }else if( sqlite3StrICmp(zDate,"now")==0){ | 389 }else if( sqlite3StrICmp(zDate,"now")==0){ |
| 335 return setDateTimeToCurrent(context, p); | 390 return setDateTimeToCurrent(context, p); |
| 336 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ | 391 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ |
| 337 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); | 392 setRawDateNumber(p, r); |
| 338 p->validJD = 1; | |
| 339 return 0; | 393 return 0; |
| 340 } | 394 } |
| 341 return 1; | 395 return 1; |
| 342 } | 396 } |
| 343 | 397 |
| 398 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999. |
| 399 ** Multiplying this by 86400000 gives 464269060799999 as the maximum value |
| 400 ** for DateTime.iJD. |
| 401 ** |
| 402 ** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with |
| 403 ** such a large integer literal, so we have to encode it. |
| 404 */ |
| 405 #define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff) |
| 406 |
| 407 /* |
| 408 ** Return TRUE if the given julian day number is within range. |
| 409 ** |
| 410 ** The input is the JulianDay times 86400000. |
| 411 */ |
| 412 static int validJulianDay(sqlite3_int64 iJD){ |
| 413 return iJD>=0 && iJD<=INT_464269060799999; |
| 414 } |
| 415 |
| 344 /* | 416 /* |
| 345 ** Compute the Year, Month, and Day from the julian day number. | 417 ** Compute the Year, Month, and Day from the julian day number. |
| 346 */ | 418 */ |
| 347 static void computeYMD(DateTime *p){ | 419 static void computeYMD(DateTime *p){ |
| 348 int Z, A, B, C, D, E, X1; | 420 int Z, A, B, C, D, E, X1; |
| 349 if( p->validYMD ) return; | 421 if( p->validYMD ) return; |
| 350 if( !p->validJD ){ | 422 if( !p->validJD ){ |
| 351 p->Y = 2000; | 423 p->Y = 2000; |
| 352 p->M = 1; | 424 p->M = 1; |
| 353 p->D = 1; | 425 p->D = 1; |
| 354 }else{ | 426 }else{ |
| 427 assert( validJulianDay(p->iJD) ); |
| 355 Z = (int)((p->iJD + 43200000)/86400000); | 428 Z = (int)((p->iJD + 43200000)/86400000); |
| 356 A = (int)((Z - 1867216.25)/36524.25); | 429 A = (int)((Z - 1867216.25)/36524.25); |
| 357 A = Z + 1 + A - (A/4); | 430 A = Z + 1 + A - (A/4); |
| 358 B = A + 1524; | 431 B = A + 1524; |
| 359 C = (int)((B - 122.1)/365.25); | 432 C = (int)((B - 122.1)/365.25); |
| 360 D = (36525*(C&32767))/100; | 433 D = (36525*(C&32767))/100; |
| 361 E = (int)((B-D)/30.6001); | 434 E = (int)((B-D)/30.6001); |
| 362 X1 = (int)(30.6001*E); | 435 X1 = (int)(30.6001*E); |
| 363 p->D = B - D - X1; | 436 p->D = B - D - X1; |
| 364 p->M = E<14 ? E-1 : E-13; | 437 p->M = E<14 ? E-1 : E-13; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 375 if( p->validHMS ) return; | 448 if( p->validHMS ) return; |
| 376 computeJD(p); | 449 computeJD(p); |
| 377 s = (int)((p->iJD + 43200000) % 86400000); | 450 s = (int)((p->iJD + 43200000) % 86400000); |
| 378 p->s = s/1000.0; | 451 p->s = s/1000.0; |
| 379 s = (int)p->s; | 452 s = (int)p->s; |
| 380 p->s -= s; | 453 p->s -= s; |
| 381 p->h = s/3600; | 454 p->h = s/3600; |
| 382 s -= p->h*3600; | 455 s -= p->h*3600; |
| 383 p->m = s/60; | 456 p->m = s/60; |
| 384 p->s += s - p->m*60; | 457 p->s += s - p->m*60; |
| 458 p->rawS = 0; |
| 385 p->validHMS = 1; | 459 p->validHMS = 1; |
| 386 } | 460 } |
| 387 | 461 |
| 388 /* | 462 /* |
| 389 ** Compute both YMD and HMS | 463 ** Compute both YMD and HMS |
| 390 */ | 464 */ |
| 391 static void computeYMD_HMS(DateTime *p){ | 465 static void computeYMD_HMS(DateTime *p){ |
| 392 computeYMD(p); | 466 computeYMD(p); |
| 393 computeHMS(p); | 467 computeHMS(p); |
| 394 } | 468 } |
| 395 | 469 |
| 396 /* | 470 /* |
| 397 ** Clear the YMD and HMS and the TZ | 471 ** Clear the YMD and HMS and the TZ |
| 398 */ | 472 */ |
| 399 static void clearYMD_HMS_TZ(DateTime *p){ | 473 static void clearYMD_HMS_TZ(DateTime *p){ |
| 400 p->validYMD = 0; | 474 p->validYMD = 0; |
| 401 p->validHMS = 0; | 475 p->validHMS = 0; |
| 402 p->validTZ = 0; | 476 p->validTZ = 0; |
| 403 } | 477 } |
| 404 | 478 |
| 479 #ifndef SQLITE_OMIT_LOCALTIME |
| 405 /* | 480 /* |
| 406 ** On recent Windows platforms, the localtime_s() function is available | 481 ** On recent Windows platforms, the localtime_s() function is available |
| 407 ** as part of the "Secure CRT". It is essentially equivalent to | 482 ** as part of the "Secure CRT". It is essentially equivalent to |
| 408 ** localtime_r() available under most POSIX platforms, except that the | 483 ** localtime_r() available under most POSIX platforms, except that the |
| 409 ** order of the parameters is reversed. | 484 ** order of the parameters is reversed. |
| 410 ** | 485 ** |
| 411 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. | 486 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. |
| 412 ** | 487 ** |
| 413 ** If the user has not indicated to use localtime_r() or localtime_s() | 488 ** If the user has not indicated to use localtime_r() or localtime_s() |
| 414 ** already, check for an MSVC build environment that provides | 489 ** already, check for an MSVC build environment that provides |
| 415 ** localtime_s(). | 490 ** localtime_s(). |
| 416 */ | 491 */ |
| 417 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \ | 492 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \ |
| 418 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) | 493 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) |
| 419 #undef HAVE_LOCALTIME_S | 494 #undef HAVE_LOCALTIME_S |
| 420 #define HAVE_LOCALTIME_S 1 | 495 #define HAVE_LOCALTIME_S 1 |
| 421 #endif | 496 #endif |
| 422 | 497 |
| 423 #ifndef SQLITE_OMIT_LOCALTIME | |
| 424 /* | 498 /* |
| 425 ** The following routine implements the rough equivalent of localtime_r() | 499 ** The following routine implements the rough equivalent of localtime_r() |
| 426 ** using whatever operating-system specific localtime facility that | 500 ** using whatever operating-system specific localtime facility that |
| 427 ** is available. This routine returns 0 on success and | 501 ** is available. This routine returns 0 on success and |
| 428 ** non-zero on any kind of error. | 502 ** non-zero on any kind of error. |
| 429 ** | 503 ** |
| 430 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this | 504 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this |
| 431 ** routine will always fail. | 505 ** routine will always fail. |
| 432 ** | 506 ** |
| 433 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C | 507 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C |
| 434 ** library function localtime_r() is used to assist in the calculation of | 508 ** library function localtime_r() is used to assist in the calculation of |
| 435 ** local time. | 509 ** local time. |
| 436 */ | 510 */ |
| 437 static int osLocaltime(time_t *t, struct tm *pTm){ | 511 static int osLocaltime(time_t *t, struct tm *pTm){ |
| 438 int rc; | 512 int rc; |
| 439 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S | 513 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S |
| 440 struct tm *pX; | 514 struct tm *pX; |
| 441 #if SQLITE_THREADSAFE>0 | 515 #if SQLITE_THREADSAFE>0 |
| 442 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 516 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 443 #endif | 517 #endif |
| 444 sqlite3_mutex_enter(mutex); | 518 sqlite3_mutex_enter(mutex); |
| 445 pX = localtime(t); | 519 pX = localtime(t); |
| 446 #ifndef SQLITE_OMIT_BUILTIN_TEST | 520 #ifndef SQLITE_UNTESTABLE |
| 447 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; | 521 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; |
| 448 #endif | 522 #endif |
| 449 if( pX ) *pTm = *pX; | 523 if( pX ) *pTm = *pX; |
| 450 sqlite3_mutex_leave(mutex); | 524 sqlite3_mutex_leave(mutex); |
| 451 rc = pX==0; | 525 rc = pX==0; |
| 452 #else | 526 #else |
| 453 #ifndef SQLITE_OMIT_BUILTIN_TEST | 527 #ifndef SQLITE_UNTESTABLE |
| 454 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; | 528 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; |
| 455 #endif | 529 #endif |
| 456 #if HAVE_LOCALTIME_R | 530 #if HAVE_LOCALTIME_R |
| 457 rc = localtime_r(t, pTm)==0; | 531 rc = localtime_r(t, pTm)==0; |
| 458 #else | 532 #else |
| 459 rc = localtime_s(pTm, t); | 533 rc = localtime_s(pTm, t); |
| 460 #endif /* HAVE_LOCALTIME_R */ | 534 #endif /* HAVE_LOCALTIME_R */ |
| 461 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ | 535 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ |
| 462 return rc; | 536 return rc; |
| 463 } | 537 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 588 } |
| 515 y.Y = sLocal.tm_year + 1900; | 589 y.Y = sLocal.tm_year + 1900; |
| 516 y.M = sLocal.tm_mon + 1; | 590 y.M = sLocal.tm_mon + 1; |
| 517 y.D = sLocal.tm_mday; | 591 y.D = sLocal.tm_mday; |
| 518 y.h = sLocal.tm_hour; | 592 y.h = sLocal.tm_hour; |
| 519 y.m = sLocal.tm_min; | 593 y.m = sLocal.tm_min; |
| 520 y.s = sLocal.tm_sec; | 594 y.s = sLocal.tm_sec; |
| 521 y.validYMD = 1; | 595 y.validYMD = 1; |
| 522 y.validHMS = 1; | 596 y.validHMS = 1; |
| 523 y.validJD = 0; | 597 y.validJD = 0; |
| 598 y.rawS = 0; |
| 524 y.validTZ = 0; | 599 y.validTZ = 0; |
| 600 y.isError = 0; |
| 525 computeJD(&y); | 601 computeJD(&y); |
| 526 *pRc = SQLITE_OK; | 602 *pRc = SQLITE_OK; |
| 527 return y.iJD - x.iJD; | 603 return y.iJD - x.iJD; |
| 528 } | 604 } |
| 529 #endif /* SQLITE_OMIT_LOCALTIME */ | 605 #endif /* SQLITE_OMIT_LOCALTIME */ |
| 530 | 606 |
| 531 /* | 607 /* |
| 608 ** The following table defines various date transformations of the form |
| 609 ** |
| 610 ** 'NNN days' |
| 611 ** |
| 612 ** Where NNN is an arbitrary floating-point number and "days" can be one |
| 613 ** of several units of time. |
| 614 */ |
| 615 static const struct { |
| 616 u8 eType; /* Transformation type code */ |
| 617 u8 nName; /* Length of th name */ |
| 618 char *zName; /* Name of the transformation */ |
| 619 double rLimit; /* Maximum NNN value for this transform */ |
| 620 double rXform; /* Constant used for this transform */ |
| 621 } aXformType[] = { |
| 622 { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) }, |
| 623 { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) }, |
| 624 { 0, 4, "hour", 128963628.0, 86400000.0/24.0 }, |
| 625 { 0, 3, "day", 5373485.0, 86400000.0 }, |
| 626 { 1, 5, "month", 176546.0, 30.0*86400000.0 }, |
| 627 { 2, 4, "year", 14713.0, 365.0*86400000.0 }, |
| 628 }; |
| 629 |
| 630 /* |
| 532 ** Process a modifier to a date-time stamp. The modifiers are | 631 ** Process a modifier to a date-time stamp. The modifiers are |
| 533 ** as follows: | 632 ** as follows: |
| 534 ** | 633 ** |
| 535 ** NNN days | 634 ** NNN days |
| 536 ** NNN hours | 635 ** NNN hours |
| 537 ** NNN minutes | 636 ** NNN minutes |
| 538 ** NNN.NNNN seconds | 637 ** NNN.NNNN seconds |
| 539 ** NNN months | 638 ** NNN months |
| 540 ** NNN years | 639 ** NNN years |
| 541 ** start of month | 640 ** start of month |
| 542 ** start of year | 641 ** start of year |
| 543 ** start of week | 642 ** start of week |
| 544 ** start of day | 643 ** start of day |
| 545 ** weekday N | 644 ** weekday N |
| 546 ** unixepoch | 645 ** unixepoch |
| 547 ** localtime | 646 ** localtime |
| 548 ** utc | 647 ** utc |
| 549 ** | 648 ** |
| 550 ** Return 0 on success and 1 if there is any kind of error. If the error | 649 ** Return 0 on success and 1 if there is any kind of error. If the error |
| 551 ** is in a system call (i.e. localtime()), then an error message is written | 650 ** is in a system call (i.e. localtime()), then an error message is written |
| 552 ** to context pCtx. If the error is an unrecognized modifier, no error is | 651 ** to context pCtx. If the error is an unrecognized modifier, no error is |
| 553 ** written to pCtx. | 652 ** written to pCtx. |
| 554 */ | 653 */ |
| 555 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ | 654 static int parseModifier( |
| 655 sqlite3_context *pCtx, /* Function context */ |
| 656 const char *z, /* The text of the modifier */ |
| 657 int n, /* Length of zMod in bytes */ |
| 658 DateTime *p /* The date/time value to be modified */ |
| 659 ){ |
| 556 int rc = 1; | 660 int rc = 1; |
| 557 int n; | |
| 558 double r; | 661 double r; |
| 559 char *z, zBuf[30]; | 662 switch(sqlite3UpperToLower[(u8)z[0]] ){ |
| 560 z = zBuf; | |
| 561 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){ | |
| 562 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]]; | |
| 563 } | |
| 564 z[n] = 0; | |
| 565 switch( z[0] ){ | |
| 566 #ifndef SQLITE_OMIT_LOCALTIME | 663 #ifndef SQLITE_OMIT_LOCALTIME |
| 567 case 'l': { | 664 case 'l': { |
| 568 /* localtime | 665 /* localtime |
| 569 ** | 666 ** |
| 570 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to | 667 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to |
| 571 ** show local time. | 668 ** show local time. |
| 572 */ | 669 */ |
| 573 if( strcmp(z, "localtime")==0 ){ | 670 if( sqlite3_stricmp(z, "localtime")==0 ){ |
| 574 computeJD(p); | 671 computeJD(p); |
| 575 p->iJD += localtimeOffset(p, pCtx, &rc); | 672 p->iJD += localtimeOffset(p, pCtx, &rc); |
| 576 clearYMD_HMS_TZ(p); | 673 clearYMD_HMS_TZ(p); |
| 577 } | 674 } |
| 578 break; | 675 break; |
| 579 } | 676 } |
| 580 #endif | 677 #endif |
| 581 case 'u': { | 678 case 'u': { |
| 582 /* | 679 /* |
| 583 ** unixepoch | 680 ** unixepoch |
| 584 ** | 681 ** |
| 585 ** Treat the current value of p->iJD as the number of | 682 ** Treat the current value of p->s as the number of |
| 586 ** seconds since 1970. Convert to a real julian day number. | 683 ** seconds since 1970. Convert to a real julian day number. |
| 587 */ | 684 */ |
| 588 if( strcmp(z, "unixepoch")==0 && p->validJD ){ | 685 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ |
| 589 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; | 686 r = p->s*1000.0 + 210866760000000.0; |
| 590 clearYMD_HMS_TZ(p); | 687 if( r>=0.0 && r<464269060800000.0 ){ |
| 591 rc = 0; | 688 clearYMD_HMS_TZ(p); |
| 689 p->iJD = (sqlite3_int64)r; |
| 690 p->validJD = 1; |
| 691 p->rawS = 0; |
| 692 rc = 0; |
| 693 } |
| 592 } | 694 } |
| 593 #ifndef SQLITE_OMIT_LOCALTIME | 695 #ifndef SQLITE_OMIT_LOCALTIME |
| 594 else if( strcmp(z, "utc")==0 ){ | 696 else if( sqlite3_stricmp(z, "utc")==0 ){ |
| 595 if( p->tzSet==0 ){ | 697 if( p->tzSet==0 ){ |
| 596 sqlite3_int64 c1; | 698 sqlite3_int64 c1; |
| 597 computeJD(p); | 699 computeJD(p); |
| 598 c1 = localtimeOffset(p, pCtx, &rc); | 700 c1 = localtimeOffset(p, pCtx, &rc); |
| 599 if( rc==SQLITE_OK ){ | 701 if( rc==SQLITE_OK ){ |
| 600 p->iJD -= c1; | 702 p->iJD -= c1; |
| 601 clearYMD_HMS_TZ(p); | 703 clearYMD_HMS_TZ(p); |
| 602 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); | 704 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); |
| 603 } | 705 } |
| 604 p->tzSet = 1; | 706 p->tzSet = 1; |
| 605 }else{ | 707 }else{ |
| 606 rc = SQLITE_OK; | 708 rc = SQLITE_OK; |
| 607 } | 709 } |
| 608 } | 710 } |
| 609 #endif | 711 #endif |
| 610 break; | 712 break; |
| 611 } | 713 } |
| 612 case 'w': { | 714 case 'w': { |
| 613 /* | 715 /* |
| 614 ** weekday N | 716 ** weekday N |
| 615 ** | 717 ** |
| 616 ** Move the date to the same time on the next occurrence of | 718 ** Move the date to the same time on the next occurrence of |
| 617 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the | 719 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
| 618 ** date is already on the appropriate weekday, this is a no-op. | 720 ** date is already on the appropriate weekday, this is a no-op. |
| 619 */ | 721 */ |
| 620 if( strncmp(z, "weekday ", 8)==0 | 722 if( sqlite3_strnicmp(z, "weekday ", 8)==0 |
| 621 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8) | 723 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8) |
| 622 && (n=(int)r)==r && n>=0 && r<7 ){ | 724 && (n=(int)r)==r && n>=0 && r<7 ){ |
| 623 sqlite3_int64 Z; | 725 sqlite3_int64 Z; |
| 624 computeYMD_HMS(p); | 726 computeYMD_HMS(p); |
| 625 p->validTZ = 0; | 727 p->validTZ = 0; |
| 626 p->validJD = 0; | 728 p->validJD = 0; |
| 627 computeJD(p); | 729 computeJD(p); |
| 628 Z = ((p->iJD + 129600000)/86400000) % 7; | 730 Z = ((p->iJD + 129600000)/86400000) % 7; |
| 629 if( Z>n ) Z -= 7; | 731 if( Z>n ) Z -= 7; |
| 630 p->iJD += (n - Z)*86400000; | 732 p->iJD += (n - Z)*86400000; |
| 631 clearYMD_HMS_TZ(p); | 733 clearYMD_HMS_TZ(p); |
| 632 rc = 0; | 734 rc = 0; |
| 633 } | 735 } |
| 634 break; | 736 break; |
| 635 } | 737 } |
| 636 case 's': { | 738 case 's': { |
| 637 /* | 739 /* |
| 638 ** start of TTTTT | 740 ** start of TTTTT |
| 639 ** | 741 ** |
| 640 ** Move the date backwards to the beginning of the current day, | 742 ** Move the date backwards to the beginning of the current day, |
| 641 ** or month or year. | 743 ** or month or year. |
| 642 */ | 744 */ |
| 643 if( strncmp(z, "start of ", 9)!=0 ) break; | 745 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break; |
| 644 z += 9; | 746 z += 9; |
| 645 computeYMD(p); | 747 computeYMD(p); |
| 646 p->validHMS = 1; | 748 p->validHMS = 1; |
| 647 p->h = p->m = 0; | 749 p->h = p->m = 0; |
| 648 p->s = 0.0; | 750 p->s = 0.0; |
| 649 p->validTZ = 0; | 751 p->validTZ = 0; |
| 650 p->validJD = 0; | 752 p->validJD = 0; |
| 651 if( strcmp(z,"month")==0 ){ | 753 if( sqlite3_stricmp(z,"month")==0 ){ |
| 652 p->D = 1; | 754 p->D = 1; |
| 653 rc = 0; | 755 rc = 0; |
| 654 }else if( strcmp(z,"year")==0 ){ | 756 }else if( sqlite3_stricmp(z,"year")==0 ){ |
| 655 computeYMD(p); | 757 computeYMD(p); |
| 656 p->M = 1; | 758 p->M = 1; |
| 657 p->D = 1; | 759 p->D = 1; |
| 658 rc = 0; | 760 rc = 0; |
| 659 }else if( strcmp(z,"day")==0 ){ | 761 }else if( sqlite3_stricmp(z,"day")==0 ){ |
| 660 rc = 0; | 762 rc = 0; |
| 661 } | 763 } |
| 662 break; | 764 break; |
| 663 } | 765 } |
| 664 case '+': | 766 case '+': |
| 665 case '-': | 767 case '-': |
| 666 case '0': | 768 case '0': |
| 667 case '1': | 769 case '1': |
| 668 case '2': | 770 case '2': |
| 669 case '3': | 771 case '3': |
| 670 case '4': | 772 case '4': |
| 671 case '5': | 773 case '5': |
| 672 case '6': | 774 case '6': |
| 673 case '7': | 775 case '7': |
| 674 case '8': | 776 case '8': |
| 675 case '9': { | 777 case '9': { |
| 676 double rRounder; | 778 double rRounder; |
| 779 int i; |
| 677 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){} | 780 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){} |
| 678 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){ | 781 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){ |
| 679 rc = 1; | 782 rc = 1; |
| 680 break; | 783 break; |
| 681 } | 784 } |
| 682 if( z[n]==':' ){ | 785 if( z[n]==':' ){ |
| 683 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the | 786 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the |
| 684 ** specified number of hours, minutes, seconds, and fractional seconds | 787 ** specified number of hours, minutes, seconds, and fractional seconds |
| 685 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be | 788 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be |
| 686 ** omitted. | 789 ** omitted. |
| 687 */ | 790 */ |
| 688 const char *z2 = z; | 791 const char *z2 = z; |
| 689 DateTime tx; | 792 DateTime tx; |
| 690 sqlite3_int64 day; | 793 sqlite3_int64 day; |
| 691 if( !sqlite3Isdigit(*z2) ) z2++; | 794 if( !sqlite3Isdigit(*z2) ) z2++; |
| 692 memset(&tx, 0, sizeof(tx)); | 795 memset(&tx, 0, sizeof(tx)); |
| 693 if( parseHhMmSs(z2, &tx) ) break; | 796 if( parseHhMmSs(z2, &tx) ) break; |
| 694 computeJD(&tx); | 797 computeJD(&tx); |
| 695 tx.iJD -= 43200000; | 798 tx.iJD -= 43200000; |
| 696 day = tx.iJD/86400000; | 799 day = tx.iJD/86400000; |
| 697 tx.iJD -= day*86400000; | 800 tx.iJD -= day*86400000; |
| 698 if( z[0]=='-' ) tx.iJD = -tx.iJD; | 801 if( z[0]=='-' ) tx.iJD = -tx.iJD; |
| 699 computeJD(p); | 802 computeJD(p); |
| 700 clearYMD_HMS_TZ(p); | 803 clearYMD_HMS_TZ(p); |
| 701 p->iJD += tx.iJD; | 804 p->iJD += tx.iJD; |
| 702 rc = 0; | 805 rc = 0; |
| 703 break; | 806 break; |
| 704 } | 807 } |
| 808 |
| 809 /* If control reaches this point, it means the transformation is |
| 810 ** one of the forms like "+NNN days". */ |
| 705 z += n; | 811 z += n; |
| 706 while( sqlite3Isspace(*z) ) z++; | 812 while( sqlite3Isspace(*z) ) z++; |
| 707 n = sqlite3Strlen30(z); | 813 n = sqlite3Strlen30(z); |
| 708 if( n>10 || n<3 ) break; | 814 if( n>10 || n<3 ) break; |
| 709 if( z[n-1]=='s' ){ z[n-1] = 0; n--; } | 815 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; |
| 710 computeJD(p); | 816 computeJD(p); |
| 711 rc = 0; | 817 rc = 1; |
| 712 rRounder = r<0 ? -0.5 : +0.5; | 818 rRounder = r<0 ? -0.5 : +0.5; |
| 713 if( n==3 && strcmp(z,"day")==0 ){ | 819 for(i=0; i<ArraySize(aXformType); i++){ |
| 714 p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder); | 820 if( aXformType[i].nName==n |
| 715 }else if( n==4 && strcmp(z,"hour")==0 ){ | 821 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0 |
| 716 p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder); | 822 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit |
| 717 }else if( n==6 && strcmp(z,"minute")==0 ){ | 823 ){ |
| 718 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder); | 824 switch( aXformType[i].eType ){ |
| 719 }else if( n==6 && strcmp(z,"second")==0 ){ | 825 case 1: { /* Special processing to add months */ |
| 720 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder); | 826 int x; |
| 721 }else if( n==5 && strcmp(z,"month")==0 ){ | 827 computeYMD_HMS(p); |
| 722 int x, y; | 828 p->M += (int)r; |
| 723 computeYMD_HMS(p); | 829 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; |
| 724 p->M += (int)r; | 830 p->Y += x; |
| 725 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; | 831 p->M -= x*12; |
| 726 p->Y += x; | 832 p->validJD = 0; |
| 727 p->M -= x*12; | 833 r -= (int)r; |
| 728 p->validJD = 0; | 834 break; |
| 729 computeJD(p); | 835 } |
| 730 y = (int)r; | 836 case 2: { /* Special processing to add years */ |
| 731 if( y!=r ){ | 837 int y = (int)r; |
| 732 p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder); | 838 computeYMD_HMS(p); |
| 839 p->Y += y; |
| 840 p->validJD = 0; |
| 841 r -= (int)r; |
| 842 break; |
| 843 } |
| 844 } |
| 845 computeJD(p); |
| 846 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); |
| 847 rc = 0; |
| 848 break; |
| 733 } | 849 } |
| 734 }else if( n==4 && strcmp(z,"year")==0 ){ | |
| 735 int y = (int)r; | |
| 736 computeYMD_HMS(p); | |
| 737 p->Y += y; | |
| 738 p->validJD = 0; | |
| 739 computeJD(p); | |
| 740 if( y!=r ){ | |
| 741 p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder); | |
| 742 } | |
| 743 }else{ | |
| 744 rc = 1; | |
| 745 } | 850 } |
| 746 clearYMD_HMS_TZ(p); | 851 clearYMD_HMS_TZ(p); |
| 747 break; | 852 break; |
| 748 } | 853 } |
| 749 default: { | 854 default: { |
| 750 break; | 855 break; |
| 751 } | 856 } |
| 752 } | 857 } |
| 753 return rc; | 858 return rc; |
| 754 } | 859 } |
| 755 | 860 |
| 756 /* | 861 /* |
| 757 ** Process time function arguments. argv[0] is a date-time stamp. | 862 ** Process time function arguments. argv[0] is a date-time stamp. |
| 758 ** argv[1] and following are modifiers. Parse them all and write | 863 ** argv[1] and following are modifiers. Parse them all and write |
| 759 ** the resulting time into the DateTime structure p. Return 0 | 864 ** the resulting time into the DateTime structure p. Return 0 |
| 760 ** on success and 1 if there are any errors. | 865 ** on success and 1 if there are any errors. |
| 761 ** | 866 ** |
| 762 ** If there are zero parameters (if even argv[0] is undefined) | 867 ** If there are zero parameters (if even argv[0] is undefined) |
| 763 ** then assume a default value of "now" for argv[0]. | 868 ** then assume a default value of "now" for argv[0]. |
| 764 */ | 869 */ |
| 765 static int isDate( | 870 static int isDate( |
| 766 sqlite3_context *context, | 871 sqlite3_context *context, |
| 767 int argc, | 872 int argc, |
| 768 sqlite3_value **argv, | 873 sqlite3_value **argv, |
| 769 DateTime *p | 874 DateTime *p |
| 770 ){ | 875 ){ |
| 771 int i; | 876 int i, n; |
| 772 const unsigned char *z; | 877 const unsigned char *z; |
| 773 int eType; | 878 int eType; |
| 774 memset(p, 0, sizeof(*p)); | 879 memset(p, 0, sizeof(*p)); |
| 775 if( argc==0 ){ | 880 if( argc==0 ){ |
| 776 return setDateTimeToCurrent(context, p); | 881 return setDateTimeToCurrent(context, p); |
| 777 } | 882 } |
| 778 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT | 883 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT |
| 779 || eType==SQLITE_INTEGER ){ | 884 || eType==SQLITE_INTEGER ){ |
| 780 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); | 885 setRawDateNumber(p, sqlite3_value_double(argv[0])); |
| 781 p->validJD = 1; | |
| 782 }else{ | 886 }else{ |
| 783 z = sqlite3_value_text(argv[0]); | 887 z = sqlite3_value_text(argv[0]); |
| 784 if( !z || parseDateOrTime(context, (char*)z, p) ){ | 888 if( !z || parseDateOrTime(context, (char*)z, p) ){ |
| 785 return 1; | 889 return 1; |
| 786 } | 890 } |
| 787 } | 891 } |
| 788 for(i=1; i<argc; i++){ | 892 for(i=1; i<argc; i++){ |
| 789 z = sqlite3_value_text(argv[i]); | 893 z = sqlite3_value_text(argv[i]); |
| 790 if( z==0 || parseModifier(context, (char*)z, p) ) return 1; | 894 n = sqlite3_value_bytes(argv[i]); |
| 895 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1; |
| 791 } | 896 } |
| 897 computeJD(p); |
| 898 if( p->isError || !validJulianDay(p->iJD) ) return 1; |
| 792 return 0; | 899 return 0; |
| 793 } | 900 } |
| 794 | 901 |
| 795 | 902 |
| 796 /* | 903 /* |
| 797 ** The following routines implement the various date and time functions | 904 ** The following routines implement the various date and time functions |
| 798 ** of SQLite. | 905 ** of SQLite. |
| 799 */ | 906 */ |
| 800 | 907 |
| 801 /* | 908 /* |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 944 testcase( n==sizeof(zBuf)-1 ); | 1051 testcase( n==sizeof(zBuf)-1 ); |
| 945 testcase( n==sizeof(zBuf) ); | 1052 testcase( n==sizeof(zBuf) ); |
| 946 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); | 1053 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); |
| 947 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); | 1054 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 948 if( n<sizeof(zBuf) ){ | 1055 if( n<sizeof(zBuf) ){ |
| 949 z = zBuf; | 1056 z = zBuf; |
| 950 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ | 1057 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 951 sqlite3_result_error_toobig(context); | 1058 sqlite3_result_error_toobig(context); |
| 952 return; | 1059 return; |
| 953 }else{ | 1060 }else{ |
| 954 z = sqlite3DbMallocRaw(db, (int)n); | 1061 z = sqlite3DbMallocRawNN(db, (int)n); |
| 955 if( z==0 ){ | 1062 if( z==0 ){ |
| 956 sqlite3_result_error_nomem(context); | 1063 sqlite3_result_error_nomem(context); |
| 957 return; | 1064 return; |
| 958 } | 1065 } |
| 959 } | 1066 } |
| 960 computeJD(&x); | 1067 computeJD(&x); |
| 961 computeYMD_HMS(&x); | 1068 computeYMD_HMS(&x); |
| 962 for(i=j=0; zFmt[i]; i++){ | 1069 for(i=j=0; zFmt[i]; i++){ |
| 963 if( zFmt[i]!='%' ){ | 1070 if( zFmt[i]!='%' ){ |
| 964 z[j++] = zFmt[i]; | 1071 z[j++] = zFmt[i]; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 ** and strftime(). The format string to pass to strftime() is supplied | 1187 ** and strftime(). The format string to pass to strftime() is supplied |
| 1081 ** as the user-data for the function. | 1188 ** as the user-data for the function. |
| 1082 */ | 1189 */ |
| 1083 static void currentTimeFunc( | 1190 static void currentTimeFunc( |
| 1084 sqlite3_context *context, | 1191 sqlite3_context *context, |
| 1085 int argc, | 1192 int argc, |
| 1086 sqlite3_value **argv | 1193 sqlite3_value **argv |
| 1087 ){ | 1194 ){ |
| 1088 time_t t; | 1195 time_t t; |
| 1089 char *zFormat = (char *)sqlite3_user_data(context); | 1196 char *zFormat = (char *)sqlite3_user_data(context); |
| 1090 sqlite3 *db; | |
| 1091 sqlite3_int64 iT; | 1197 sqlite3_int64 iT; |
| 1092 struct tm *pTm; | 1198 struct tm *pTm; |
| 1093 struct tm sNow; | 1199 struct tm sNow; |
| 1094 char zBuf[20]; | 1200 char zBuf[20]; |
| 1095 | 1201 |
| 1096 UNUSED_PARAMETER(argc); | 1202 UNUSED_PARAMETER(argc); |
| 1097 UNUSED_PARAMETER(argv); | 1203 UNUSED_PARAMETER(argv); |
| 1098 | 1204 |
| 1099 iT = sqlite3StmtCurrentTime(context); | 1205 iT = sqlite3StmtCurrentTime(context); |
| 1100 if( iT<=0 ) return; | 1206 if( iT<=0 ) return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1113 } | 1219 } |
| 1114 } | 1220 } |
| 1115 #endif | 1221 #endif |
| 1116 | 1222 |
| 1117 /* | 1223 /* |
| 1118 ** This function registered all of the above C functions as SQL | 1224 ** This function registered all of the above C functions as SQL |
| 1119 ** functions. This should be the only routine in this file with | 1225 ** functions. This should be the only routine in this file with |
| 1120 ** external linkage. | 1226 ** external linkage. |
| 1121 */ | 1227 */ |
| 1122 void sqlite3RegisterDateTimeFunctions(void){ | 1228 void sqlite3RegisterDateTimeFunctions(void){ |
| 1123 static SQLITE_WSD FuncDef aDateTimeFuncs[] = { | 1229 static FuncDef aDateTimeFuncs[] = { |
| 1124 #ifndef SQLITE_OMIT_DATETIME_FUNCS | 1230 #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 1125 DFUNCTION(julianday, -1, 0, 0, juliandayFunc ), | 1231 DFUNCTION(julianday, -1, 0, 0, juliandayFunc ), |
| 1126 DFUNCTION(date, -1, 0, 0, dateFunc ), | 1232 DFUNCTION(date, -1, 0, 0, dateFunc ), |
| 1127 DFUNCTION(time, -1, 0, 0, timeFunc ), | 1233 DFUNCTION(time, -1, 0, 0, timeFunc ), |
| 1128 DFUNCTION(datetime, -1, 0, 0, datetimeFunc ), | 1234 DFUNCTION(datetime, -1, 0, 0, datetimeFunc ), |
| 1129 DFUNCTION(strftime, -1, 0, 0, strftimeFunc ), | 1235 DFUNCTION(strftime, -1, 0, 0, strftimeFunc ), |
| 1130 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), | 1236 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), |
| 1131 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), | 1237 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), |
| 1132 DFUNCTION(current_date, 0, 0, 0, cdateFunc ), | 1238 DFUNCTION(current_date, 0, 0, 0, cdateFunc ), |
| 1133 #else | 1239 #else |
| 1134 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), | 1240 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), |
| 1135 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), | 1241 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), |
| 1136 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), | 1242 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), |
| 1137 #endif | 1243 #endif |
| 1138 }; | 1244 }; |
| 1139 int i; | 1245 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)); |
| 1140 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); | |
| 1141 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); | |
| 1142 | |
| 1143 for(i=0; i<ArraySize(aDateTimeFuncs); i++){ | |
| 1144 sqlite3FuncDefInsert(pHash, &aFunc[i]); | |
| 1145 } | |
| 1146 } | 1246 } |
| OLD | NEW |