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 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains the C functions that implement date and time | 12 ** This file contains the C functions that implement date and time |
13 ** functions for SQLite. | 13 ** functions for SQLite. |
14 ** | 14 ** |
15 ** There is only one exported symbol in this file - the function | 15 ** There is only one exported symbol in this file - the function |
16 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. | 16 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
17 ** All other code has file scope. | 17 ** All other code has file scope. |
18 ** | 18 ** |
19 ** SQLite processes all times and dates as Julian Day numbers. The | 19 ** SQLite processes all times and dates as Julian Day numbers. The |
20 ** dates and times are stored as the number of days since noon | 20 ** dates and times are stored as the number of days since noon |
21 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian | 21 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
22 ** calendar system. | 22 ** calendar system. |
23 ** | 23 ** |
24 ** 1970-01-01 00:00:00 is JD 2440587.5 | 24 ** 1970-01-01 00:00:00 is JD 2440587.5 |
25 ** 2000-01-01 00:00:00 is JD 2451544.5 | 25 ** 2000-01-01 00:00:00 is JD 2451544.5 |
26 ** | 26 ** |
27 ** This implemention requires years to be expressed as a 4-digit number | 27 ** This implementation requires years to be expressed as a 4-digit number |
28 ** which means that only dates between 0000-01-01 and 9999-12-31 can | 28 ** which means that only dates between 0000-01-01 and 9999-12-31 can |
29 ** be represented, even though julian day numbers allow a much wider | 29 ** be represented, even though julian day numbers allow a much wider |
30 ** range of dates. | 30 ** range of dates. |
31 ** | 31 ** |
32 ** The Gregorian calendar system is used for all dates and times, | 32 ** The Gregorian calendar system is used for all dates and times, |
33 ** even those that predate the Gregorian calendar. Historians usually | 33 ** even those that predate the Gregorian calendar. Historians usually |
34 ** use the Julian calendar for dates prior to 1582-10-15 and for some | 34 ** use the Julian calendar for dates prior to 1582-10-15 and for some |
35 ** dates afterwards, depending on locale. Beware of this difference. | 35 ** dates afterwards, depending on locale. Beware of this difference. |
36 ** | 36 ** |
37 ** The conversion algorithms are implemented based on descriptions | 37 ** The conversion algorithms are implemented based on descriptions |
38 ** in the following text: | 38 ** in the following text: |
39 ** | 39 ** |
40 ** Jean Meeus | 40 ** Jean Meeus |
41 ** Astronomical Algorithms, 2nd Edition, 1998 | 41 ** Astronomical Algorithms, 2nd Edition, 1998 |
42 ** ISBM 0-943396-61-1 | 42 ** ISBM 0-943396-61-1 |
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 ** On recent Windows platforms, the localtime_s() function is available | |
55 ** as part of the "Secure CRT". It is essentially equivalent to | |
56 ** localtime_r() available under most POSIX platforms, except that the | |
57 ** order of the parameters is reversed. | |
58 ** | |
59 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. | |
60 ** | |
61 ** If the user has not indicated to use localtime_r() or localtime_s() | |
62 ** already, check for an MSVC build environment that provides | |
63 ** localtime_s(). | |
64 */ | |
65 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \ | |
66 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) | |
67 #define HAVE_LOCALTIME_S 1 | |
68 #endif | |
69 | 53 |
70 /* | 54 /* |
71 ** A structure for holding a single date and time. | 55 ** A structure for holding a single date and time. |
72 */ | 56 */ |
73 typedef struct DateTime DateTime; | 57 typedef struct DateTime DateTime; |
74 struct DateTime { | 58 struct DateTime { |
75 sqlite3_int64 iJD; /* The julian day number times 86400000 */ | 59 sqlite3_int64 iJD; /* The julian day number times 86400000 */ |
76 int Y, M, D; /* Year, month, and day */ | 60 int Y, M, D; /* Year, month, and day */ |
77 int h, m; /* Hour and minutes */ | 61 int h, m; /* Hour and minutes */ |
78 int tz; /* Timezone offset in minutes */ | 62 int tz; /* Timezone offset in minutes */ |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 p->Y = neg ? -Y : Y; | 282 p->Y = neg ? -Y : Y; |
299 p->M = M; | 283 p->M = M; |
300 p->D = D; | 284 p->D = D; |
301 if( p->validTZ ){ | 285 if( p->validTZ ){ |
302 computeJD(p); | 286 computeJD(p); |
303 } | 287 } |
304 return 0; | 288 return 0; |
305 } | 289 } |
306 | 290 |
307 /* | 291 /* |
308 ** Set the time to the current time reported by the VFS | 292 ** Set the time to the current time reported by the VFS. |
| 293 ** |
| 294 ** Return the number of errors. |
309 */ | 295 */ |
310 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ | 296 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ |
311 sqlite3 *db = sqlite3_context_db_handle(context); | 297 p->iJD = sqlite3StmtCurrentTime(context); |
312 sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD); | 298 if( p->iJD>0 ){ |
313 p->validJD = 1; | 299 p->validJD = 1; |
| 300 return 0; |
| 301 }else{ |
| 302 return 1; |
| 303 } |
314 } | 304 } |
315 | 305 |
316 /* | 306 /* |
317 ** Attempt to parse the given string into a Julian Day Number. Return | 307 ** Attempt to parse the given string into a Julian Day Number. Return |
318 ** the number of errors. | 308 ** the number of errors. |
319 ** | 309 ** |
320 ** The following are acceptable forms for the input string: | 310 ** The following are acceptable forms for the input string: |
321 ** | 311 ** |
322 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM | 312 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
323 ** DDDD.DD | 313 ** DDDD.DD |
324 ** now | 314 ** now |
325 ** | 315 ** |
326 ** In the first form, the +/-HH:MM is always optional. The fractional | 316 ** In the first form, the +/-HH:MM is always optional. The fractional |
327 ** seconds extension (the ".FFF") is optional. The seconds portion | 317 ** seconds extension (the ".FFF") is optional. The seconds portion |
328 ** (":SS.FFF") is option. The year and date can be omitted as long | 318 ** (":SS.FFF") is option. The year and date can be omitted as long |
329 ** as there is a time string. The time string can be omitted as long | 319 ** as there is a time string. The time string can be omitted as long |
330 ** as there is a year and date. | 320 ** as there is a year and date. |
331 */ | 321 */ |
332 static int parseDateOrTime( | 322 static int parseDateOrTime( |
333 sqlite3_context *context, | 323 sqlite3_context *context, |
334 const char *zDate, | 324 const char *zDate, |
335 DateTime *p | 325 DateTime *p |
336 ){ | 326 ){ |
337 double r; | 327 double r; |
338 if( parseYyyyMmDd(zDate,p)==0 ){ | 328 if( parseYyyyMmDd(zDate,p)==0 ){ |
339 return 0; | 329 return 0; |
340 }else if( parseHhMmSs(zDate, p)==0 ){ | 330 }else if( parseHhMmSs(zDate, p)==0 ){ |
341 return 0; | 331 return 0; |
342 }else if( sqlite3StrICmp(zDate,"now")==0){ | 332 }else if( sqlite3StrICmp(zDate,"now")==0){ |
343 setDateTimeToCurrent(context, p); | 333 return setDateTimeToCurrent(context, p); |
344 return 0; | |
345 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ | 334 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ |
346 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); | 335 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); |
347 p->validJD = 1; | 336 p->validJD = 1; |
348 return 0; | 337 return 0; |
349 } | 338 } |
350 return 1; | 339 return 1; |
351 } | 340 } |
352 | 341 |
353 /* | 342 /* |
354 ** Compute the Year, Month, and Day from the julian day number. | 343 ** Compute the Year, Month, and Day from the julian day number. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 | 393 |
405 /* | 394 /* |
406 ** Clear the YMD and HMS and the TZ | 395 ** Clear the YMD and HMS and the TZ |
407 */ | 396 */ |
408 static void clearYMD_HMS_TZ(DateTime *p){ | 397 static void clearYMD_HMS_TZ(DateTime *p){ |
409 p->validYMD = 0; | 398 p->validYMD = 0; |
410 p->validHMS = 0; | 399 p->validHMS = 0; |
411 p->validTZ = 0; | 400 p->validTZ = 0; |
412 } | 401 } |
413 | 402 |
| 403 /* |
| 404 ** On recent Windows platforms, the localtime_s() function is available |
| 405 ** as part of the "Secure CRT". It is essentially equivalent to |
| 406 ** localtime_r() available under most POSIX platforms, except that the |
| 407 ** order of the parameters is reversed. |
| 408 ** |
| 409 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. |
| 410 ** |
| 411 ** If the user has not indicated to use localtime_r() or localtime_s() |
| 412 ** already, check for an MSVC build environment that provides |
| 413 ** localtime_s(). |
| 414 */ |
| 415 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \ |
| 416 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) |
| 417 #define HAVE_LOCALTIME_S 1 |
| 418 #endif |
| 419 |
414 #ifndef SQLITE_OMIT_LOCALTIME | 420 #ifndef SQLITE_OMIT_LOCALTIME |
415 /* | 421 /* |
416 ** Compute the difference (in milliseconds) | 422 ** The following routine implements the rough equivalent of localtime_r() |
417 ** between localtime and UTC (a.k.a. GMT) | 423 ** using whatever operating-system specific localtime facility that |
418 ** for the time value p where p is in UTC. | 424 ** is available. This routine returns 0 on success and |
| 425 ** non-zero on any kind of error. |
| 426 ** |
| 427 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this |
| 428 ** routine will always fail. |
| 429 ** |
| 430 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C |
| 431 ** library function localtime_r() is used to assist in the calculation of |
| 432 ** local time. |
419 */ | 433 */ |
420 static sqlite3_int64 localtimeOffset(DateTime *p){ | 434 static int osLocaltime(time_t *t, struct tm *pTm){ |
| 435 int rc; |
| 436 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \ |
| 437 && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S) |
| 438 struct tm *pX; |
| 439 #if SQLITE_THREADSAFE>0 |
| 440 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 441 #endif |
| 442 sqlite3_mutex_enter(mutex); |
| 443 pX = localtime(t); |
| 444 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 445 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; |
| 446 #endif |
| 447 if( pX ) *pTm = *pX; |
| 448 sqlite3_mutex_leave(mutex); |
| 449 rc = pX==0; |
| 450 #else |
| 451 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 452 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; |
| 453 #endif |
| 454 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R |
| 455 rc = localtime_r(t, pTm)==0; |
| 456 #else |
| 457 rc = localtime_s(pTm, t); |
| 458 #endif /* HAVE_LOCALTIME_R */ |
| 459 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ |
| 460 return rc; |
| 461 } |
| 462 #endif /* SQLITE_OMIT_LOCALTIME */ |
| 463 |
| 464 |
| 465 #ifndef SQLITE_OMIT_LOCALTIME |
| 466 /* |
| 467 ** Compute the difference (in milliseconds) between localtime and UTC |
| 468 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs, |
| 469 ** return this value and set *pRc to SQLITE_OK. |
| 470 ** |
| 471 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value |
| 472 ** is undefined in this case. |
| 473 */ |
| 474 static sqlite3_int64 localtimeOffset( |
| 475 DateTime *p, /* Date at which to calculate offset */ |
| 476 sqlite3_context *pCtx, /* Write error here if one occurs */ |
| 477 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */ |
| 478 ){ |
421 DateTime x, y; | 479 DateTime x, y; |
422 time_t t; | 480 time_t t; |
| 481 struct tm sLocal; |
| 482 |
| 483 /* Initialize the contents of sLocal to avoid a compiler warning. */ |
| 484 memset(&sLocal, 0, sizeof(sLocal)); |
| 485 |
423 x = *p; | 486 x = *p; |
424 computeYMD_HMS(&x); | 487 computeYMD_HMS(&x); |
425 if( x.Y<1971 || x.Y>=2038 ){ | 488 if( x.Y<1971 || x.Y>=2038 ){ |
| 489 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only |
| 490 ** works for years between 1970 and 2037. For dates outside this range, |
| 491 ** SQLite attempts to map the year into an equivalent year within this |
| 492 ** range, do the calculation, then map the year back. |
| 493 */ |
426 x.Y = 2000; | 494 x.Y = 2000; |
427 x.M = 1; | 495 x.M = 1; |
428 x.D = 1; | 496 x.D = 1; |
429 x.h = 0; | 497 x.h = 0; |
430 x.m = 0; | 498 x.m = 0; |
431 x.s = 0.0; | 499 x.s = 0.0; |
432 } else { | 500 } else { |
433 int s = (int)(x.s + 0.5); | 501 int s = (int)(x.s + 0.5); |
434 x.s = s; | 502 x.s = s; |
435 } | 503 } |
436 x.tz = 0; | 504 x.tz = 0; |
437 x.validJD = 0; | 505 x.validJD = 0; |
438 computeJD(&x); | 506 computeJD(&x); |
439 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); | 507 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); |
440 #ifdef HAVE_LOCALTIME_R | 508 if( osLocaltime(&t, &sLocal) ){ |
441 { | 509 sqlite3_result_error(pCtx, "local time unavailable", -1); |
442 struct tm sLocal; | 510 *pRc = SQLITE_ERROR; |
443 localtime_r(&t, &sLocal); | 511 return 0; |
444 y.Y = sLocal.tm_year + 1900; | |
445 y.M = sLocal.tm_mon + 1; | |
446 y.D = sLocal.tm_mday; | |
447 y.h = sLocal.tm_hour; | |
448 y.m = sLocal.tm_min; | |
449 y.s = sLocal.tm_sec; | |
450 } | 512 } |
451 #elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S | 513 y.Y = sLocal.tm_year + 1900; |
452 { | 514 y.M = sLocal.tm_mon + 1; |
453 struct tm sLocal; | 515 y.D = sLocal.tm_mday; |
454 localtime_s(&sLocal, &t); | 516 y.h = sLocal.tm_hour; |
455 y.Y = sLocal.tm_year + 1900; | 517 y.m = sLocal.tm_min; |
456 y.M = sLocal.tm_mon + 1; | 518 y.s = sLocal.tm_sec; |
457 y.D = sLocal.tm_mday; | |
458 y.h = sLocal.tm_hour; | |
459 y.m = sLocal.tm_min; | |
460 y.s = sLocal.tm_sec; | |
461 } | |
462 #else | |
463 { | |
464 struct tm *pTm; | |
465 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | |
466 pTm = localtime(&t); | |
467 y.Y = pTm->tm_year + 1900; | |
468 y.M = pTm->tm_mon + 1; | |
469 y.D = pTm->tm_mday; | |
470 y.h = pTm->tm_hour; | |
471 y.m = pTm->tm_min; | |
472 y.s = pTm->tm_sec; | |
473 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | |
474 } | |
475 #endif | |
476 y.validYMD = 1; | 519 y.validYMD = 1; |
477 y.validHMS = 1; | 520 y.validHMS = 1; |
478 y.validJD = 0; | 521 y.validJD = 0; |
479 y.validTZ = 0; | 522 y.validTZ = 0; |
480 computeJD(&y); | 523 computeJD(&y); |
| 524 *pRc = SQLITE_OK; |
481 return y.iJD - x.iJD; | 525 return y.iJD - x.iJD; |
482 } | 526 } |
483 #endif /* SQLITE_OMIT_LOCALTIME */ | 527 #endif /* SQLITE_OMIT_LOCALTIME */ |
484 | 528 |
485 /* | 529 /* |
486 ** Process a modifier to a date-time stamp. The modifiers are | 530 ** Process a modifier to a date-time stamp. The modifiers are |
487 ** as follows: | 531 ** as follows: |
488 ** | 532 ** |
489 ** NNN days | 533 ** NNN days |
490 ** NNN hours | 534 ** NNN hours |
491 ** NNN minutes | 535 ** NNN minutes |
492 ** NNN.NNNN seconds | 536 ** NNN.NNNN seconds |
493 ** NNN months | 537 ** NNN months |
494 ** NNN years | 538 ** NNN years |
495 ** start of month | 539 ** start of month |
496 ** start of year | 540 ** start of year |
497 ** start of week | 541 ** start of week |
498 ** start of day | 542 ** start of day |
499 ** weekday N | 543 ** weekday N |
500 ** unixepoch | 544 ** unixepoch |
501 ** localtime | 545 ** localtime |
502 ** utc | 546 ** utc |
503 ** | 547 ** |
504 ** Return 0 on success and 1 if there is any kind of error. | 548 ** Return 0 on success and 1 if there is any kind of error. If the error |
| 549 ** is in a system call (i.e. localtime()), then an error message is written |
| 550 ** to context pCtx. If the error is an unrecognized modifier, no error is |
| 551 ** written to pCtx. |
505 */ | 552 */ |
506 static int parseModifier(const char *zMod, DateTime *p){ | 553 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ |
507 int rc = 1; | 554 int rc = 1; |
508 int n; | 555 int n; |
509 double r; | 556 double r; |
510 char *z, zBuf[30]; | 557 char *z, zBuf[30]; |
511 z = zBuf; | 558 z = zBuf; |
512 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){ | 559 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){ |
513 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]]; | 560 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]]; |
514 } | 561 } |
515 z[n] = 0; | 562 z[n] = 0; |
516 switch( z[0] ){ | 563 switch( z[0] ){ |
517 #ifndef SQLITE_OMIT_LOCALTIME | 564 #ifndef SQLITE_OMIT_LOCALTIME |
518 case 'l': { | 565 case 'l': { |
519 /* localtime | 566 /* localtime |
520 ** | 567 ** |
521 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to | 568 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to |
522 ** show local time. | 569 ** show local time. |
523 */ | 570 */ |
524 if( strcmp(z, "localtime")==0 ){ | 571 if( strcmp(z, "localtime")==0 ){ |
525 computeJD(p); | 572 computeJD(p); |
526 p->iJD += localtimeOffset(p); | 573 p->iJD += localtimeOffset(p, pCtx, &rc); |
527 clearYMD_HMS_TZ(p); | 574 clearYMD_HMS_TZ(p); |
528 rc = 0; | |
529 } | 575 } |
530 break; | 576 break; |
531 } | 577 } |
532 #endif | 578 #endif |
533 case 'u': { | 579 case 'u': { |
534 /* | 580 /* |
535 ** unixepoch | 581 ** unixepoch |
536 ** | 582 ** |
537 ** Treat the current value of p->iJD as the number of | 583 ** Treat the current value of p->iJD as the number of |
538 ** seconds since 1970. Convert to a real julian day number. | 584 ** seconds since 1970. Convert to a real julian day number. |
539 */ | 585 */ |
540 if( strcmp(z, "unixepoch")==0 && p->validJD ){ | 586 if( strcmp(z, "unixepoch")==0 && p->validJD ){ |
541 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; | 587 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; |
542 clearYMD_HMS_TZ(p); | 588 clearYMD_HMS_TZ(p); |
543 rc = 0; | 589 rc = 0; |
544 } | 590 } |
545 #ifndef SQLITE_OMIT_LOCALTIME | 591 #ifndef SQLITE_OMIT_LOCALTIME |
546 else if( strcmp(z, "utc")==0 ){ | 592 else if( strcmp(z, "utc")==0 ){ |
547 sqlite3_int64 c1; | 593 sqlite3_int64 c1; |
548 computeJD(p); | 594 computeJD(p); |
549 c1 = localtimeOffset(p); | 595 c1 = localtimeOffset(p, pCtx, &rc); |
550 p->iJD -= c1; | 596 if( rc==SQLITE_OK ){ |
551 clearYMD_HMS_TZ(p); | 597 p->iJD -= c1; |
552 p->iJD += c1 - localtimeOffset(p); | 598 clearYMD_HMS_TZ(p); |
553 rc = 0; | 599 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); |
| 600 } |
554 } | 601 } |
555 #endif | 602 #endif |
556 break; | 603 break; |
557 } | 604 } |
558 case 'w': { | 605 case 'w': { |
559 /* | 606 /* |
560 ** weekday N | 607 ** weekday N |
561 ** | 608 ** |
562 ** Move the date to the same time on the next occurrence of | 609 ** Move the date to the same time on the next occurrence of |
563 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the | 610 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 sqlite3_context *context, | 759 sqlite3_context *context, |
713 int argc, | 760 int argc, |
714 sqlite3_value **argv, | 761 sqlite3_value **argv, |
715 DateTime *p | 762 DateTime *p |
716 ){ | 763 ){ |
717 int i; | 764 int i; |
718 const unsigned char *z; | 765 const unsigned char *z; |
719 int eType; | 766 int eType; |
720 memset(p, 0, sizeof(*p)); | 767 memset(p, 0, sizeof(*p)); |
721 if( argc==0 ){ | 768 if( argc==0 ){ |
722 setDateTimeToCurrent(context, p); | 769 return setDateTimeToCurrent(context, p); |
723 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT | 770 } |
| 771 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT |
724 || eType==SQLITE_INTEGER ){ | 772 || eType==SQLITE_INTEGER ){ |
725 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); | 773 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); |
726 p->validJD = 1; | 774 p->validJD = 1; |
727 }else{ | 775 }else{ |
728 z = sqlite3_value_text(argv[0]); | 776 z = sqlite3_value_text(argv[0]); |
729 if( !z || parseDateOrTime(context, (char*)z, p) ){ | 777 if( !z || parseDateOrTime(context, (char*)z, p) ){ |
730 return 1; | 778 return 1; |
731 } | 779 } |
732 } | 780 } |
733 for(i=1; i<argc; i++){ | 781 for(i=1; i<argc; i++){ |
734 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){ | 782 z = sqlite3_value_text(argv[i]); |
735 return 1; | 783 if( z==0 || parseModifier(context, (char*)z, p) ) return 1; |
736 } | |
737 } | 784 } |
738 return 0; | 785 return 0; |
739 } | 786 } |
740 | 787 |
741 | 788 |
742 /* | 789 /* |
743 ** The following routines implement the various date and time functions | 790 ** The following routines implement the various date and time functions |
744 ** of SQLite. | 791 ** of SQLite. |
745 */ | 792 */ |
746 | 793 |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1026 */ | 1073 */ |
1027 static void currentTimeFunc( | 1074 static void currentTimeFunc( |
1028 sqlite3_context *context, | 1075 sqlite3_context *context, |
1029 int argc, | 1076 int argc, |
1030 sqlite3_value **argv | 1077 sqlite3_value **argv |
1031 ){ | 1078 ){ |
1032 time_t t; | 1079 time_t t; |
1033 char *zFormat = (char *)sqlite3_user_data(context); | 1080 char *zFormat = (char *)sqlite3_user_data(context); |
1034 sqlite3 *db; | 1081 sqlite3 *db; |
1035 sqlite3_int64 iT; | 1082 sqlite3_int64 iT; |
| 1083 struct tm *pTm; |
| 1084 struct tm sNow; |
1036 char zBuf[20]; | 1085 char zBuf[20]; |
1037 | 1086 |
1038 UNUSED_PARAMETER(argc); | 1087 UNUSED_PARAMETER(argc); |
1039 UNUSED_PARAMETER(argv); | 1088 UNUSED_PARAMETER(argv); |
1040 | 1089 |
1041 db = sqlite3_context_db_handle(context); | 1090 iT = sqlite3StmtCurrentTime(context); |
1042 sqlite3OsCurrentTimeInt64(db->pVfs, &iT); | 1091 if( iT<=0 ) return; |
1043 t = iT/1000 - 10000*(sqlite3_int64)21086676; | 1092 t = iT/1000 - 10000*(sqlite3_int64)21086676; |
1044 #ifdef HAVE_GMTIME_R | 1093 #ifdef HAVE_GMTIME_R |
1045 { | 1094 pTm = gmtime_r(&t, &sNow); |
1046 struct tm sNow; | 1095 #else |
1047 gmtime_r(&t, &sNow); | 1096 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 1097 pTm = gmtime(&t); |
| 1098 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); |
| 1099 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 1100 #endif |
| 1101 if( pTm ){ |
1048 strftime(zBuf, 20, zFormat, &sNow); | 1102 strftime(zBuf, 20, zFormat, &sNow); |
| 1103 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
1049 } | 1104 } |
1050 #else | |
1051 { | |
1052 struct tm *pTm; | |
1053 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | |
1054 pTm = gmtime(&t); | |
1055 strftime(zBuf, 20, zFormat, pTm); | |
1056 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | |
1057 } | |
1058 #endif | |
1059 | |
1060 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); | |
1061 } | 1105 } |
1062 #endif | 1106 #endif |
1063 | 1107 |
1064 /* | 1108 /* |
1065 ** This function registered all of the above C functions as SQL | 1109 ** This function registered all of the above C functions as SQL |
1066 ** functions. This should be the only routine in this file with | 1110 ** functions. This should be the only routine in this file with |
1067 ** external linkage. | 1111 ** external linkage. |
1068 */ | 1112 */ |
1069 void sqlite3RegisterDateTimeFunctions(void){ | 1113 void sqlite3RegisterDateTimeFunctions(void){ |
1070 static SQLITE_WSD FuncDef aDateTimeFuncs[] = { | 1114 static SQLITE_WSD FuncDef aDateTimeFuncs[] = { |
(...skipping 13 matching lines...) Expand all Loading... |
1084 #endif | 1128 #endif |
1085 }; | 1129 }; |
1086 int i; | 1130 int i; |
1087 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); | 1131 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
1088 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); | 1132 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); |
1089 | 1133 |
1090 for(i=0; i<ArraySize(aDateTimeFuncs); i++){ | 1134 for(i=0; i<ArraySize(aDateTimeFuncs); i++){ |
1091 sqlite3FuncDefInsert(pHash, &aFunc[i]); | 1135 sqlite3FuncDefInsert(pHash, &aFunc[i]); |
1092 } | 1136 } |
1093 } | 1137 } |
OLD | NEW |