| 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 implementation 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) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 58 struct DateTime { | 58 struct DateTime { |
| 59 sqlite3_int64 iJD; /* The julian day number times 86400000 */ | 59 sqlite3_int64 iJD; /* The julian day number times 86400000 */ |
| 60 int Y, M, D; /* Year, month, and day */ | 60 int Y, M, D; /* Year, month, and day */ |
| 61 int h, m; /* Hour and minutes */ | 61 int h, m; /* Hour and minutes */ |
| 62 int tz; /* Timezone offset in minutes */ | 62 int tz; /* Timezone offset in minutes */ |
| 63 double s; /* Seconds */ | 63 double s; /* Seconds */ |
| 64 char validYMD; /* True (1) if Y,M,D are valid */ | 64 char validYMD; /* True (1) if Y,M,D are valid */ |
| 65 char validHMS; /* True (1) if h,m,s are valid */ | 65 char validHMS; /* True (1) if h,m,s are valid */ |
| 66 char validJD; /* True (1) if iJD is valid */ | 66 char validJD; /* True (1) if iJD is valid */ |
| 67 char validTZ; /* True (1) if tz is valid */ | 67 char validTZ; /* True (1) if tz is valid */ |
| 68 char tzSet; /* Timezone was set explicitly */ |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 | 71 |
| 71 /* | 72 /* |
| 72 ** Convert zDate into one or more integers. Additional arguments | 73 ** Convert zDate into one or more integers. Additional arguments |
| 73 ** come in groups of 5 as follows: | 74 ** come in groups of 5 as follows: |
| 74 ** | 75 ** |
| 75 ** N number of digits in the integer | 76 ** N number of digits in the integer |
| 76 ** min minimum allowed value of the integer | 77 ** min minimum allowed value of the integer |
| 77 ** max maximum allowed value of the integer | 78 ** max maximum allowed value of the integer |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return c!=0; | 152 return c!=0; |
| 152 } | 153 } |
| 153 zDate++; | 154 zDate++; |
| 154 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ | 155 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ |
| 155 return 1; | 156 return 1; |
| 156 } | 157 } |
| 157 zDate += 5; | 158 zDate += 5; |
| 158 p->tz = sgn*(nMn + nHr*60); | 159 p->tz = sgn*(nMn + nHr*60); |
| 159 zulu_time: | 160 zulu_time: |
| 160 while( sqlite3Isspace(*zDate) ){ zDate++; } | 161 while( sqlite3Isspace(*zDate) ){ zDate++; } |
| 162 p->tzSet = 1; |
| 161 return *zDate!=0; | 163 return *zDate!=0; |
| 162 } | 164 } |
| 163 | 165 |
| 164 /* | 166 /* |
| 165 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. | 167 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. |
| 166 ** The HH, MM, and SS must each be exactly 2 digits. The | 168 ** The HH, MM, and SS must each be exactly 2 digits. The |
| 167 ** fractional seconds FFFF can be one or more digits. | 169 ** fractional seconds FFFF can be one or more digits. |
| 168 ** | 170 ** |
| 169 ** Return 1 if there is a parsing error and 0 on success. | 171 ** Return 1 if there is a parsing error and 0 on success. |
| 170 */ | 172 */ |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 p->iJD = sqlite3StmtCurrentTime(context); | 299 p->iJD = sqlite3StmtCurrentTime(context); |
| 298 if( p->iJD>0 ){ | 300 if( p->iJD>0 ){ |
| 299 p->validJD = 1; | 301 p->validJD = 1; |
| 300 return 0; | 302 return 0; |
| 301 }else{ | 303 }else{ |
| 302 return 1; | 304 return 1; |
| 303 } | 305 } |
| 304 } | 306 } |
| 305 | 307 |
| 306 /* | 308 /* |
| 307 ** Attempt to parse the given string into a Julian Day Number. Return | 309 ** Attempt to parse the given string into a julian day number. Return |
| 308 ** the number of errors. | 310 ** the number of errors. |
| 309 ** | 311 ** |
| 310 ** The following are acceptable forms for the input string: | 312 ** The following are acceptable forms for the input string: |
| 311 ** | 313 ** |
| 312 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM | 314 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| 313 ** DDDD.DD | 315 ** DDDD.DD |
| 314 ** now | 316 ** now |
| 315 ** | 317 ** |
| 316 ** In the first form, the +/-HH:MM is always optional. The fractional | 318 ** In the first form, the +/-HH:MM is always optional. The fractional |
| 317 ** seconds extension (the ".FFF") is optional. The seconds portion | 319 ** seconds extension (the ".FFF") is optional. The seconds portion |
| (...skipping 30 matching lines...) Expand all Loading... |
| 348 if( !p->validJD ){ | 350 if( !p->validJD ){ |
| 349 p->Y = 2000; | 351 p->Y = 2000; |
| 350 p->M = 1; | 352 p->M = 1; |
| 351 p->D = 1; | 353 p->D = 1; |
| 352 }else{ | 354 }else{ |
| 353 Z = (int)((p->iJD + 43200000)/86400000); | 355 Z = (int)((p->iJD + 43200000)/86400000); |
| 354 A = (int)((Z - 1867216.25)/36524.25); | 356 A = (int)((Z - 1867216.25)/36524.25); |
| 355 A = Z + 1 + A - (A/4); | 357 A = Z + 1 + A - (A/4); |
| 356 B = A + 1524; | 358 B = A + 1524; |
| 357 C = (int)((B - 122.1)/365.25); | 359 C = (int)((B - 122.1)/365.25); |
| 358 D = (36525*C)/100; | 360 D = (36525*(C&32767))/100; |
| 359 E = (int)((B-D)/30.6001); | 361 E = (int)((B-D)/30.6001); |
| 360 X1 = (int)(30.6001*E); | 362 X1 = (int)(30.6001*E); |
| 361 p->D = B - D - X1; | 363 p->D = B - D - X1; |
| 362 p->M = E<14 ? E-1 : E-13; | 364 p->M = E<14 ? E-1 : E-13; |
| 363 p->Y = p->M>2 ? C - 4716 : C - 4715; | 365 p->Y = p->M>2 ? C - 4716 : C - 4715; |
| 364 } | 366 } |
| 365 p->validYMD = 1; | 367 p->validYMD = 1; |
| 366 } | 368 } |
| 367 | 369 |
| 368 /* | 370 /* |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 ** as part of the "Secure CRT". It is essentially equivalent to | 407 ** as part of the "Secure CRT". It is essentially equivalent to |
| 406 ** localtime_r() available under most POSIX platforms, except that the | 408 ** localtime_r() available under most POSIX platforms, except that the |
| 407 ** order of the parameters is reversed. | 409 ** order of the parameters is reversed. |
| 408 ** | 410 ** |
| 409 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. | 411 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. |
| 410 ** | 412 ** |
| 411 ** If the user has not indicated to use localtime_r() or localtime_s() | 413 ** If the user has not indicated to use localtime_r() or localtime_s() |
| 412 ** already, check for an MSVC build environment that provides | 414 ** already, check for an MSVC build environment that provides |
| 413 ** localtime_s(). | 415 ** localtime_s(). |
| 414 */ | 416 */ |
| 415 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \ | 417 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \ |
| 416 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) | 418 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) |
| 419 #undef HAVE_LOCALTIME_S |
| 417 #define HAVE_LOCALTIME_S 1 | 420 #define HAVE_LOCALTIME_S 1 |
| 418 #endif | 421 #endif |
| 419 | 422 |
| 420 #ifndef SQLITE_OMIT_LOCALTIME | 423 #ifndef SQLITE_OMIT_LOCALTIME |
| 421 /* | 424 /* |
| 422 ** The following routine implements the rough equivalent of localtime_r() | 425 ** The following routine implements the rough equivalent of localtime_r() |
| 423 ** using whatever operating-system specific localtime facility that | 426 ** using whatever operating-system specific localtime facility that |
| 424 ** is available. This routine returns 0 on success and | 427 ** is available. This routine returns 0 on success and |
| 425 ** non-zero on any kind of error. | 428 ** non-zero on any kind of error. |
| 426 ** | 429 ** |
| 427 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this | 430 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this |
| 428 ** routine will always fail. | 431 ** routine will always fail. |
| 429 ** | 432 ** |
| 430 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C | 433 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C |
| 431 ** library function localtime_r() is used to assist in the calculation of | 434 ** library function localtime_r() is used to assist in the calculation of |
| 432 ** local time. | 435 ** local time. |
| 433 */ | 436 */ |
| 434 static int osLocaltime(time_t *t, struct tm *pTm){ | 437 static int osLocaltime(time_t *t, struct tm *pTm){ |
| 435 int rc; | 438 int rc; |
| 436 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \ | 439 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S |
| 437 && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S) | |
| 438 struct tm *pX; | 440 struct tm *pX; |
| 439 #if SQLITE_THREADSAFE>0 | 441 #if SQLITE_THREADSAFE>0 |
| 440 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 442 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 441 #endif | 443 #endif |
| 442 sqlite3_mutex_enter(mutex); | 444 sqlite3_mutex_enter(mutex); |
| 443 pX = localtime(t); | 445 pX = localtime(t); |
| 444 #ifndef SQLITE_OMIT_BUILTIN_TEST | 446 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 445 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; | 447 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; |
| 446 #endif | 448 #endif |
| 447 if( pX ) *pTm = *pX; | 449 if( pX ) *pTm = *pX; |
| 448 sqlite3_mutex_leave(mutex); | 450 sqlite3_mutex_leave(mutex); |
| 449 rc = pX==0; | 451 rc = pX==0; |
| 450 #else | 452 #else |
| 451 #ifndef SQLITE_OMIT_BUILTIN_TEST | 453 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 452 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; | 454 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; |
| 453 #endif | 455 #endif |
| 454 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R | 456 #if HAVE_LOCALTIME_R |
| 455 rc = localtime_r(t, pTm)==0; | 457 rc = localtime_r(t, pTm)==0; |
| 456 #else | 458 #else |
| 457 rc = localtime_s(pTm, t); | 459 rc = localtime_s(pTm, t); |
| 458 #endif /* HAVE_LOCALTIME_R */ | 460 #endif /* HAVE_LOCALTIME_R */ |
| 459 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ | 461 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ |
| 460 return rc; | 462 return rc; |
| 461 } | 463 } |
| 462 #endif /* SQLITE_OMIT_LOCALTIME */ | 464 #endif /* SQLITE_OMIT_LOCALTIME */ |
| 463 | 465 |
| 464 | 466 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 ** Treat the current value of p->iJD as the number of | 585 ** Treat the current value of p->iJD as the number of |
| 584 ** seconds since 1970. Convert to a real julian day number. | 586 ** seconds since 1970. Convert to a real julian day number. |
| 585 */ | 587 */ |
| 586 if( strcmp(z, "unixepoch")==0 && p->validJD ){ | 588 if( strcmp(z, "unixepoch")==0 && p->validJD ){ |
| 587 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; | 589 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; |
| 588 clearYMD_HMS_TZ(p); | 590 clearYMD_HMS_TZ(p); |
| 589 rc = 0; | 591 rc = 0; |
| 590 } | 592 } |
| 591 #ifndef SQLITE_OMIT_LOCALTIME | 593 #ifndef SQLITE_OMIT_LOCALTIME |
| 592 else if( strcmp(z, "utc")==0 ){ | 594 else if( strcmp(z, "utc")==0 ){ |
| 593 sqlite3_int64 c1; | 595 if( p->tzSet==0 ){ |
| 594 computeJD(p); | 596 sqlite3_int64 c1; |
| 595 c1 = localtimeOffset(p, pCtx, &rc); | 597 computeJD(p); |
| 596 if( rc==SQLITE_OK ){ | 598 c1 = localtimeOffset(p, pCtx, &rc); |
| 597 p->iJD -= c1; | 599 if( rc==SQLITE_OK ){ |
| 598 clearYMD_HMS_TZ(p); | 600 p->iJD -= c1; |
| 599 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); | 601 clearYMD_HMS_TZ(p); |
| 602 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); |
| 603 } |
| 604 p->tzSet = 1; |
| 605 }else{ |
| 606 rc = SQLITE_OK; |
| 600 } | 607 } |
| 601 } | 608 } |
| 602 #endif | 609 #endif |
| 603 break; | 610 break; |
| 604 } | 611 } |
| 605 case 'w': { | 612 case 'w': { |
| 606 /* | 613 /* |
| 607 ** weekday N | 614 ** weekday N |
| 608 ** | 615 ** |
| 609 ** Move the date to the same time on the next occurrence of | 616 ** Move the date to the same time on the next occurrence of |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 | 875 |
| 869 /* | 876 /* |
| 870 ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) | 877 ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) |
| 871 ** | 878 ** |
| 872 ** Return a string described by FORMAT. Conversions as follows: | 879 ** Return a string described by FORMAT. Conversions as follows: |
| 873 ** | 880 ** |
| 874 ** %d day of month | 881 ** %d day of month |
| 875 ** %f ** fractional seconds SS.SSS | 882 ** %f ** fractional seconds SS.SSS |
| 876 ** %H hour 00-24 | 883 ** %H hour 00-24 |
| 877 ** %j day of year 000-366 | 884 ** %j day of year 000-366 |
| 878 ** %J ** Julian day number | 885 ** %J ** julian day number |
| 879 ** %m month 01-12 | 886 ** %m month 01-12 |
| 880 ** %M minute 00-59 | 887 ** %M minute 00-59 |
| 881 ** %s seconds since 1970-01-01 | 888 ** %s seconds since 1970-01-01 |
| 882 ** %S seconds 00-59 | 889 ** %S seconds 00-59 |
| 883 ** %w day of week 0-6 sunday==0 | 890 ** %w day of week 0-6 sunday==0 |
| 884 ** %W week of year 00-53 | 891 ** %W week of year 00-53 |
| 885 ** %Y year 0000-9999 | 892 ** %Y year 0000-9999 |
| 886 ** %% % | 893 ** %% % |
| 887 */ | 894 */ |
| 888 static void strftimeFunc( | 895 static void strftimeFunc( |
| 889 sqlite3_context *context, | 896 sqlite3_context *context, |
| 890 int argc, | 897 int argc, |
| 891 sqlite3_value **argv | 898 sqlite3_value **argv |
| 892 ){ | 899 ){ |
| 893 DateTime x; | 900 DateTime x; |
| 894 u64 n; | 901 u64 n; |
| 895 size_t i,j; | 902 size_t i,j; |
| 896 char *z; | 903 char *z; |
| 897 sqlite3 *db; | 904 sqlite3 *db; |
| 898 const char *zFmt = (const char*)sqlite3_value_text(argv[0]); | 905 const char *zFmt; |
| 899 char zBuf[100]; | 906 char zBuf[100]; |
| 907 if( argc==0 ) return; |
| 908 zFmt = (const char*)sqlite3_value_text(argv[0]); |
| 900 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; | 909 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; |
| 901 db = sqlite3_context_db_handle(context); | 910 db = sqlite3_context_db_handle(context); |
| 902 for(i=0, n=1; zFmt[i]; i++, n++){ | 911 for(i=0, n=1; zFmt[i]; i++, n++){ |
| 903 if( zFmt[i]=='%' ){ | 912 if( zFmt[i]=='%' ){ |
| 904 switch( zFmt[i+1] ){ | 913 switch( zFmt[i+1] ){ |
| 905 case 'd': | 914 case 'd': |
| 906 case 'H': | 915 case 'H': |
| 907 case 'm': | 916 case 'm': |
| 908 case 'M': | 917 case 'M': |
| 909 case 'S': | 918 case 'S': |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1083 struct tm *pTm; | 1092 struct tm *pTm; |
| 1084 struct tm sNow; | 1093 struct tm sNow; |
| 1085 char zBuf[20]; | 1094 char zBuf[20]; |
| 1086 | 1095 |
| 1087 UNUSED_PARAMETER(argc); | 1096 UNUSED_PARAMETER(argc); |
| 1088 UNUSED_PARAMETER(argv); | 1097 UNUSED_PARAMETER(argv); |
| 1089 | 1098 |
| 1090 iT = sqlite3StmtCurrentTime(context); | 1099 iT = sqlite3StmtCurrentTime(context); |
| 1091 if( iT<=0 ) return; | 1100 if( iT<=0 ) return; |
| 1092 t = iT/1000 - 10000*(sqlite3_int64)21086676; | 1101 t = iT/1000 - 10000*(sqlite3_int64)21086676; |
| 1093 #ifdef HAVE_GMTIME_R | 1102 #if HAVE_GMTIME_R |
| 1094 pTm = gmtime_r(&t, &sNow); | 1103 pTm = gmtime_r(&t, &sNow); |
| 1095 #else | 1104 #else |
| 1096 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | 1105 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 1097 pTm = gmtime(&t); | 1106 pTm = gmtime(&t); |
| 1098 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); | 1107 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); |
| 1099 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); | 1108 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 1100 #endif | 1109 #endif |
| 1101 if( pTm ){ | 1110 if( pTm ){ |
| 1102 strftime(zBuf, 20, zFormat, &sNow); | 1111 strftime(zBuf, 20, zFormat, &sNow); |
| 1103 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); | 1112 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 1104 } | 1113 } |
| 1105 } | 1114 } |
| 1106 #endif | 1115 #endif |
| 1107 | 1116 |
| 1108 /* | 1117 /* |
| 1109 ** This function registered all of the above C functions as SQL | 1118 ** This function registered all of the above C functions as SQL |
| 1110 ** functions. This should be the only routine in this file with | 1119 ** functions. This should be the only routine in this file with |
| 1111 ** external linkage. | 1120 ** external linkage. |
| 1112 */ | 1121 */ |
| 1113 void sqlite3RegisterDateTimeFunctions(void){ | 1122 void sqlite3RegisterDateTimeFunctions(void){ |
| 1114 static SQLITE_WSD FuncDef aDateTimeFuncs[] = { | 1123 static SQLITE_WSD FuncDef aDateTimeFuncs[] = { |
| 1115 #ifndef SQLITE_OMIT_DATETIME_FUNCS | 1124 #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 1116 FUNCTION(julianday, -1, 0, 0, juliandayFunc ), | 1125 DFUNCTION(julianday, -1, 0, 0, juliandayFunc ), |
| 1117 FUNCTION(date, -1, 0, 0, dateFunc ), | 1126 DFUNCTION(date, -1, 0, 0, dateFunc ), |
| 1118 FUNCTION(time, -1, 0, 0, timeFunc ), | 1127 DFUNCTION(time, -1, 0, 0, timeFunc ), |
| 1119 FUNCTION(datetime, -1, 0, 0, datetimeFunc ), | 1128 DFUNCTION(datetime, -1, 0, 0, datetimeFunc ), |
| 1120 FUNCTION(strftime, -1, 0, 0, strftimeFunc ), | 1129 DFUNCTION(strftime, -1, 0, 0, strftimeFunc ), |
| 1121 FUNCTION(current_time, 0, 0, 0, ctimeFunc ), | 1130 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), |
| 1122 FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), | 1131 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), |
| 1123 FUNCTION(current_date, 0, 0, 0, cdateFunc ), | 1132 DFUNCTION(current_date, 0, 0, 0, cdateFunc ), |
| 1124 #else | 1133 #else |
| 1125 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), | 1134 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc), |
| 1126 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), | 1135 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), |
| 1127 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), | 1136 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), |
| 1128 #endif | 1137 #endif |
| 1129 }; | 1138 }; |
| 1130 int i; | 1139 int i; |
| 1131 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); | 1140 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
| 1132 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); | 1141 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); |
| 1133 | 1142 |
| 1134 for(i=0; i<ArraySize(aDateTimeFuncs); i++){ | 1143 for(i=0; i<ArraySize(aDateTimeFuncs); i++){ |
| 1135 sqlite3FuncDefInsert(pHash, &aFunc[i]); | 1144 sqlite3FuncDefInsert(pHash, &aFunc[i]); |
| 1136 } | 1145 } |
| 1137 } | 1146 } |
| OLD | NEW |