ds@1952: /* ds@1952: ** 2003 October 31 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains the C functions that implement date and time ds@1952: ** functions for SQLite. ds@1952: ** ds@1952: ** There is only one exported symbol in this file - the function ds@1952: ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ds@1952: ** All other code has file scope. ds@1952: ** ds@1952: ** $Id$ ds@1952: ** ds@1952: ** SQLite processes all times and dates as Julian Day numbers. The ds@1952: ** dates and times are stored as the number of days since noon ds@1952: ** in Greenwich on November 24, 4714 B.C. according to the Gregorian ds@1952: ** calendar system. ds@1952: ** ds@1952: ** 1970-01-01 00:00:00 is JD 2440587.5 ds@1952: ** 2000-01-01 00:00:00 is JD 2451544.5 ds@1952: ** ds@1952: ** This implemention requires years to be expressed as a 4-digit number ds@1952: ** which means that only dates between 0000-01-01 and 9999-12-31 can ds@1952: ** be represented, even though julian day numbers allow a much wider ds@1952: ** range of dates. ds@1952: ** ds@1952: ** The Gregorian calendar system is used for all dates and times, ds@1952: ** even those that predate the Gregorian calendar. Historians usually ds@1952: ** use the Julian calendar for dates prior to 1582-10-15 and for some ds@1952: ** dates afterwards, depending on locale. Beware of this difference. ds@1952: ** ds@1952: ** The conversion algorithms are implemented based on descriptions ds@1952: ** in the following text: ds@1952: ** ds@1952: ** Jean Meeus ds@1952: ** Astronomical Algorithms, 2nd Edition, 1998 ds@1952: ** ISBM 0-943396-61-1 ds@1952: ** Willmann-Bell, Inc ds@1952: ** Richmond, Virginia (USA) ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: #include ds@1952: #include ds@1952: #include ds@1952: ds@1952: #ifndef SQLITE_OMIT_DATETIME_FUNCS ds@1952: ds@1952: /* ds@1952: ** A structure for holding a single date and time. ds@1952: */ ds@1952: typedef struct DateTime DateTime; ds@1952: struct DateTime { ds@1952: double rJD; /* The julian day number */ ds@1952: int Y, M, D; /* Year, month, and day */ ds@1952: int h, m; /* Hour and minutes */ ds@1952: int tz; /* Timezone offset in minutes */ ds@1952: double s; /* Seconds */ ds@1952: char validYMD; /* True if Y,M,D are valid */ ds@1952: char validHMS; /* True if h,m,s are valid */ ds@1952: char validJD; /* True if rJD is valid */ ds@1952: char validTZ; /* True if tz is valid */ ds@1952: }; ds@1952: ds@1952: ds@1952: /* ds@1952: ** Convert zDate into one or more integers. Additional arguments ds@1952: ** come in groups of 5 as follows: ds@1952: ** ds@1952: ** N number of digits in the integer ds@1952: ** min minimum allowed value of the integer ds@1952: ** max maximum allowed value of the integer ds@1952: ** nextC first character after the integer ds@1952: ** pVal where to write the integers value. ds@1952: ** ds@1952: ** Conversions continue until one with nextC==0 is encountered. ds@1952: ** The function returns the number of successful conversions. ds@1952: */ ds@1952: static int getDigits(const char *zDate, ...){ ds@1952: va_list ap; ds@1952: int val; ds@1952: int N; ds@1952: int min; ds@1952: int max; ds@1952: int nextC; ds@1952: int *pVal; ds@1952: int cnt = 0; ds@1952: va_start(ap, zDate); ds@1952: do{ ds@1952: N = va_arg(ap, int); ds@1952: min = va_arg(ap, int); ds@1952: max = va_arg(ap, int); ds@1952: nextC = va_arg(ap, int); ds@1952: pVal = va_arg(ap, int*); ds@1952: val = 0; ds@1952: while( N-- ){ ds@1952: if( !isdigit(*(u8*)zDate) ){ ds@1952: goto end_getDigits; ds@1952: } ds@1952: val = val*10 + *zDate - '0'; ds@1952: zDate++; ds@1952: } ds@1952: if( valmax || (nextC!=0 && nextC!=*zDate) ){ ds@1952: goto end_getDigits; ds@1952: } ds@1952: *pVal = val; ds@1952: zDate++; ds@1952: cnt++; ds@1952: }while( nextC ); ds@1952: end_getDigits: ds@1952: va_end(ap); ds@1952: return cnt; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Read text from z[] and convert into a floating point number. Return ds@1952: ** the number of digits converted. ds@1952: */ ds@1952: #define getValue sqlite3AtoF ds@1952: ds@1952: /* ds@1952: ** Parse a timezone extension on the end of a date-time. ds@1952: ** The extension is of the form: ds@1952: ** ds@1952: ** (+/-)HH:MM ds@1952: ** ds@1952: ** If the parse is successful, write the number of minutes ds@1952: ** of change in *pnMin and return 0. If a parser error occurs, ds@1952: ** return 0. ds@1952: ** ds@1952: ** A missing specifier is not considered an error. ds@1952: */ ds@1952: static int parseTimezone(const char *zDate, DateTime *p){ ds@1952: int sgn = 0; ds@1952: int nHr, nMn; ds@1952: while( isspace(*(u8*)zDate) ){ zDate++; } ds@1952: p->tz = 0; ds@1952: if( *zDate=='-' ){ ds@1952: sgn = -1; ds@1952: }else if( *zDate=='+' ){ ds@1952: sgn = +1; ds@1952: }else{ ds@1952: return *zDate!=0; ds@1952: } ds@1952: zDate++; ds@1952: if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ ds@1952: return 1; ds@1952: } ds@1952: zDate += 5; ds@1952: p->tz = sgn*(nMn + nHr*60); ds@1952: while( isspace(*(u8*)zDate) ){ zDate++; } ds@1952: return *zDate!=0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. ds@1952: ** The HH, MM, and SS must each be exactly 2 digits. The ds@1952: ** fractional seconds FFFF can be one or more digits. ds@1952: ** ds@1952: ** Return 1 if there is a parsing error and 0 on success. ds@1952: */ ds@1952: static int parseHhMmSs(const char *zDate, DateTime *p){ ds@1952: int h, m, s; ds@1952: double ms = 0.0; ds@1952: if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ ds@1952: return 1; ds@1952: } ds@1952: zDate += 5; ds@1952: if( *zDate==':' ){ ds@1952: zDate++; ds@1952: if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ ds@1952: return 1; ds@1952: } ds@1952: zDate += 2; ds@1952: if( *zDate=='.' && isdigit((u8)zDate[1]) ){ ds@1952: double rScale = 1.0; ds@1952: zDate++; ds@1952: while( isdigit(*(u8*)zDate) ){ ds@1952: ms = ms*10.0 + *zDate - '0'; ds@1952: rScale *= 10.0; ds@1952: zDate++; ds@1952: } ds@1952: ms /= rScale; ds@1952: } ds@1952: }else{ ds@1952: s = 0; ds@1952: } ds@1952: p->validJD = 0; ds@1952: p->validHMS = 1; ds@1952: p->h = h; ds@1952: p->m = m; ds@1952: p->s = s + ms; ds@1952: if( parseTimezone(zDate, p) ) return 1; ds@1952: p->validTZ = p->tz!=0; ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume ds@1952: ** that the YYYY-MM-DD is according to the Gregorian calendar. ds@1952: ** ds@1952: ** Reference: Meeus page 61 ds@1952: */ ds@1952: static void computeJD(DateTime *p){ ds@1952: int Y, M, D, A, B, X1, X2; ds@1952: ds@1952: if( p->validJD ) return; ds@1952: if( p->validYMD ){ ds@1952: Y = p->Y; ds@1952: M = p->M; ds@1952: D = p->D; ds@1952: }else{ ds@1952: Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ ds@1952: M = 1; ds@1952: D = 1; ds@1952: } ds@1952: if( M<=2 ){ ds@1952: Y--; ds@1952: M += 12; ds@1952: } ds@1952: A = Y/100; ds@1952: B = 2 - A + (A/4); ds@1952: X1 = 365.25*(Y+4716); ds@1952: X2 = 30.6001*(M+1); ds@1952: p->rJD = X1 + X2 + D + B - 1524.5; ds@1952: p->validJD = 1; ds@1952: if( p->validHMS ){ ds@1952: p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0; ds@1952: if( p->validTZ ){ ds@1952: p->rJD -= p->tz*60/86400.0; ds@1952: p->validYMD = 0; ds@1952: p->validHMS = 0; ds@1952: p->validTZ = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Parse dates of the form ds@1952: ** ds@1952: ** YYYY-MM-DD HH:MM:SS.FFF ds@1952: ** YYYY-MM-DD HH:MM:SS ds@1952: ** YYYY-MM-DD HH:MM ds@1952: ** YYYY-MM-DD ds@1952: ** ds@1952: ** Write the result into the DateTime structure and return 0 ds@1952: ** on success and 1 if the input string is not a well-formed ds@1952: ** date. ds@1952: */ ds@1952: static int parseYyyyMmDd(const char *zDate, DateTime *p){ ds@1952: int Y, M, D, neg; ds@1952: ds@1952: if( zDate[0]=='-' ){ ds@1952: zDate++; ds@1952: neg = 1; ds@1952: }else{ ds@1952: neg = 0; ds@1952: } ds@1952: if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ ds@1952: return 1; ds@1952: } ds@1952: zDate += 10; ds@1952: while( isspace(*(u8*)zDate) || 'T'==*(u8*)zDate ){ zDate++; } ds@1952: if( parseHhMmSs(zDate, p)==0 ){ ds@1952: /* We got the time */ ds@1952: }else if( *zDate==0 ){ ds@1952: p->validHMS = 0; ds@1952: }else{ ds@1952: return 1; ds@1952: } ds@1952: p->validJD = 0; ds@1952: p->validYMD = 1; ds@1952: p->Y = neg ? -Y : Y; ds@1952: p->M = M; ds@1952: p->D = D; ds@1952: if( p->validTZ ){ ds@1952: computeJD(p); ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Attempt to parse the given string into a Julian Day Number. Return ds@1952: ** the number of errors. ds@1952: ** ds@1952: ** The following are acceptable forms for the input string: ds@1952: ** ds@1952: ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM ds@1952: ** DDDD.DD ds@1952: ** now ds@1952: ** ds@1952: ** In the first form, the +/-HH:MM is always optional. The fractional ds@1952: ** seconds extension (the ".FFF") is optional. The seconds portion ds@1952: ** (":SS.FFF") is option. The year and date can be omitted as long ds@1952: ** as there is a time string. The time string can be omitted as long ds@1952: ** as there is a year and date. ds@1952: */ ds@1952: static int parseDateOrTime( ds@1952: sqlite3_context *context, ds@1952: const char *zDate, ds@1952: DateTime *p ds@1952: ){ ds@1952: memset(p, 0, sizeof(*p)); ds@1952: if( parseYyyyMmDd(zDate,p)==0 ){ ds@1952: return 0; ds@1952: }else if( parseHhMmSs(zDate, p)==0 ){ ds@1952: return 0; ds@1952: }else if( sqlite3StrICmp(zDate,"now")==0){ ds@1952: double r; ds@1952: sqlite3OsCurrentTime((sqlite3_vfs *)sqlite3_user_data(context), &r); ds@1952: p->rJD = r; ds@1952: p->validJD = 1; ds@1952: return 0; ds@1952: }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){ ds@1952: getValue(zDate, &p->rJD); ds@1952: p->validJD = 1; ds@1952: return 0; ds@1952: } ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compute the Year, Month, and Day from the julian day number. ds@1952: */ ds@1952: static void computeYMD(DateTime *p){ ds@1952: int Z, A, B, C, D, E, X1; ds@1952: if( p->validYMD ) return; ds@1952: if( !p->validJD ){ ds@1952: p->Y = 2000; ds@1952: p->M = 1; ds@1952: p->D = 1; ds@1952: }else{ ds@1952: Z = p->rJD + 0.5; ds@1952: A = (Z - 1867216.25)/36524.25; ds@1952: A = Z + 1 + A - (A/4); ds@1952: B = A + 1524; ds@1952: C = (B - 122.1)/365.25; ds@1952: D = 365.25*C; ds@1952: E = (B-D)/30.6001; ds@1952: X1 = 30.6001*E; ds@1952: p->D = B - D - X1; ds@1952: p->M = E<14 ? E-1 : E-13; ds@1952: p->Y = p->M>2 ? C - 4716 : C - 4715; ds@1952: } ds@1952: p->validYMD = 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compute the Hour, Minute, and Seconds from the julian day number. ds@1952: */ ds@1952: static void computeHMS(DateTime *p){ ds@1952: int Z, s; ds@1952: if( p->validHMS ) return; ds@1952: computeJD(p); ds@1952: Z = p->rJD + 0.5; ds@1952: s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5; ds@1952: p->s = 0.001*s; ds@1952: s = p->s; ds@1952: p->s -= s; ds@1952: p->h = s/3600; ds@1952: s -= p->h*3600; ds@1952: p->m = s/60; ds@1952: p->s += s - p->m*60; ds@1952: p->validHMS = 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compute both YMD and HMS ds@1952: */ ds@1952: static void computeYMD_HMS(DateTime *p){ ds@1952: computeYMD(p); ds@1952: computeHMS(p); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Clear the YMD and HMS and the TZ ds@1952: */ ds@1952: static void clearYMD_HMS_TZ(DateTime *p){ ds@1952: p->validYMD = 0; ds@1952: p->validHMS = 0; ds@1952: p->validTZ = 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compute the difference (in days) between localtime and UTC (a.k.a. GMT) ds@1952: ** for the time value p where p is in UTC. ds@1952: */ ds@1952: static double localtimeOffset(DateTime *p){ ds@1952: DateTime x, y; ds@1952: time_t t; ds@1952: x = *p; ds@1952: computeYMD_HMS(&x); ds@1952: if( x.Y<1971 || x.Y>=2038 ){ ds@1952: x.Y = 2000; ds@1952: x.M = 1; ds@1952: x.D = 1; ds@1952: x.h = 0; ds@1952: x.m = 0; ds@1952: x.s = 0.0; ds@1952: } else { ds@1952: int s = x.s + 0.5; ds@1952: x.s = s; ds@1952: } ds@1952: x.tz = 0; ds@1952: x.validJD = 0; ds@1952: computeJD(&x); ds@1952: t = (x.rJD-2440587.5)*86400.0 + 0.5; ds@1952: #ifdef HAVE_LOCALTIME_R ds@1952: { ds@1952: struct tm sLocal; ds@1952: localtime_r(&t, &sLocal); ds@1952: y.Y = sLocal.tm_year + 1900; ds@1952: y.M = sLocal.tm_mon + 1; ds@1952: y.D = sLocal.tm_mday; ds@1952: y.h = sLocal.tm_hour; ds@1952: y.m = sLocal.tm_min; ds@1952: y.s = sLocal.tm_sec; ds@1952: } ds@1952: #else ds@1952: { ds@1952: struct tm *pTm; ds@1952: sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); ds@1952: pTm = localtime(&t); ds@1952: y.Y = pTm->tm_year + 1900; ds@1952: y.M = pTm->tm_mon + 1; ds@1952: y.D = pTm->tm_mday; ds@1952: y.h = pTm->tm_hour; ds@1952: y.m = pTm->tm_min; ds@1952: y.s = pTm->tm_sec; ds@1952: sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); ds@1952: } ds@1952: #endif ds@1952: y.validYMD = 1; ds@1952: y.validHMS = 1; ds@1952: y.validJD = 0; ds@1952: y.validTZ = 0; ds@1952: computeJD(&y); ds@1952: return y.rJD - x.rJD; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Process a modifier to a date-time stamp. The modifiers are ds@1952: ** as follows: ds@1952: ** ds@1952: ** NNN days ds@1952: ** NNN hours ds@1952: ** NNN minutes ds@1952: ** NNN.NNNN seconds ds@1952: ** NNN months ds@1952: ** NNN years ds@1952: ** start of month ds@1952: ** start of year ds@1952: ** start of week ds@1952: ** start of day ds@1952: ** weekday N ds@1952: ** unixepoch ds@1952: ** localtime ds@1952: ** utc ds@1952: ** ds@1952: ** Return 0 on success and 1 if there is any kind of error. ds@1952: */ ds@1952: static int parseModifier(const char *zMod, DateTime *p){ ds@1952: int rc = 1; ds@1952: int n; ds@1952: double r; ds@1952: char *z, zBuf[30]; ds@1952: z = zBuf; ds@1952: for(n=0; nrJD += localtimeOffset(p); ds@1952: clearYMD_HMS_TZ(p); ds@1952: rc = 0; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 'u': { ds@1952: /* ds@1952: ** unixepoch ds@1952: ** ds@1952: ** Treat the current value of p->rJD as the number of ds@1952: ** seconds since 1970. Convert to a real julian day number. ds@1952: */ ds@1952: if( strcmp(z, "unixepoch")==0 && p->validJD ){ ds@1952: p->rJD = p->rJD/86400.0 + 2440587.5; ds@1952: clearYMD_HMS_TZ(p); ds@1952: rc = 0; ds@1952: }else if( strcmp(z, "utc")==0 ){ ds@1952: double c1; ds@1952: computeJD(p); ds@1952: c1 = localtimeOffset(p); ds@1952: p->rJD -= c1; ds@1952: clearYMD_HMS_TZ(p); ds@1952: p->rJD += c1 - localtimeOffset(p); ds@1952: rc = 0; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 'w': { ds@1952: /* ds@1952: ** weekday N ds@1952: ** ds@1952: ** Move the date to the same time on the next occurrence of ds@1952: ** weekday N where 0==Sunday, 1==Monday, and so forth. If the ds@1952: ** date is already on the appropriate weekday, this is a no-op. ds@1952: */ ds@1952: if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0 ds@1952: && (n=r)==r && n>=0 && r<7 ){ ds@1952: int Z; ds@1952: computeYMD_HMS(p); ds@1952: p->validTZ = 0; ds@1952: p->validJD = 0; ds@1952: computeJD(p); ds@1952: Z = p->rJD + 1.5; ds@1952: Z %= 7; ds@1952: if( Z>n ) Z -= 7; ds@1952: p->rJD += n - Z; ds@1952: clearYMD_HMS_TZ(p); ds@1952: rc = 0; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 's': { ds@1952: /* ds@1952: ** start of TTTTT ds@1952: ** ds@1952: ** Move the date backwards to the beginning of the current day, ds@1952: ** or month or year. ds@1952: */ ds@1952: if( strncmp(z, "start of ", 9)!=0 ) break; ds@1952: z += 9; ds@1952: computeYMD(p); ds@1952: p->validHMS = 1; ds@1952: p->h = p->m = 0; ds@1952: p->s = 0.0; ds@1952: p->validTZ = 0; ds@1952: p->validJD = 0; ds@1952: if( strcmp(z,"month")==0 ){ ds@1952: p->D = 1; ds@1952: rc = 0; ds@1952: }else if( strcmp(z,"year")==0 ){ ds@1952: computeYMD(p); ds@1952: p->M = 1; ds@1952: p->D = 1; ds@1952: rc = 0; ds@1952: }else if( strcmp(z,"day")==0 ){ ds@1952: rc = 0; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case '+': ds@1952: case '-': ds@1952: case '0': ds@1952: case '1': ds@1952: case '2': ds@1952: case '3': ds@1952: case '4': ds@1952: case '5': ds@1952: case '6': ds@1952: case '7': ds@1952: case '8': ds@1952: case '9': { ds@1952: n = getValue(z, &r); ds@1952: assert( n>=1 ); ds@1952: if( z[n]==':' ){ ds@1952: /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the ds@1952: ** specified number of hours, minutes, seconds, and fractional seconds ds@1952: ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be ds@1952: ** omitted. ds@1952: */ ds@1952: const char *z2 = z; ds@1952: DateTime tx; ds@1952: int day; ds@1952: if( !isdigit(*(u8*)z2) ) z2++; ds@1952: memset(&tx, 0, sizeof(tx)); ds@1952: if( parseHhMmSs(z2, &tx) ) break; ds@1952: computeJD(&tx); ds@1952: tx.rJD -= 0.5; ds@1952: day = (int)tx.rJD; ds@1952: tx.rJD -= day; ds@1952: if( z[0]=='-' ) tx.rJD = -tx.rJD; ds@1952: computeJD(p); ds@1952: clearYMD_HMS_TZ(p); ds@1952: p->rJD += tx.rJD; ds@1952: rc = 0; ds@1952: break; ds@1952: } ds@1952: z += n; ds@1952: while( isspace(*(u8*)z) ) z++; ds@1952: n = strlen(z); ds@1952: if( n>10 || n<3 ) break; ds@1952: if( z[n-1]=='s' ){ z[n-1] = 0; n--; } ds@1952: computeJD(p); ds@1952: rc = 0; ds@1952: if( n==3 && strcmp(z,"day")==0 ){ ds@1952: p->rJD += r; ds@1952: }else if( n==4 && strcmp(z,"hour")==0 ){ ds@1952: p->rJD += r/24.0; ds@1952: }else if( n==6 && strcmp(z,"minute")==0 ){ ds@1952: p->rJD += r/(24.0*60.0); ds@1952: }else if( n==6 && strcmp(z,"second")==0 ){ ds@1952: p->rJD += r/(24.0*60.0*60.0); ds@1952: }else if( n==5 && strcmp(z,"month")==0 ){ ds@1952: int x, y; ds@1952: computeYMD_HMS(p); ds@1952: p->M += r; ds@1952: x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; ds@1952: p->Y += x; ds@1952: p->M -= x*12; ds@1952: p->validJD = 0; ds@1952: computeJD(p); ds@1952: y = r; ds@1952: if( y!=r ){ ds@1952: p->rJD += (r - y)*30.0; ds@1952: } ds@1952: }else if( n==4 && strcmp(z,"year")==0 ){ ds@1952: computeYMD_HMS(p); ds@1952: p->Y += r; ds@1952: p->validJD = 0; ds@1952: computeJD(p); ds@1952: }else{ ds@1952: rc = 1; ds@1952: } ds@1952: clearYMD_HMS_TZ(p); ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: break; ds@1952: } ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Process time function arguments. argv[0] is a date-time stamp. ds@1952: ** argv[1] and following are modifiers. Parse them all and write ds@1952: ** the resulting time into the DateTime structure p. Return 0 ds@1952: ** on success and 1 if there are any errors. ds@1952: */ ds@1952: static int isDate( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv, ds@1952: DateTime *p ds@1952: ){ ds@1952: int i; ds@1952: const unsigned char *z; ds@1952: if( argc==0 ) return 1; ds@1952: z = sqlite3_value_text(argv[0]); ds@1952: if( !z || parseDateOrTime(context, (char*)z, p) ){ ds@1952: return 1; ds@1952: } ds@1952: for(i=1; iSQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: return; ds@1952: }else{ ds@1952: z = sqlite3_malloc( n ); ds@1952: if( z==0 ) return; ds@1952: } ds@1952: computeJD(&x); ds@1952: computeYMD_HMS(&x); ds@1952: for(i=j=0; zFmt[i]; i++){ ds@1952: if( zFmt[i]!='%' ){ ds@1952: z[j++] = zFmt[i]; ds@1952: }else{ ds@1952: i++; ds@1952: switch( zFmt[i] ){ ds@1952: case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break; ds@1952: case 'f': { ds@1952: double s = x.s; ds@1952: if( s>59.999 ) s = 59.999; ds@1952: sqlite3_snprintf(7, &z[j],"%06.3f", s); ds@1952: j += strlen(&z[j]); ds@1952: break; ds@1952: } ds@1952: case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break; ds@1952: case 'W': /* Fall thru */ ds@1952: case 'j': { ds@1952: int nDay; /* Number of days since 1st day of year */ ds@1952: DateTime y = x; ds@1952: y.validJD = 0; ds@1952: y.M = 1; ds@1952: y.D = 1; ds@1952: computeJD(&y); ds@1952: nDay = x.rJD - y.rJD + 0.5; ds@1952: if( zFmt[i]=='W' ){ ds@1952: int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ ds@1952: wd = ((int)(x.rJD+0.5)) % 7; ds@1952: sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); ds@1952: j += 2; ds@1952: }else{ ds@1952: sqlite3_snprintf(4, &z[j],"%03d",nDay+1); ds@1952: j += 3; ds@1952: } ds@1952: break; ds@1952: } ds@1952: case 'J': { ds@1952: sqlite3_snprintf(20, &z[j],"%.16g",x.rJD); ds@1952: j+=strlen(&z[j]); ds@1952: break; ds@1952: } ds@1952: case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break; ds@1952: case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break; ds@1952: case 's': { ds@1952: sqlite3_snprintf(30,&z[j],"%d", ds@1952: (int)((x.rJD-2440587.5)*86400.0 + 0.5)); ds@1952: j += strlen(&z[j]); ds@1952: break; ds@1952: } ds@1952: case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break; ds@1952: case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break; ds@1952: case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break; ds@1952: case '%': z[j++] = '%'; break; ds@1952: } ds@1952: } ds@1952: } ds@1952: z[j] = 0; ds@1952: sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); ds@1952: if( z!=zBuf ){ ds@1952: sqlite3_free(z); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** current_time() ds@1952: ** ds@1952: ** This function returns the same value as time('now'). ds@1952: */ ds@1952: static void ctimeFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3_value *pVal = sqlite3ValueNew(0); ds@1952: if( pVal ){ ds@1952: sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); ds@1952: timeFunc(context, 1, &pVal); ds@1952: sqlite3ValueFree(pVal); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** current_date() ds@1952: ** ds@1952: ** This function returns the same value as date('now'). ds@1952: */ ds@1952: static void cdateFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3_value *pVal = sqlite3ValueNew(0); ds@1952: if( pVal ){ ds@1952: sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); ds@1952: dateFunc(context, 1, &pVal); ds@1952: sqlite3ValueFree(pVal); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** current_timestamp() ds@1952: ** ds@1952: ** This function returns the same value as datetime('now'). ds@1952: */ ds@1952: static void ctimestampFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3_value *pVal = sqlite3ValueNew(0); ds@1952: if( pVal ){ ds@1952: sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); ds@1952: datetimeFunc(context, 1, &pVal); ds@1952: sqlite3ValueFree(pVal); ds@1952: } ds@1952: } ds@1952: #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ ds@1952: ds@1952: #ifdef SQLITE_OMIT_DATETIME_FUNCS ds@1952: /* ds@1952: ** If the library is compiled to omit the full-scale date and time ds@1952: ** handling (to get a smaller binary), the following minimal version ds@1952: ** of the functions current_time(), current_date() and current_timestamp() ds@1952: ** are included instead. This is to support column declarations that ds@1952: ** include "DEFAULT CURRENT_TIME" etc. ds@1952: ** ds@1952: ** This function uses the C-library functions time(), gmtime() ds@1952: ** and strftime(). The format string to pass to strftime() is supplied ds@1952: ** as the user-data for the function. ds@1952: */ ds@1952: static void currentTimeFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: time_t t; ds@1952: char *zFormat = (char *)sqlite3_user_data(context); ds@1952: char zBuf[20]; ds@1952: ds@1952: time(&t); ds@1952: #ifdef SQLITE_TEST ds@1952: { ds@1952: extern int sqlite3_current_time; /* See os_XXX.c */ ds@1952: if( sqlite3_current_time ){ ds@1952: t = sqlite3_current_time; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifdef HAVE_GMTIME_R ds@1952: { ds@1952: struct tm sNow; ds@1952: gmtime_r(&t, &sNow); ds@1952: strftime(zBuf, 20, zFormat, &sNow); ds@1952: } ds@1952: #else ds@1952: { ds@1952: struct tm *pTm; ds@1952: sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); ds@1952: pTm = gmtime(&t); ds@1952: strftime(zBuf, 20, zFormat, pTm); ds@1952: sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); ds@1952: } ds@1952: #endif ds@1952: ds@1952: sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** This function registered all of the above C functions as SQL ds@1952: ** functions. This should be the only routine in this file with ds@1952: ** external linkage. ds@1952: */ ds@1952: void sqlite3RegisterDateTimeFunctions(sqlite3 *db){ ds@1952: #ifndef SQLITE_OMIT_DATETIME_FUNCS ds@1952: static const struct { ds@1952: char *zName; ds@1952: int nArg; ds@1952: void (*xFunc)(sqlite3_context*,int,sqlite3_value**); ds@1952: } aFuncs[] = { ds@1952: { "julianday", -1, juliandayFunc }, ds@1952: { "date", -1, dateFunc }, ds@1952: { "time", -1, timeFunc }, ds@1952: { "datetime", -1, datetimeFunc }, ds@1952: { "strftime", -1, strftimeFunc }, ds@1952: { "current_time", 0, ctimeFunc }, ds@1952: { "current_timestamp", 0, ctimestampFunc }, ds@1952: { "current_date", 0, cdateFunc }, ds@1952: }; ds@1952: int i; ds@1952: ds@1952: for(i=0; ipVfs), aFuncs[i].xFunc, 0, 0); ds@1952: } ds@1952: #else ds@1952: static const struct { ds@1952: char *zName; ds@1952: char *zFormat; ds@1952: } aFuncs[] = { ds@1952: { "current_time", "%H:%M:%S" }, ds@1952: { "current_date", "%Y-%m-%d" }, ds@1952: { "current_timestamp", "%Y-%m-%d %H:%M:%S" } ds@1952: }; ds@1952: int i; ds@1952: ds@1952: for(i=0; i