blob: 9831541808482d310445ba72511018f6dec74fd6 [file] [log] [blame]
drh7014aff2003-11-01 01:53:53 +00001/*
2** 2003 October 31
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains the C functions that implement date and time
13** functions for SQLite.
14**
15** There is only one exported symbol in this file - the function
danielk19774adee202004-05-08 08:23:19 +000016** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
drh7014aff2003-11-01 01:53:53 +000017** All other code has file scope.
18**
drh66147c92008-06-12 12:51:37 +000019** $Id: date.c,v 1.81 2008/06/12 12:51:37 drh Exp $
drh7014aff2003-11-01 01:53:53 +000020**
21** SQLite processes all times and dates as Julian Day numbers. The
22** dates and times are stored as the number of days since noon
23** in Greenwich on November 24, 4714 B.C. according to the Gregorian
drh7f986a62006-09-25 18:05:04 +000024** calendar system.
drh7014aff2003-11-01 01:53:53 +000025**
26** 1970-01-01 00:00:00 is JD 2440587.5
27** 2000-01-01 00:00:00 is JD 2451544.5
28**
29** This implemention requires years to be expressed as a 4-digit number
30** which means that only dates between 0000-01-01 and 9999-12-31 can
31** be represented, even though julian day numbers allow a much wider
32** range of dates.
33**
34** The Gregorian calendar system is used for all dates and times,
35** even those that predate the Gregorian calendar. Historians usually
36** use the Julian calendar for dates prior to 1582-10-15 and for some
37** dates afterwards, depending on locale. Beware of this difference.
38**
39** The conversion algorithms are implemented based on descriptions
40** in the following text:
41**
42** Jean Meeus
43** Astronomical Algorithms, 2nd Edition, 1998
44** ISBM 0-943396-61-1
45** Willmann-Bell, Inc
46** Richmond, Virginia (USA)
47*/
dougcurrieae534182003-12-24 01:41:19 +000048#include "sqliteInt.h"
drh7014aff2003-11-01 01:53:53 +000049#include <ctype.h>
50#include <stdlib.h>
51#include <assert.h>
drh7091cb02003-12-23 16:22:18 +000052#include <time.h>
drh7014aff2003-11-01 01:53:53 +000053
drh4bc05852004-02-10 13:19:35 +000054#ifndef SQLITE_OMIT_DATETIME_FUNCS
55
drh7014aff2003-11-01 01:53:53 +000056/*
shaneb8109ad2008-05-27 19:49:21 +000057** On recent Windows platforms, the localtime_s() function is available
58** as part of the "Secure CRT". It is essentially equivalent to
59** localtime_r() available under most POSIX platforms, except that the
60** order of the parameters is reversed.
61**
62** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
63**
64** If the user has not indicated to use localtime_r() or localtime_s()
65** already, check for an MSVC build environment that provides
66** localtime_s().
67*/
68#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
69 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
70#define HAVE_LOCALTIME_S 1
71#endif
72
73/*
drh7014aff2003-11-01 01:53:53 +000074** A structure for holding a single date and time.
75*/
76typedef struct DateTime DateTime;
77struct DateTime {
78 double rJD; /* The julian day number */
79 int Y, M, D; /* Year, month, and day */
80 int h, m; /* Hour and minutes */
81 int tz; /* Timezone offset in minutes */
82 double s; /* Seconds */
83 char validYMD; /* True if Y,M,D are valid */
84 char validHMS; /* True if h,m,s are valid */
85 char validJD; /* True if rJD is valid */
86 char validTZ; /* True if tz is valid */
87};
88
89
90/*
drheb9a9e82004-02-22 17:49:32 +000091** Convert zDate into one or more integers. Additional arguments
92** come in groups of 5 as follows:
93**
94** N number of digits in the integer
95** min minimum allowed value of the integer
96** max maximum allowed value of the integer
97** nextC first character after the integer
98** pVal where to write the integers value.
99**
100** Conversions continue until one with nextC==0 is encountered.
101** The function returns the number of successful conversions.
drh7014aff2003-11-01 01:53:53 +0000102*/
drheb9a9e82004-02-22 17:49:32 +0000103static int getDigits(const char *zDate, ...){
104 va_list ap;
105 int val;
106 int N;
107 int min;
108 int max;
109 int nextC;
110 int *pVal;
111 int cnt = 0;
112 va_start(ap, zDate);
113 do{
114 N = va_arg(ap, int);
115 min = va_arg(ap, int);
116 max = va_arg(ap, int);
117 nextC = va_arg(ap, int);
118 pVal = va_arg(ap, int*);
119 val = 0;
120 while( N-- ){
drh4c755c02004-08-08 20:22:17 +0000121 if( !isdigit(*(u8*)zDate) ){
drh029b44b2006-01-15 00:13:15 +0000122 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000123 }
124 val = val*10 + *zDate - '0';
125 zDate++;
126 }
127 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000128 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000129 }
130 *pVal = val;
drh7014aff2003-11-01 01:53:53 +0000131 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000132 cnt++;
133 }while( nextC );
drh029b44b2006-01-15 00:13:15 +0000134end_getDigits:
drh15b9a152006-01-31 20:49:13 +0000135 va_end(ap);
drheb9a9e82004-02-22 17:49:32 +0000136 return cnt;
drh7014aff2003-11-01 01:53:53 +0000137}
138
139/*
140** Read text from z[] and convert into a floating point number. Return
141** the number of digits converted.
142*/
drh487e2622005-06-25 18:42:14 +0000143#define getValue sqlite3AtoF
drh7014aff2003-11-01 01:53:53 +0000144
145/*
146** Parse a timezone extension on the end of a date-time.
147** The extension is of the form:
148**
149** (+/-)HH:MM
150**
drh1cfdc902008-02-21 20:40:43 +0000151** Or the "zulu" notation:
152**
153** Z
154**
drh7014aff2003-11-01 01:53:53 +0000155** If the parse is successful, write the number of minutes
drh1cfdc902008-02-21 20:40:43 +0000156** of change in p->tz and return 0. If a parser error occurs,
157** return non-zero.
drh7014aff2003-11-01 01:53:53 +0000158**
159** A missing specifier is not considered an error.
160*/
161static int parseTimezone(const char *zDate, DateTime *p){
162 int sgn = 0;
163 int nHr, nMn;
drh1cfdc902008-02-21 20:40:43 +0000164 int c;
drh4c755c02004-08-08 20:22:17 +0000165 while( isspace(*(u8*)zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000166 p->tz = 0;
drh1cfdc902008-02-21 20:40:43 +0000167 c = *zDate;
168 if( c=='-' ){
drh7014aff2003-11-01 01:53:53 +0000169 sgn = -1;
drh1cfdc902008-02-21 20:40:43 +0000170 }else if( c=='+' ){
drh7014aff2003-11-01 01:53:53 +0000171 sgn = +1;
drh1cfdc902008-02-21 20:40:43 +0000172 }else if( c=='Z' || c=='z' ){
173 zDate++;
174 goto zulu_time;
drh7014aff2003-11-01 01:53:53 +0000175 }else{
drh1cfdc902008-02-21 20:40:43 +0000176 return c!=0;
drh7014aff2003-11-01 01:53:53 +0000177 }
178 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000179 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
180 return 1;
181 }
182 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000183 p->tz = sgn*(nMn + nHr*60);
drh1cfdc902008-02-21 20:40:43 +0000184zulu_time:
drh4c755c02004-08-08 20:22:17 +0000185 while( isspace(*(u8*)zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000186 return *zDate!=0;
187}
188
189/*
190** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
191** The HH, MM, and SS must each be exactly 2 digits. The
192** fractional seconds FFFF can be one or more digits.
193**
194** Return 1 if there is a parsing error and 0 on success.
195*/
196static int parseHhMmSs(const char *zDate, DateTime *p){
197 int h, m, s;
198 double ms = 0.0;
drheb9a9e82004-02-22 17:49:32 +0000199 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
200 return 1;
201 }
202 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000203 if( *zDate==':' ){
drheb9a9e82004-02-22 17:49:32 +0000204 zDate++;
205 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
206 return 1;
207 }
208 zDate += 2;
drh4c755c02004-08-08 20:22:17 +0000209 if( *zDate=='.' && isdigit((u8)zDate[1]) ){
drh7014aff2003-11-01 01:53:53 +0000210 double rScale = 1.0;
211 zDate++;
drh4c755c02004-08-08 20:22:17 +0000212 while( isdigit(*(u8*)zDate) ){
drh7014aff2003-11-01 01:53:53 +0000213 ms = ms*10.0 + *zDate - '0';
214 rScale *= 10.0;
215 zDate++;
216 }
217 ms /= rScale;
218 }
219 }else{
220 s = 0;
221 }
222 p->validJD = 0;
223 p->validHMS = 1;
224 p->h = h;
225 p->m = m;
226 p->s = s + ms;
227 if( parseTimezone(zDate, p) ) return 1;
228 p->validTZ = p->tz!=0;
229 return 0;
230}
231
232/*
233** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
234** that the YYYY-MM-DD is according to the Gregorian calendar.
235**
236** Reference: Meeus page 61
237*/
238static void computeJD(DateTime *p){
239 int Y, M, D, A, B, X1, X2;
240
241 if( p->validJD ) return;
242 if( p->validYMD ){
243 Y = p->Y;
244 M = p->M;
245 D = p->D;
246 }else{
drhba212562004-01-08 02:17:31 +0000247 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
drh7014aff2003-11-01 01:53:53 +0000248 M = 1;
249 D = 1;
250 }
251 if( M<=2 ){
252 Y--;
253 M += 12;
254 }
255 A = Y/100;
256 B = 2 - A + (A/4);
257 X1 = 365.25*(Y+4716);
258 X2 = 30.6001*(M+1);
259 p->rJD = X1 + X2 + D + B - 1524.5;
260 p->validJD = 1;
drh7014aff2003-11-01 01:53:53 +0000261 if( p->validHMS ){
262 p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0;
263 if( p->validTZ ){
drh57391032006-01-09 00:18:02 +0000264 p->rJD -= p->tz*60/86400.0;
drhf11c34d2006-09-08 12:27:36 +0000265 p->validYMD = 0;
drh7014aff2003-11-01 01:53:53 +0000266 p->validHMS = 0;
267 p->validTZ = 0;
268 }
269 }
270}
271
272/*
273** Parse dates of the form
274**
275** YYYY-MM-DD HH:MM:SS.FFF
276** YYYY-MM-DD HH:MM:SS
277** YYYY-MM-DD HH:MM
278** YYYY-MM-DD
279**
280** Write the result into the DateTime structure and return 0
281** on success and 1 if the input string is not a well-formed
282** date.
283*/
284static int parseYyyyMmDd(const char *zDate, DateTime *p){
drh8eb2cce2004-02-21 03:28:18 +0000285 int Y, M, D, neg;
drh7014aff2003-11-01 01:53:53 +0000286
drh8eb2cce2004-02-21 03:28:18 +0000287 if( zDate[0]=='-' ){
288 zDate++;
289 neg = 1;
290 }else{
291 neg = 0;
292 }
drheb9a9e82004-02-22 17:49:32 +0000293 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
294 return 1;
295 }
296 zDate += 10;
drh4cb29b42005-03-21 00:43:44 +0000297 while( isspace(*(u8*)zDate) || 'T'==*(u8*)zDate ){ zDate++; }
drheb9a9e82004-02-22 17:49:32 +0000298 if( parseHhMmSs(zDate, p)==0 ){
299 /* We got the time */
drh7014aff2003-11-01 01:53:53 +0000300 }else if( *zDate==0 ){
301 p->validHMS = 0;
302 }else{
303 return 1;
304 }
305 p->validJD = 0;
306 p->validYMD = 1;
drh8eb2cce2004-02-21 03:28:18 +0000307 p->Y = neg ? -Y : Y;
drh7014aff2003-11-01 01:53:53 +0000308 p->M = M;
309 p->D = D;
310 if( p->validTZ ){
311 computeJD(p);
312 }
313 return 0;
314}
315
316/*
317** Attempt to parse the given string into a Julian Day Number. Return
318** the number of errors.
319**
320** The following are acceptable forms for the input string:
321**
322** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
323** DDDD.DD
324** now
325**
326** In the first form, the +/-HH:MM is always optional. The fractional
327** seconds extension (the ".FFF") is optional. The seconds portion
328** (":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
330** as there is a year and date.
331*/
danielk1977fee2d252007-08-18 10:59:19 +0000332static int parseDateOrTime(
333 sqlite3_context *context,
334 const char *zDate,
335 DateTime *p
336){
drh7014aff2003-11-01 01:53:53 +0000337 memset(p, 0, sizeof(*p));
drh8eb2cce2004-02-21 03:28:18 +0000338 if( parseYyyyMmDd(zDate,p)==0 ){
drh7014aff2003-11-01 01:53:53 +0000339 return 0;
drh8eb2cce2004-02-21 03:28:18 +0000340 }else if( parseHhMmSs(zDate, p)==0 ){
341 return 0;
danielk19774adee202004-05-08 08:23:19 +0000342 }else if( sqlite3StrICmp(zDate,"now")==0){
drh7014aff2003-11-01 01:53:53 +0000343 double r;
drhfa4a4b92008-03-19 21:45:51 +0000344 sqlite3 *db = sqlite3_context_db_handle(context);
345 sqlite3OsCurrentTime(db->pVfs, &r);
drh018d1a42005-01-15 01:52:31 +0000346 p->rJD = r;
347 p->validJD = 1;
348 return 0;
danielk1977dc8453f2004-06-12 00:42:34 +0000349 }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
drh487e2622005-06-25 18:42:14 +0000350 getValue(zDate, &p->rJD);
drh7014aff2003-11-01 01:53:53 +0000351 p->validJD = 1;
352 return 0;
353 }
354 return 1;
355}
356
357/*
358** Compute the Year, Month, and Day from the julian day number.
359*/
360static void computeYMD(DateTime *p){
361 int Z, A, B, C, D, E, X1;
362 if( p->validYMD ) return;
drh33a9ad22004-02-29 00:40:32 +0000363 if( !p->validJD ){
364 p->Y = 2000;
365 p->M = 1;
366 p->D = 1;
367 }else{
368 Z = p->rJD + 0.5;
369 A = (Z - 1867216.25)/36524.25;
370 A = Z + 1 + A - (A/4);
371 B = A + 1524;
372 C = (B - 122.1)/365.25;
373 D = 365.25*C;
374 E = (B-D)/30.6001;
375 X1 = 30.6001*E;
376 p->D = B - D - X1;
377 p->M = E<14 ? E-1 : E-13;
378 p->Y = p->M>2 ? C - 4716 : C - 4715;
379 }
drh7014aff2003-11-01 01:53:53 +0000380 p->validYMD = 1;
381}
382
383/*
384** Compute the Hour, Minute, and Seconds from the julian day number.
385*/
386static void computeHMS(DateTime *p){
387 int Z, s;
388 if( p->validHMS ) return;
drhf11c34d2006-09-08 12:27:36 +0000389 computeJD(p);
drh7014aff2003-11-01 01:53:53 +0000390 Z = p->rJD + 0.5;
391 s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
392 p->s = 0.001*s;
393 s = p->s;
394 p->s -= s;
395 p->h = s/3600;
396 s -= p->h*3600;
397 p->m = s/60;
398 p->s += s - p->m*60;
399 p->validHMS = 1;
400}
401
402/*
drhba212562004-01-08 02:17:31 +0000403** Compute both YMD and HMS
404*/
405static void computeYMD_HMS(DateTime *p){
406 computeYMD(p);
407 computeHMS(p);
408}
409
410/*
411** Clear the YMD and HMS and the TZ
412*/
413static void clearYMD_HMS_TZ(DateTime *p){
414 p->validYMD = 0;
415 p->validHMS = 0;
416 p->validTZ = 0;
417}
418
drh66147c92008-06-12 12:51:37 +0000419#ifndef SQLITE_OMIT_LOCALTIME
drhba212562004-01-08 02:17:31 +0000420/*
drh7091cb02003-12-23 16:22:18 +0000421** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
422** for the time value p where p is in UTC.
423*/
424static double localtimeOffset(DateTime *p){
425 DateTime x, y;
426 time_t t;
drh7091cb02003-12-23 16:22:18 +0000427 x = *p;
drhba212562004-01-08 02:17:31 +0000428 computeYMD_HMS(&x);
drh7091cb02003-12-23 16:22:18 +0000429 if( x.Y<1971 || x.Y>=2038 ){
430 x.Y = 2000;
431 x.M = 1;
432 x.D = 1;
433 x.h = 0;
434 x.m = 0;
435 x.s = 0.0;
436 } else {
437 int s = x.s + 0.5;
438 x.s = s;
439 }
440 x.tz = 0;
441 x.validJD = 0;
442 computeJD(&x);
443 t = (x.rJD-2440587.5)*86400.0 + 0.5;
drh87595762006-09-08 12:49:43 +0000444#ifdef HAVE_LOCALTIME_R
445 {
446 struct tm sLocal;
447 localtime_r(&t, &sLocal);
448 y.Y = sLocal.tm_year + 1900;
449 y.M = sLocal.tm_mon + 1;
450 y.D = sLocal.tm_mday;
451 y.h = sLocal.tm_hour;
452 y.m = sLocal.tm_min;
453 y.s = sLocal.tm_sec;
454 }
shaneb8109ad2008-05-27 19:49:21 +0000455#elif defined(HAVE_LOCALTIME_S)
456 {
457 struct tm sLocal;
458 localtime_s(&sLocal, &t);
459 y.Y = sLocal.tm_year + 1900;
460 y.M = sLocal.tm_mon + 1;
461 y.D = sLocal.tm_mday;
462 y.h = sLocal.tm_hour;
463 y.m = sLocal.tm_min;
464 y.s = sLocal.tm_sec;
465 }
drh87595762006-09-08 12:49:43 +0000466#else
467 {
468 struct tm *pTm;
drh153c62c2007-08-24 03:51:33 +0000469 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000470 pTm = localtime(&t);
471 y.Y = pTm->tm_year + 1900;
472 y.M = pTm->tm_mon + 1;
473 y.D = pTm->tm_mday;
474 y.h = pTm->tm_hour;
475 y.m = pTm->tm_min;
476 y.s = pTm->tm_sec;
drh153c62c2007-08-24 03:51:33 +0000477 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000478 }
479#endif
drh7091cb02003-12-23 16:22:18 +0000480 y.validYMD = 1;
481 y.validHMS = 1;
482 y.validJD = 0;
483 y.validTZ = 0;
484 computeJD(&y);
drh7091cb02003-12-23 16:22:18 +0000485 return y.rJD - x.rJD;
486}
drh66147c92008-06-12 12:51:37 +0000487#endif /* SQLITE_OMIT_LOCALTIME */
drh7091cb02003-12-23 16:22:18 +0000488
489/*
drh7014aff2003-11-01 01:53:53 +0000490** Process a modifier to a date-time stamp. The modifiers are
491** as follows:
492**
493** NNN days
494** NNN hours
495** NNN minutes
496** NNN.NNNN seconds
497** NNN months
498** NNN years
499** start of month
500** start of year
501** start of week
502** start of day
503** weekday N
504** unixepoch
drh7091cb02003-12-23 16:22:18 +0000505** localtime
506** utc
drh7014aff2003-11-01 01:53:53 +0000507**
508** Return 0 on success and 1 if there is any kind of error.
509*/
510static int parseModifier(const char *zMod, DateTime *p){
511 int rc = 1;
512 int n;
513 double r;
drh4d5b8362004-01-17 01:16:21 +0000514 char *z, zBuf[30];
515 z = zBuf;
516 for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
drh7014aff2003-11-01 01:53:53 +0000517 z[n] = tolower(zMod[n]);
518 }
519 z[n] = 0;
520 switch( z[0] ){
drh66147c92008-06-12 12:51:37 +0000521#ifndef SQLITE_OMIT_LOCALTIME
drh7091cb02003-12-23 16:22:18 +0000522 case 'l': {
523 /* localtime
524 **
525 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
526 ** show local time.
527 */
528 if( strcmp(z, "localtime")==0 ){
529 computeJD(p);
530 p->rJD += localtimeOffset(p);
drhba212562004-01-08 02:17:31 +0000531 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000532 rc = 0;
533 }
534 break;
535 }
drh66147c92008-06-12 12:51:37 +0000536#endif
drh7014aff2003-11-01 01:53:53 +0000537 case 'u': {
538 /*
539 ** unixepoch
540 **
541 ** Treat the current value of p->rJD as the number of
542 ** seconds since 1970. Convert to a real julian day number.
543 */
544 if( strcmp(z, "unixepoch")==0 && p->validJD ){
545 p->rJD = p->rJD/86400.0 + 2440587.5;
drhba212562004-01-08 02:17:31 +0000546 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000547 rc = 0;
drh7091cb02003-12-23 16:22:18 +0000548 }else if( strcmp(z, "utc")==0 ){
549 double c1;
550 computeJD(p);
551 c1 = localtimeOffset(p);
552 p->rJD -= c1;
drhba212562004-01-08 02:17:31 +0000553 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000554 p->rJD += c1 - localtimeOffset(p);
drh7091cb02003-12-23 16:22:18 +0000555 rc = 0;
drh7014aff2003-11-01 01:53:53 +0000556 }
557 break;
558 }
559 case 'w': {
560 /*
561 ** weekday N
562 **
drh181fc992004-08-17 10:42:54 +0000563 ** Move the date to the same time on the next occurrence of
drh7014aff2003-11-01 01:53:53 +0000564 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
drhc5dd9fa2004-01-07 03:29:16 +0000565 ** date is already on the appropriate weekday, this is a no-op.
drh7014aff2003-11-01 01:53:53 +0000566 */
567 if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
568 && (n=r)==r && n>=0 && r<7 ){
569 int Z;
drhba212562004-01-08 02:17:31 +0000570 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000571 p->validTZ = 0;
572 p->validJD = 0;
573 computeJD(p);
574 Z = p->rJD + 1.5;
575 Z %= 7;
576 if( Z>n ) Z -= 7;
577 p->rJD += n - Z;
drhba212562004-01-08 02:17:31 +0000578 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000579 rc = 0;
580 }
581 break;
582 }
583 case 's': {
584 /*
585 ** start of TTTTT
586 **
587 ** Move the date backwards to the beginning of the current day,
588 ** or month or year.
589 */
590 if( strncmp(z, "start of ", 9)!=0 ) break;
drh4d5b8362004-01-17 01:16:21 +0000591 z += 9;
drh7014aff2003-11-01 01:53:53 +0000592 computeYMD(p);
593 p->validHMS = 1;
594 p->h = p->m = 0;
595 p->s = 0.0;
596 p->validTZ = 0;
597 p->validJD = 0;
drh4d5b8362004-01-17 01:16:21 +0000598 if( strcmp(z,"month")==0 ){
drh7014aff2003-11-01 01:53:53 +0000599 p->D = 1;
600 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000601 }else if( strcmp(z,"year")==0 ){
drh7014aff2003-11-01 01:53:53 +0000602 computeYMD(p);
603 p->M = 1;
604 p->D = 1;
605 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000606 }else if( strcmp(z,"day")==0 ){
drh7014aff2003-11-01 01:53:53 +0000607 rc = 0;
608 }
609 break;
610 }
611 case '+':
612 case '-':
613 case '0':
614 case '1':
615 case '2':
616 case '3':
617 case '4':
618 case '5':
619 case '6':
620 case '7':
621 case '8':
622 case '9': {
623 n = getValue(z, &r);
drh05f7c192007-04-06 02:32:33 +0000624 assert( n>=1 );
drh33a9ad22004-02-29 00:40:32 +0000625 if( z[n]==':' ){
626 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
627 ** specified number of hours, minutes, seconds, and fractional seconds
628 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
629 ** omitted.
630 */
631 const char *z2 = z;
632 DateTime tx;
633 int day;
drh4c755c02004-08-08 20:22:17 +0000634 if( !isdigit(*(u8*)z2) ) z2++;
drh33a9ad22004-02-29 00:40:32 +0000635 memset(&tx, 0, sizeof(tx));
636 if( parseHhMmSs(z2, &tx) ) break;
637 computeJD(&tx);
drh0d131ab2004-02-29 01:08:17 +0000638 tx.rJD -= 0.5;
drh33a9ad22004-02-29 00:40:32 +0000639 day = (int)tx.rJD;
drhb6829e92004-02-29 00:50:33 +0000640 tx.rJD -= day;
drhb6829e92004-02-29 00:50:33 +0000641 if( z[0]=='-' ) tx.rJD = -tx.rJD;
drh0d131ab2004-02-29 01:08:17 +0000642 computeJD(p);
643 clearYMD_HMS_TZ(p);
drhf11c34d2006-09-08 12:27:36 +0000644 p->rJD += tx.rJD;
drh33a9ad22004-02-29 00:40:32 +0000645 rc = 0;
646 break;
647 }
drh4d5b8362004-01-17 01:16:21 +0000648 z += n;
drh4c755c02004-08-08 20:22:17 +0000649 while( isspace(*(u8*)z) ) z++;
drh4d5b8362004-01-17 01:16:21 +0000650 n = strlen(z);
drh7014aff2003-11-01 01:53:53 +0000651 if( n>10 || n<3 ) break;
drh7014aff2003-11-01 01:53:53 +0000652 if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
653 computeJD(p);
654 rc = 0;
655 if( n==3 && strcmp(z,"day")==0 ){
656 p->rJD += r;
657 }else if( n==4 && strcmp(z,"hour")==0 ){
drh7014aff2003-11-01 01:53:53 +0000658 p->rJD += r/24.0;
659 }else if( n==6 && strcmp(z,"minute")==0 ){
drh7014aff2003-11-01 01:53:53 +0000660 p->rJD += r/(24.0*60.0);
661 }else if( n==6 && strcmp(z,"second")==0 ){
drh7014aff2003-11-01 01:53:53 +0000662 p->rJD += r/(24.0*60.0*60.0);
663 }else if( n==5 && strcmp(z,"month")==0 ){
664 int x, y;
drhba212562004-01-08 02:17:31 +0000665 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000666 p->M += r;
667 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
668 p->Y += x;
669 p->M -= x*12;
670 p->validJD = 0;
671 computeJD(p);
672 y = r;
673 if( y!=r ){
674 p->rJD += (r - y)*30.0;
675 }
676 }else if( n==4 && strcmp(z,"year")==0 ){
drhba212562004-01-08 02:17:31 +0000677 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000678 p->Y += r;
679 p->validJD = 0;
680 computeJD(p);
681 }else{
682 rc = 1;
683 }
drhba212562004-01-08 02:17:31 +0000684 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000685 break;
686 }
687 default: {
688 break;
689 }
690 }
691 return rc;
692}
693
694/*
695** Process time function arguments. argv[0] is a date-time stamp.
696** argv[1] and following are modifiers. Parse them all and write
697** the resulting time into the DateTime structure p. Return 0
698** on success and 1 if there are any errors.
drh008e4762008-01-17 22:27:53 +0000699**
700** If there are zero parameters (if even argv[0] is undefined)
701** then assume a default value of "now" for argv[0].
drh7014aff2003-11-01 01:53:53 +0000702*/
danielk1977fee2d252007-08-18 10:59:19 +0000703static int isDate(
704 sqlite3_context *context,
705 int argc,
706 sqlite3_value **argv,
707 DateTime *p
708){
drh7014aff2003-11-01 01:53:53 +0000709 int i;
drh7a521cf2007-04-25 18:23:52 +0000710 const unsigned char *z;
drh008e4762008-01-17 22:27:53 +0000711 static const unsigned char zDflt[] = "now";
712 if( argc==0 ){
713 z = zDflt;
714 }else{
715 z = sqlite3_value_text(argv[0]);
716 }
danielk1977fee2d252007-08-18 10:59:19 +0000717 if( !z || parseDateOrTime(context, (char*)z, p) ){
drh7a521cf2007-04-25 18:23:52 +0000718 return 1;
719 }
drh7014aff2003-11-01 01:53:53 +0000720 for(i=1; i<argc; i++){
drh7a521cf2007-04-25 18:23:52 +0000721 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
722 return 1;
723 }
drh7014aff2003-11-01 01:53:53 +0000724 }
725 return 0;
726}
727
728
729/*
730** The following routines implement the various date and time functions
731** of SQLite.
732*/
733
734/*
735** julianday( TIMESTRING, MOD, MOD, ...)
736**
737** Return the julian day number of the date specified in the arguments
738*/
drhf9b596e2004-05-26 16:54:42 +0000739static void juliandayFunc(
740 sqlite3_context *context,
741 int argc,
742 sqlite3_value **argv
743){
drh7014aff2003-11-01 01:53:53 +0000744 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000745 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000746 computeJD(&x);
danielk19777e18c252004-05-25 11:47:24 +0000747 sqlite3_result_double(context, x.rJD);
drh7014aff2003-11-01 01:53:53 +0000748 }
749}
750
751/*
752** datetime( TIMESTRING, MOD, MOD, ...)
753**
754** Return YYYY-MM-DD HH:MM:SS
755*/
drhf9b596e2004-05-26 16:54:42 +0000756static void datetimeFunc(
757 sqlite3_context *context,
758 int argc,
759 sqlite3_value **argv
760){
drh7014aff2003-11-01 01:53:53 +0000761 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000762 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000763 char zBuf[100];
drhba212562004-01-08 02:17:31 +0000764 computeYMD_HMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000765 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
766 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
danielk1977d8123362004-06-12 09:25:12 +0000767 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000768 }
769}
770
771/*
772** time( TIMESTRING, MOD, MOD, ...)
773**
774** Return HH:MM:SS
775*/
drhf9b596e2004-05-26 16:54:42 +0000776static void timeFunc(
777 sqlite3_context *context,
778 int argc,
779 sqlite3_value **argv
780){
drh7014aff2003-11-01 01:53:53 +0000781 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000782 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000783 char zBuf[100];
784 computeHMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000785 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
danielk1977d8123362004-06-12 09:25:12 +0000786 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000787 }
788}
789
790/*
791** date( TIMESTRING, MOD, MOD, ...)
792**
793** Return YYYY-MM-DD
794*/
drhf9b596e2004-05-26 16:54:42 +0000795static void dateFunc(
796 sqlite3_context *context,
797 int argc,
798 sqlite3_value **argv
799){
drh7014aff2003-11-01 01:53:53 +0000800 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000801 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000802 char zBuf[100];
803 computeYMD(&x);
drh5bb3eb92007-05-04 13:15:55 +0000804 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
danielk1977d8123362004-06-12 09:25:12 +0000805 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000806 }
807}
808
809/*
810** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
811**
812** Return a string described by FORMAT. Conversions as follows:
813**
814** %d day of month
815** %f ** fractional seconds SS.SSS
816** %H hour 00-24
817** %j day of year 000-366
818** %J ** Julian day number
819** %m month 01-12
820** %M minute 00-59
821** %s seconds since 1970-01-01
822** %S seconds 00-59
823** %w day of week 0-6 sunday==0
824** %W week of year 00-53
825** %Y year 0000-9999
826** %% %
827*/
drhf9b596e2004-05-26 16:54:42 +0000828static void strftimeFunc(
829 sqlite3_context *context,
830 int argc,
831 sqlite3_value **argv
832){
drh7014aff2003-11-01 01:53:53 +0000833 DateTime x;
drha0206bc2007-05-08 15:15:02 +0000834 u64 n;
835 int i, j;
drh7014aff2003-11-01 01:53:53 +0000836 char *z;
drh2646da72005-12-09 20:02:05 +0000837 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
drh7014aff2003-11-01 01:53:53 +0000838 char zBuf[100];
danielk1977fee2d252007-08-18 10:59:19 +0000839 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
drh7014aff2003-11-01 01:53:53 +0000840 for(i=0, n=1; zFmt[i]; i++, n++){
841 if( zFmt[i]=='%' ){
842 switch( zFmt[i+1] ){
843 case 'd':
844 case 'H':
845 case 'm':
846 case 'M':
847 case 'S':
848 case 'W':
849 n++;
850 /* fall thru */
851 case 'w':
852 case '%':
853 break;
854 case 'f':
855 n += 8;
856 break;
857 case 'j':
858 n += 3;
859 break;
860 case 'Y':
861 n += 8;
862 break;
863 case 's':
864 case 'J':
865 n += 50;
866 break;
867 default:
868 return; /* ERROR. return a NULL */
869 }
870 i++;
871 }
872 }
873 if( n<sizeof(zBuf) ){
874 z = zBuf;
drhbb4957f2008-03-20 14:03:29 +0000875 }else if( n>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){
drha0206bc2007-05-08 15:15:02 +0000876 sqlite3_result_error_toobig(context);
877 return;
drh7014aff2003-11-01 01:53:53 +0000878 }else{
drh17435752007-08-16 04:30:38 +0000879 z = sqlite3_malloc( n );
drh3334e942008-01-17 20:26:46 +0000880 if( z==0 ){
881 sqlite3_result_error_nomem(context);
882 return;
883 }
drh7014aff2003-11-01 01:53:53 +0000884 }
885 computeJD(&x);
drhba212562004-01-08 02:17:31 +0000886 computeYMD_HMS(&x);
drh7014aff2003-11-01 01:53:53 +0000887 for(i=j=0; zFmt[i]; i++){
888 if( zFmt[i]!='%' ){
889 z[j++] = zFmt[i];
890 }else{
891 i++;
892 switch( zFmt[i] ){
drh5bb3eb92007-05-04 13:15:55 +0000893 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000894 case 'f': {
drhb1f1e6e2006-09-25 18:01:31 +0000895 double s = x.s;
896 if( s>59.999 ) s = 59.999;
drh2ecad3b2007-03-29 17:57:21 +0000897 sqlite3_snprintf(7, &z[j],"%06.3f", s);
drh7014aff2003-11-01 01:53:53 +0000898 j += strlen(&z[j]);
899 break;
900 }
drh5bb3eb92007-05-04 13:15:55 +0000901 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000902 case 'W': /* Fall thru */
903 case 'j': {
danielk1977f0113002006-01-24 12:09:17 +0000904 int nDay; /* Number of days since 1st day of year */
drh7014aff2003-11-01 01:53:53 +0000905 DateTime y = x;
906 y.validJD = 0;
907 y.M = 1;
908 y.D = 1;
909 computeJD(&y);
drhc2c9eef2007-01-08 13:07:30 +0000910 nDay = x.rJD - y.rJD + 0.5;
drh7014aff2003-11-01 01:53:53 +0000911 if( zFmt[i]=='W' ){
drh1020d492004-07-18 22:22:43 +0000912 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
913 wd = ((int)(x.rJD+0.5)) % 7;
drh5bb3eb92007-05-04 13:15:55 +0000914 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
drh7014aff2003-11-01 01:53:53 +0000915 j += 2;
916 }else{
drh5bb3eb92007-05-04 13:15:55 +0000917 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
drh7014aff2003-11-01 01:53:53 +0000918 j += 3;
919 }
920 break;
921 }
drh5bb3eb92007-05-04 13:15:55 +0000922 case 'J': {
923 sqlite3_snprintf(20, &z[j],"%.16g",x.rJD);
924 j+=strlen(&z[j]);
925 break;
926 }
927 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
928 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000929 case 's': {
drh5bb3eb92007-05-04 13:15:55 +0000930 sqlite3_snprintf(30,&z[j],"%d",
931 (int)((x.rJD-2440587.5)*86400.0 + 0.5));
drh7014aff2003-11-01 01:53:53 +0000932 j += strlen(&z[j]);
933 break;
934 }
drh5bb3eb92007-05-04 13:15:55 +0000935 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000936 case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
drh5bb3eb92007-05-04 13:15:55 +0000937 case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break;
drh008e4762008-01-17 22:27:53 +0000938 default: z[j++] = '%'; break;
drh7014aff2003-11-01 01:53:53 +0000939 }
940 }
941 }
942 z[j] = 0;
drh3334e942008-01-17 20:26:46 +0000943 sqlite3_result_text(context, z, -1,
944 z==zBuf ? SQLITE_TRANSIENT : sqlite3_free);
drh7014aff2003-11-01 01:53:53 +0000945}
946
danielk19777977a172004-11-09 12:44:37 +0000947/*
948** current_time()
949**
950** This function returns the same value as time('now').
951*/
952static void ctimeFunc(
953 sqlite3_context *context,
954 int argc,
955 sqlite3_value **argv
956){
drh008e4762008-01-17 22:27:53 +0000957 timeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000958}
drh7014aff2003-11-01 01:53:53 +0000959
danielk19777977a172004-11-09 12:44:37 +0000960/*
961** current_date()
962**
963** This function returns the same value as date('now').
964*/
965static void cdateFunc(
966 sqlite3_context *context,
967 int argc,
968 sqlite3_value **argv
969){
drh008e4762008-01-17 22:27:53 +0000970 dateFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000971}
972
973/*
974** current_timestamp()
975**
976** This function returns the same value as datetime('now').
977*/
978static void ctimestampFunc(
979 sqlite3_context *context,
980 int argc,
981 sqlite3_value **argv
982){
drh008e4762008-01-17 22:27:53 +0000983 datetimeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000984}
drh7014aff2003-11-01 01:53:53 +0000985#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
986
danielk1977752e6792004-11-09 16:13:33 +0000987#ifdef SQLITE_OMIT_DATETIME_FUNCS
988/*
989** If the library is compiled to omit the full-scale date and time
990** handling (to get a smaller binary), the following minimal version
991** of the functions current_time(), current_date() and current_timestamp()
992** are included instead. This is to support column declarations that
993** include "DEFAULT CURRENT_TIME" etc.
994**
danielk19772df9fab2004-11-11 01:50:30 +0000995** This function uses the C-library functions time(), gmtime()
danielk1977752e6792004-11-09 16:13:33 +0000996** and strftime(). The format string to pass to strftime() is supplied
997** as the user-data for the function.
998*/
danielk1977752e6792004-11-09 16:13:33 +0000999static void currentTimeFunc(
1000 sqlite3_context *context,
1001 int argc,
1002 sqlite3_value **argv
1003){
1004 time_t t;
1005 char *zFormat = (char *)sqlite3_user_data(context);
drhfa4a4b92008-03-19 21:45:51 +00001006 sqlite3 *db;
drh8257f0c2008-03-19 20:18:27 +00001007 double rT;
danielk1977752e6792004-11-09 16:13:33 +00001008 char zBuf[20];
danielk1977752e6792004-11-09 16:13:33 +00001009
drhfa4a4b92008-03-19 21:45:51 +00001010 db = sqlite3_context_db_handle(context);
1011 sqlite3OsCurrentTime(db->pVfs, &rT);
drh8257f0c2008-03-19 20:18:27 +00001012 t = 86400.0*(rT - 2440587.5) + 0.5;
drh87595762006-09-08 12:49:43 +00001013#ifdef HAVE_GMTIME_R
1014 {
1015 struct tm sNow;
1016 gmtime_r(&t, &sNow);
1017 strftime(zBuf, 20, zFormat, &sNow);
1018 }
1019#else
1020 {
1021 struct tm *pTm;
danielk19774152e672007-09-12 17:01:45 +00001022 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001023 pTm = gmtime(&t);
1024 strftime(zBuf, 20, zFormat, pTm);
danielk19774152e672007-09-12 17:01:45 +00001025 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001026 }
1027#endif
danielk1977e6efa742004-11-10 11:55:10 +00001028
danielk1977752e6792004-11-09 16:13:33 +00001029 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1030}
1031#endif
1032
drh7014aff2003-11-01 01:53:53 +00001033/*
1034** This function registered all of the above C functions as SQL
1035** functions. This should be the only routine in this file with
1036** external linkage.
1037*/
drh9bb575f2004-09-06 17:24:11 +00001038void sqlite3RegisterDateTimeFunctions(sqlite3 *db){
drhfd1f3942004-07-20 00:39:14 +00001039#ifndef SQLITE_OMIT_DATETIME_FUNCS
drh57196282004-10-06 15:41:16 +00001040 static const struct {
drh7014aff2003-11-01 01:53:53 +00001041 char *zName;
1042 int nArg;
danielk19770ae8b832004-05-25 12:05:56 +00001043 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drh7014aff2003-11-01 01:53:53 +00001044 } aFuncs[] = {
drhf9b596e2004-05-26 16:54:42 +00001045 { "julianday", -1, juliandayFunc },
1046 { "date", -1, dateFunc },
1047 { "time", -1, timeFunc },
1048 { "datetime", -1, datetimeFunc },
1049 { "strftime", -1, strftimeFunc },
danielk19777977a172004-11-09 12:44:37 +00001050 { "current_time", 0, ctimeFunc },
1051 { "current_timestamp", 0, ctimestampFunc },
1052 { "current_date", 0, cdateFunc },
drh7014aff2003-11-01 01:53:53 +00001053 };
1054 int i;
1055
1056 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977771151b2006-01-17 13:21:40 +00001057 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
drhfa4a4b92008-03-19 21:45:51 +00001058 SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
drh7014aff2003-11-01 01:53:53 +00001059 }
danielk1977752e6792004-11-09 16:13:33 +00001060#else
1061 static const struct {
1062 char *zName;
1063 char *zFormat;
1064 } aFuncs[] = {
1065 { "current_time", "%H:%M:%S" },
1066 { "current_date", "%Y-%m-%d" },
1067 { "current_timestamp", "%Y-%m-%d %H:%M:%S" }
1068 };
1069 int i;
1070
1071 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
danielk1977771151b2006-01-17 13:21:40 +00001072 sqlite3CreateFunc(db, aFuncs[i].zName, 0, SQLITE_UTF8,
danielk1977752e6792004-11-09 16:13:33 +00001073 aFuncs[i].zFormat, currentTimeFunc, 0, 0);
1074 }
drhfd1f3942004-07-20 00:39:14 +00001075#endif
drh7014aff2003-11-01 01:53:53 +00001076}