blob: 2c39a0a0d0f19bc2a542e17622826ad47de3effe [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**
drh7014aff2003-11-01 01:53:53 +000019** SQLite processes all times and dates as Julian Day numbers. The
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
drh7f986a62006-09-25 18:05:04 +000022** calendar system.
drh7014aff2003-11-01 01:53:53 +000023**
24** 1970-01-01 00:00:00 is JD 2440587.5
25** 2000-01-01 00:00:00 is JD 2451544.5
26**
27** This implemention 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
29** be represented, even though julian day numbers allow a much wider
30** range of dates.
31**
32** The Gregorian calendar system is used for all dates and times,
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
35** dates afterwards, depending on locale. Beware of this difference.
36**
37** The conversion algorithms are implemented based on descriptions
38** in the following text:
39**
40** Jean Meeus
41** Astronomical Algorithms, 2nd Edition, 1998
42** ISBM 0-943396-61-1
43** Willmann-Bell, Inc
44** Richmond, Virginia (USA)
45*/
dougcurrieae534182003-12-24 01:41:19 +000046#include "sqliteInt.h"
drh7014aff2003-11-01 01:53:53 +000047#include <stdlib.h>
48#include <assert.h>
drh7091cb02003-12-23 16:22:18 +000049#include <time.h>
drh7014aff2003-11-01 01:53:53 +000050
drh4bc05852004-02-10 13:19:35 +000051#ifndef SQLITE_OMIT_DATETIME_FUNCS
52
drh7014aff2003-11-01 01:53:53 +000053/*
shaneb8109ad2008-05-27 19:49:21 +000054** 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
70/*
drh7014aff2003-11-01 01:53:53 +000071** A structure for holding a single date and time.
72*/
73typedef struct DateTime DateTime;
74struct DateTime {
drh85f477a2008-06-12 16:35:38 +000075 sqlite3_int64 iJD; /* The julian day number times 86400000 */
76 int Y, M, D; /* Year, month, and day */
77 int h, m; /* Hour and minutes */
78 int tz; /* Timezone offset in minutes */
79 double s; /* Seconds */
shaneaef3af52008-12-09 04:59:00 +000080 char validYMD; /* True (1) if Y,M,D are valid */
81 char validHMS; /* True (1) if h,m,s are valid */
82 char validJD; /* True (1) if iJD is valid */
83 char validTZ; /* True (1) if tz is valid */
drh7014aff2003-11-01 01:53:53 +000084};
85
86
87/*
drheb9a9e82004-02-22 17:49:32 +000088** Convert zDate into one or more integers. Additional arguments
89** come in groups of 5 as follows:
90**
91** N number of digits in the integer
92** min minimum allowed value of the integer
93** max maximum allowed value of the integer
94** nextC first character after the integer
95** pVal where to write the integers value.
96**
97** Conversions continue until one with nextC==0 is encountered.
98** The function returns the number of successful conversions.
drh7014aff2003-11-01 01:53:53 +000099*/
drheb9a9e82004-02-22 17:49:32 +0000100static int getDigits(const char *zDate, ...){
101 va_list ap;
102 int val;
103 int N;
104 int min;
105 int max;
106 int nextC;
107 int *pVal;
108 int cnt = 0;
109 va_start(ap, zDate);
110 do{
111 N = va_arg(ap, int);
112 min = va_arg(ap, int);
113 max = va_arg(ap, int);
114 nextC = va_arg(ap, int);
115 pVal = va_arg(ap, int*);
116 val = 0;
117 while( N-- ){
danielk197778ca0e72009-01-20 16:53:39 +0000118 if( !sqlite3Isdigit(*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000119 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000120 }
121 val = val*10 + *zDate - '0';
122 zDate++;
123 }
124 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000125 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000126 }
127 *pVal = val;
drh7014aff2003-11-01 01:53:53 +0000128 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000129 cnt++;
130 }while( nextC );
drh029b44b2006-01-15 00:13:15 +0000131end_getDigits:
drh15b9a152006-01-31 20:49:13 +0000132 va_end(ap);
drheb9a9e82004-02-22 17:49:32 +0000133 return cnt;
drh7014aff2003-11-01 01:53:53 +0000134}
135
136/*
137** Read text from z[] and convert into a floating point number. Return
138** the number of digits converted.
139*/
drh487e2622005-06-25 18:42:14 +0000140#define getValue sqlite3AtoF
drh7014aff2003-11-01 01:53:53 +0000141
142/*
143** Parse a timezone extension on the end of a date-time.
144** The extension is of the form:
145**
146** (+/-)HH:MM
147**
drh1cfdc902008-02-21 20:40:43 +0000148** Or the "zulu" notation:
149**
150** Z
151**
drh7014aff2003-11-01 01:53:53 +0000152** If the parse is successful, write the number of minutes
drh1cfdc902008-02-21 20:40:43 +0000153** of change in p->tz and return 0. If a parser error occurs,
154** return non-zero.
drh7014aff2003-11-01 01:53:53 +0000155**
156** A missing specifier is not considered an error.
157*/
158static int parseTimezone(const char *zDate, DateTime *p){
159 int sgn = 0;
160 int nHr, nMn;
drh1cfdc902008-02-21 20:40:43 +0000161 int c;
danielk197778ca0e72009-01-20 16:53:39 +0000162 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000163 p->tz = 0;
drh1cfdc902008-02-21 20:40:43 +0000164 c = *zDate;
165 if( c=='-' ){
drh7014aff2003-11-01 01:53:53 +0000166 sgn = -1;
drh1cfdc902008-02-21 20:40:43 +0000167 }else if( c=='+' ){
drh7014aff2003-11-01 01:53:53 +0000168 sgn = +1;
drh1cfdc902008-02-21 20:40:43 +0000169 }else if( c=='Z' || c=='z' ){
170 zDate++;
171 goto zulu_time;
drh7014aff2003-11-01 01:53:53 +0000172 }else{
drh1cfdc902008-02-21 20:40:43 +0000173 return c!=0;
drh7014aff2003-11-01 01:53:53 +0000174 }
175 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000176 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
177 return 1;
178 }
179 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000180 p->tz = sgn*(nMn + nHr*60);
drh1cfdc902008-02-21 20:40:43 +0000181zulu_time:
danielk197778ca0e72009-01-20 16:53:39 +0000182 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000183 return *zDate!=0;
184}
185
186/*
187** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
188** The HH, MM, and SS must each be exactly 2 digits. The
189** fractional seconds FFFF can be one or more digits.
190**
191** Return 1 if there is a parsing error and 0 on success.
192*/
193static int parseHhMmSs(const char *zDate, DateTime *p){
194 int h, m, s;
195 double ms = 0.0;
drheb9a9e82004-02-22 17:49:32 +0000196 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
197 return 1;
198 }
199 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000200 if( *zDate==':' ){
drheb9a9e82004-02-22 17:49:32 +0000201 zDate++;
202 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
203 return 1;
204 }
205 zDate += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000206 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
drh7014aff2003-11-01 01:53:53 +0000207 double rScale = 1.0;
208 zDate++;
danielk197778ca0e72009-01-20 16:53:39 +0000209 while( sqlite3Isdigit(*zDate) ){
drh7014aff2003-11-01 01:53:53 +0000210 ms = ms*10.0 + *zDate - '0';
211 rScale *= 10.0;
212 zDate++;
213 }
214 ms /= rScale;
215 }
216 }else{
217 s = 0;
218 }
219 p->validJD = 0;
220 p->validHMS = 1;
221 p->h = h;
222 p->m = m;
223 p->s = s + ms;
224 if( parseTimezone(zDate, p) ) return 1;
shaneaef3af52008-12-09 04:59:00 +0000225 p->validTZ = (p->tz!=0)?1:0;
drh7014aff2003-11-01 01:53:53 +0000226 return 0;
227}
228
229/*
230** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
231** that the YYYY-MM-DD is according to the Gregorian calendar.
232**
233** Reference: Meeus page 61
234*/
235static void computeJD(DateTime *p){
236 int Y, M, D, A, B, X1, X2;
237
238 if( p->validJD ) return;
239 if( p->validYMD ){
240 Y = p->Y;
241 M = p->M;
242 D = p->D;
243 }else{
drhba212562004-01-08 02:17:31 +0000244 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
drh7014aff2003-11-01 01:53:53 +0000245 M = 1;
246 D = 1;
247 }
248 if( M<=2 ){
249 Y--;
250 M += 12;
251 }
252 A = Y/100;
253 B = 2 - A + (A/4);
shaneaef3af52008-12-09 04:59:00 +0000254 X1 = 36525*(Y+4716)/100;
255 X2 = 306001*(M+1)/10000;
256 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
drh7014aff2003-11-01 01:53:53 +0000257 p->validJD = 1;
drh7014aff2003-11-01 01:53:53 +0000258 if( p->validHMS ){
shaneaef3af52008-12-09 04:59:00 +0000259 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
drh7014aff2003-11-01 01:53:53 +0000260 if( p->validTZ ){
drh85f477a2008-06-12 16:35:38 +0000261 p->iJD -= p->tz*60000;
drhf11c34d2006-09-08 12:27:36 +0000262 p->validYMD = 0;
drh7014aff2003-11-01 01:53:53 +0000263 p->validHMS = 0;
264 p->validTZ = 0;
265 }
266 }
267}
268
269/*
270** Parse dates of the form
271**
272** YYYY-MM-DD HH:MM:SS.FFF
273** YYYY-MM-DD HH:MM:SS
274** YYYY-MM-DD HH:MM
275** YYYY-MM-DD
276**
277** Write the result into the DateTime structure and return 0
278** on success and 1 if the input string is not a well-formed
279** date.
280*/
281static int parseYyyyMmDd(const char *zDate, DateTime *p){
drh8eb2cce2004-02-21 03:28:18 +0000282 int Y, M, D, neg;
drh7014aff2003-11-01 01:53:53 +0000283
drh8eb2cce2004-02-21 03:28:18 +0000284 if( zDate[0]=='-' ){
285 zDate++;
286 neg = 1;
287 }else{
288 neg = 0;
289 }
drheb9a9e82004-02-22 17:49:32 +0000290 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
291 return 1;
292 }
293 zDate += 10;
danielk197778ca0e72009-01-20 16:53:39 +0000294 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
drheb9a9e82004-02-22 17:49:32 +0000295 if( parseHhMmSs(zDate, p)==0 ){
296 /* We got the time */
drh7014aff2003-11-01 01:53:53 +0000297 }else if( *zDate==0 ){
298 p->validHMS = 0;
299 }else{
300 return 1;
301 }
302 p->validJD = 0;
303 p->validYMD = 1;
drh8eb2cce2004-02-21 03:28:18 +0000304 p->Y = neg ? -Y : Y;
drh7014aff2003-11-01 01:53:53 +0000305 p->M = M;
306 p->D = D;
307 if( p->validTZ ){
308 computeJD(p);
309 }
310 return 0;
311}
312
313/*
drh3af5d682008-06-12 13:50:00 +0000314** Set the time to the current time reported by the VFS
315*/
316static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
317 double r;
318 sqlite3 *db = sqlite3_context_db_handle(context);
319 sqlite3OsCurrentTime(db->pVfs, &r);
drh85f477a2008-06-12 16:35:38 +0000320 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000321 p->validJD = 1;
322}
323
324/*
drh7014aff2003-11-01 01:53:53 +0000325** Attempt to parse the given string into a Julian Day Number. Return
326** the number of errors.
327**
328** The following are acceptable forms for the input string:
329**
330** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
331** DDDD.DD
332** now
333**
334** In the first form, the +/-HH:MM is always optional. The fractional
335** seconds extension (the ".FFF") is optional. The seconds portion
336** (":SS.FFF") is option. The year and date can be omitted as long
337** as there is a time string. The time string can be omitted as long
338** as there is a year and date.
339*/
danielk1977fee2d252007-08-18 10:59:19 +0000340static int parseDateOrTime(
341 sqlite3_context *context,
342 const char *zDate,
343 DateTime *p
344){
drhdee0e402009-05-03 20:23:53 +0000345 int isRealNum; /* Return from sqlite3IsNumber(). Not used */
drh8eb2cce2004-02-21 03:28:18 +0000346 if( parseYyyyMmDd(zDate,p)==0 ){
drh7014aff2003-11-01 01:53:53 +0000347 return 0;
drh8eb2cce2004-02-21 03:28:18 +0000348 }else if( parseHhMmSs(zDate, p)==0 ){
349 return 0;
danielk19774adee202004-05-08 08:23:19 +0000350 }else if( sqlite3StrICmp(zDate,"now")==0){
drh3af5d682008-06-12 13:50:00 +0000351 setDateTimeToCurrent(context, p);
drh018d1a42005-01-15 01:52:31 +0000352 return 0;
drhdee0e402009-05-03 20:23:53 +0000353 }else if( sqlite3IsNumber(zDate, &isRealNum, SQLITE_UTF8) ){
drh85f477a2008-06-12 16:35:38 +0000354 double r;
355 getValue(zDate, &r);
356 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh7014aff2003-11-01 01:53:53 +0000357 p->validJD = 1;
358 return 0;
359 }
360 return 1;
361}
362
363/*
364** Compute the Year, Month, and Day from the julian day number.
365*/
366static void computeYMD(DateTime *p){
367 int Z, A, B, C, D, E, X1;
368 if( p->validYMD ) return;
drh33a9ad22004-02-29 00:40:32 +0000369 if( !p->validJD ){
370 p->Y = 2000;
371 p->M = 1;
372 p->D = 1;
373 }else{
shaneaef3af52008-12-09 04:59:00 +0000374 Z = (int)((p->iJD + 43200000)/86400000);
375 A = (int)((Z - 1867216.25)/36524.25);
drh33a9ad22004-02-29 00:40:32 +0000376 A = Z + 1 + A - (A/4);
377 B = A + 1524;
shaneaef3af52008-12-09 04:59:00 +0000378 C = (int)((B - 122.1)/365.25);
379 D = (36525*C)/100;
380 E = (int)((B-D)/30.6001);
381 X1 = (int)(30.6001*E);
drh33a9ad22004-02-29 00:40:32 +0000382 p->D = B - D - X1;
383 p->M = E<14 ? E-1 : E-13;
384 p->Y = p->M>2 ? C - 4716 : C - 4715;
385 }
drh7014aff2003-11-01 01:53:53 +0000386 p->validYMD = 1;
387}
388
389/*
390** Compute the Hour, Minute, and Seconds from the julian day number.
391*/
392static void computeHMS(DateTime *p){
drh85f477a2008-06-12 16:35:38 +0000393 int s;
drh7014aff2003-11-01 01:53:53 +0000394 if( p->validHMS ) return;
drhf11c34d2006-09-08 12:27:36 +0000395 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000396 s = (int)((p->iJD + 43200000) % 86400000);
drh85f477a2008-06-12 16:35:38 +0000397 p->s = s/1000.0;
shaneaef3af52008-12-09 04:59:00 +0000398 s = (int)p->s;
drh7014aff2003-11-01 01:53:53 +0000399 p->s -= s;
400 p->h = s/3600;
401 s -= p->h*3600;
402 p->m = s/60;
403 p->s += s - p->m*60;
404 p->validHMS = 1;
405}
406
407/*
drhba212562004-01-08 02:17:31 +0000408** Compute both YMD and HMS
409*/
410static void computeYMD_HMS(DateTime *p){
411 computeYMD(p);
412 computeHMS(p);
413}
414
415/*
416** Clear the YMD and HMS and the TZ
417*/
418static void clearYMD_HMS_TZ(DateTime *p){
419 p->validYMD = 0;
420 p->validHMS = 0;
421 p->validTZ = 0;
422}
423
drh66147c92008-06-12 12:51:37 +0000424#ifndef SQLITE_OMIT_LOCALTIME
drhba212562004-01-08 02:17:31 +0000425/*
drh85f477a2008-06-12 16:35:38 +0000426** Compute the difference (in milliseconds)
427** between localtime and UTC (a.k.a. GMT)
drh7091cb02003-12-23 16:22:18 +0000428** for the time value p where p is in UTC.
429*/
shaneaef3af52008-12-09 04:59:00 +0000430static sqlite3_int64 localtimeOffset(DateTime *p){
drh7091cb02003-12-23 16:22:18 +0000431 DateTime x, y;
432 time_t t;
drh7091cb02003-12-23 16:22:18 +0000433 x = *p;
drhba212562004-01-08 02:17:31 +0000434 computeYMD_HMS(&x);
drh7091cb02003-12-23 16:22:18 +0000435 if( x.Y<1971 || x.Y>=2038 ){
436 x.Y = 2000;
437 x.M = 1;
438 x.D = 1;
439 x.h = 0;
440 x.m = 0;
441 x.s = 0.0;
442 } else {
shaneaef3af52008-12-09 04:59:00 +0000443 int s = (int)(x.s + 0.5);
drh7091cb02003-12-23 16:22:18 +0000444 x.s = s;
445 }
446 x.tz = 0;
447 x.validJD = 0;
448 computeJD(&x);
shane11bb41f2009-09-10 20:23:30 +0000449 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
drh87595762006-09-08 12:49:43 +0000450#ifdef HAVE_LOCALTIME_R
451 {
452 struct tm sLocal;
453 localtime_r(&t, &sLocal);
454 y.Y = sLocal.tm_year + 1900;
455 y.M = sLocal.tm_mon + 1;
456 y.D = sLocal.tm_mday;
457 y.h = sLocal.tm_hour;
458 y.m = sLocal.tm_min;
459 y.s = sLocal.tm_sec;
460 }
shane3e82c1d2009-09-22 13:25:00 +0000461#elif defined(HAVE_LOCALTIME_S) && HAVE_LOCALTIME_S
shaneb8109ad2008-05-27 19:49:21 +0000462 {
463 struct tm sLocal;
464 localtime_s(&sLocal, &t);
465 y.Y = sLocal.tm_year + 1900;
466 y.M = sLocal.tm_mon + 1;
467 y.D = sLocal.tm_mday;
468 y.h = sLocal.tm_hour;
469 y.m = sLocal.tm_min;
470 y.s = sLocal.tm_sec;
471 }
drh87595762006-09-08 12:49:43 +0000472#else
473 {
474 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +0000475 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000476 pTm = localtime(&t);
477 y.Y = pTm->tm_year + 1900;
478 y.M = pTm->tm_mon + 1;
479 y.D = pTm->tm_mday;
480 y.h = pTm->tm_hour;
481 y.m = pTm->tm_min;
482 y.s = pTm->tm_sec;
danielk197759f8c082008-06-18 17:09:10 +0000483 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000484 }
485#endif
drh7091cb02003-12-23 16:22:18 +0000486 y.validYMD = 1;
487 y.validHMS = 1;
488 y.validJD = 0;
489 y.validTZ = 0;
490 computeJD(&y);
drh85f477a2008-06-12 16:35:38 +0000491 return y.iJD - x.iJD;
drh7091cb02003-12-23 16:22:18 +0000492}
drh66147c92008-06-12 12:51:37 +0000493#endif /* SQLITE_OMIT_LOCALTIME */
drh7091cb02003-12-23 16:22:18 +0000494
495/*
drh7014aff2003-11-01 01:53:53 +0000496** Process a modifier to a date-time stamp. The modifiers are
497** as follows:
498**
499** NNN days
500** NNN hours
501** NNN minutes
502** NNN.NNNN seconds
503** NNN months
504** NNN years
505** start of month
506** start of year
507** start of week
508** start of day
509** weekday N
510** unixepoch
drh7091cb02003-12-23 16:22:18 +0000511** localtime
512** utc
drh7014aff2003-11-01 01:53:53 +0000513**
514** Return 0 on success and 1 if there is any kind of error.
515*/
516static int parseModifier(const char *zMod, DateTime *p){
517 int rc = 1;
518 int n;
519 double r;
drh4d5b8362004-01-17 01:16:21 +0000520 char *z, zBuf[30];
521 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000522 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
drh1bd10f82008-12-10 21:19:56 +0000523 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
drh7014aff2003-11-01 01:53:53 +0000524 }
525 z[n] = 0;
526 switch( z[0] ){
drh66147c92008-06-12 12:51:37 +0000527#ifndef SQLITE_OMIT_LOCALTIME
drh7091cb02003-12-23 16:22:18 +0000528 case 'l': {
529 /* localtime
530 **
531 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
532 ** show local time.
533 */
534 if( strcmp(z, "localtime")==0 ){
535 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000536 p->iJD += localtimeOffset(p);
drhba212562004-01-08 02:17:31 +0000537 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000538 rc = 0;
539 }
540 break;
541 }
drh66147c92008-06-12 12:51:37 +0000542#endif
drh7014aff2003-11-01 01:53:53 +0000543 case 'u': {
544 /*
545 ** unixepoch
546 **
drh85f477a2008-06-12 16:35:38 +0000547 ** Treat the current value of p->iJD as the number of
drh7014aff2003-11-01 01:53:53 +0000548 ** seconds since 1970. Convert to a real julian day number.
549 */
550 if( strcmp(z, "unixepoch")==0 && p->validJD ){
drh7fee3602009-04-16 12:58:03 +0000551 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
drhba212562004-01-08 02:17:31 +0000552 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000553 rc = 0;
drh66cccd92008-07-25 16:39:24 +0000554 }
555#ifndef SQLITE_OMIT_LOCALTIME
556 else if( strcmp(z, "utc")==0 ){
shaneaef3af52008-12-09 04:59:00 +0000557 sqlite3_int64 c1;
drh7091cb02003-12-23 16:22:18 +0000558 computeJD(p);
559 c1 = localtimeOffset(p);
drh85f477a2008-06-12 16:35:38 +0000560 p->iJD -= c1;
drhba212562004-01-08 02:17:31 +0000561 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000562 p->iJD += c1 - localtimeOffset(p);
drh7091cb02003-12-23 16:22:18 +0000563 rc = 0;
drh7014aff2003-11-01 01:53:53 +0000564 }
drh66cccd92008-07-25 16:39:24 +0000565#endif
drh7014aff2003-11-01 01:53:53 +0000566 break;
567 }
568 case 'w': {
569 /*
570 ** weekday N
571 **
drh181fc992004-08-17 10:42:54 +0000572 ** Move the date to the same time on the next occurrence of
drh7014aff2003-11-01 01:53:53 +0000573 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
drhc5dd9fa2004-01-07 03:29:16 +0000574 ** date is already on the appropriate weekday, this is a no-op.
drh7014aff2003-11-01 01:53:53 +0000575 */
576 if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
shaneaef3af52008-12-09 04:59:00 +0000577 && (n=(int)r)==r && n>=0 && r<7 ){
drh85f477a2008-06-12 16:35:38 +0000578 sqlite3_int64 Z;
drhba212562004-01-08 02:17:31 +0000579 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000580 p->validTZ = 0;
581 p->validJD = 0;
582 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000583 Z = ((p->iJD + 129600000)/86400000) % 7;
drh7014aff2003-11-01 01:53:53 +0000584 if( Z>n ) Z -= 7;
drh85f477a2008-06-12 16:35:38 +0000585 p->iJD += (n - Z)*86400000;
drhba212562004-01-08 02:17:31 +0000586 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000587 rc = 0;
588 }
589 break;
590 }
591 case 's': {
592 /*
593 ** start of TTTTT
594 **
595 ** Move the date backwards to the beginning of the current day,
596 ** or month or year.
597 */
598 if( strncmp(z, "start of ", 9)!=0 ) break;
drh4d5b8362004-01-17 01:16:21 +0000599 z += 9;
drh7014aff2003-11-01 01:53:53 +0000600 computeYMD(p);
601 p->validHMS = 1;
602 p->h = p->m = 0;
603 p->s = 0.0;
604 p->validTZ = 0;
605 p->validJD = 0;
drh4d5b8362004-01-17 01:16:21 +0000606 if( strcmp(z,"month")==0 ){
drh7014aff2003-11-01 01:53:53 +0000607 p->D = 1;
608 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000609 }else if( strcmp(z,"year")==0 ){
drh7014aff2003-11-01 01:53:53 +0000610 computeYMD(p);
611 p->M = 1;
612 p->D = 1;
613 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000614 }else if( strcmp(z,"day")==0 ){
drh7014aff2003-11-01 01:53:53 +0000615 rc = 0;
616 }
617 break;
618 }
619 case '+':
620 case '-':
621 case '0':
622 case '1':
623 case '2':
624 case '3':
625 case '4':
626 case '5':
627 case '6':
628 case '7':
629 case '8':
630 case '9': {
drhc531a222009-01-30 17:27:44 +0000631 double rRounder;
drh7014aff2003-11-01 01:53:53 +0000632 n = getValue(z, &r);
drh05f7c192007-04-06 02:32:33 +0000633 assert( n>=1 );
drh33a9ad22004-02-29 00:40:32 +0000634 if( z[n]==':' ){
635 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
636 ** specified number of hours, minutes, seconds, and fractional seconds
637 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
638 ** omitted.
639 */
640 const char *z2 = z;
641 DateTime tx;
drh85f477a2008-06-12 16:35:38 +0000642 sqlite3_int64 day;
danielk197778ca0e72009-01-20 16:53:39 +0000643 if( !sqlite3Isdigit(*z2) ) z2++;
drh33a9ad22004-02-29 00:40:32 +0000644 memset(&tx, 0, sizeof(tx));
645 if( parseHhMmSs(z2, &tx) ) break;
646 computeJD(&tx);
drh85f477a2008-06-12 16:35:38 +0000647 tx.iJD -= 43200000;
648 day = tx.iJD/86400000;
649 tx.iJD -= day*86400000;
650 if( z[0]=='-' ) tx.iJD = -tx.iJD;
drh0d131ab2004-02-29 01:08:17 +0000651 computeJD(p);
652 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000653 p->iJD += tx.iJD;
drh33a9ad22004-02-29 00:40:32 +0000654 rc = 0;
655 break;
656 }
drh4d5b8362004-01-17 01:16:21 +0000657 z += n;
danielk197778ca0e72009-01-20 16:53:39 +0000658 while( sqlite3Isspace(*z) ) z++;
drhea678832008-12-10 19:26:22 +0000659 n = sqlite3Strlen30(z);
drh7014aff2003-11-01 01:53:53 +0000660 if( n>10 || n<3 ) break;
drh7014aff2003-11-01 01:53:53 +0000661 if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
662 computeJD(p);
663 rc = 0;
drhc531a222009-01-30 17:27:44 +0000664 rRounder = r<0 ? -0.5 : +0.5;
drh7014aff2003-11-01 01:53:53 +0000665 if( n==3 && strcmp(z,"day")==0 ){
drhc531a222009-01-30 17:27:44 +0000666 p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000667 }else if( n==4 && strcmp(z,"hour")==0 ){
drhc531a222009-01-30 17:27:44 +0000668 p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000669 }else if( n==6 && strcmp(z,"minute")==0 ){
drhc531a222009-01-30 17:27:44 +0000670 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000671 }else if( n==6 && strcmp(z,"second")==0 ){
drhc531a222009-01-30 17:27:44 +0000672 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000673 }else if( n==5 && strcmp(z,"month")==0 ){
674 int x, y;
drhba212562004-01-08 02:17:31 +0000675 computeYMD_HMS(p);
shaneaef3af52008-12-09 04:59:00 +0000676 p->M += (int)r;
drh7014aff2003-11-01 01:53:53 +0000677 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
678 p->Y += x;
679 p->M -= x*12;
680 p->validJD = 0;
681 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000682 y = (int)r;
drh7014aff2003-11-01 01:53:53 +0000683 if( y!=r ){
drhc531a222009-01-30 17:27:44 +0000684 p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000685 }
686 }else if( n==4 && strcmp(z,"year")==0 ){
drhc531a222009-01-30 17:27:44 +0000687 int y = (int)r;
drhba212562004-01-08 02:17:31 +0000688 computeYMD_HMS(p);
drhc531a222009-01-30 17:27:44 +0000689 p->Y += y;
drh7014aff2003-11-01 01:53:53 +0000690 p->validJD = 0;
691 computeJD(p);
drhc531a222009-01-30 17:27:44 +0000692 if( y!=r ){
693 p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
694 }
drh7014aff2003-11-01 01:53:53 +0000695 }else{
696 rc = 1;
697 }
drhba212562004-01-08 02:17:31 +0000698 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000699 break;
700 }
701 default: {
702 break;
703 }
704 }
705 return rc;
706}
707
708/*
709** Process time function arguments. argv[0] is a date-time stamp.
710** argv[1] and following are modifiers. Parse them all and write
711** the resulting time into the DateTime structure p. Return 0
712** on success and 1 if there are any errors.
drh008e4762008-01-17 22:27:53 +0000713**
714** If there are zero parameters (if even argv[0] is undefined)
715** then assume a default value of "now" for argv[0].
drh7014aff2003-11-01 01:53:53 +0000716*/
danielk1977fee2d252007-08-18 10:59:19 +0000717static int isDate(
718 sqlite3_context *context,
719 int argc,
720 sqlite3_value **argv,
721 DateTime *p
722){
drh7014aff2003-11-01 01:53:53 +0000723 int i;
drh7a521cf2007-04-25 18:23:52 +0000724 const unsigned char *z;
drh85f477a2008-06-12 16:35:38 +0000725 int eType;
drh3af5d682008-06-12 13:50:00 +0000726 memset(p, 0, sizeof(*p));
drh008e4762008-01-17 22:27:53 +0000727 if( argc==0 ){
drh3af5d682008-06-12 13:50:00 +0000728 setDateTimeToCurrent(context, p);
drh85f477a2008-06-12 16:35:38 +0000729 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
730 || eType==SQLITE_INTEGER ){
shaneaef3af52008-12-09 04:59:00 +0000731 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000732 p->validJD = 1;
drh008e4762008-01-17 22:27:53 +0000733 }else{
734 z = sqlite3_value_text(argv[0]);
drh3af5d682008-06-12 13:50:00 +0000735 if( !z || parseDateOrTime(context, (char*)z, p) ){
736 return 1;
737 }
drh7a521cf2007-04-25 18:23:52 +0000738 }
drh7014aff2003-11-01 01:53:53 +0000739 for(i=1; i<argc; i++){
drh7a521cf2007-04-25 18:23:52 +0000740 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
741 return 1;
742 }
drh7014aff2003-11-01 01:53:53 +0000743 }
744 return 0;
745}
746
747
748/*
749** The following routines implement the various date and time functions
750** of SQLite.
751*/
752
753/*
754** julianday( TIMESTRING, MOD, MOD, ...)
755**
756** Return the julian day number of the date specified in the arguments
757*/
drhf9b596e2004-05-26 16:54:42 +0000758static void juliandayFunc(
759 sqlite3_context *context,
760 int argc,
761 sqlite3_value **argv
762){
drh7014aff2003-11-01 01:53:53 +0000763 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000764 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000765 computeJD(&x);
drh85f477a2008-06-12 16:35:38 +0000766 sqlite3_result_double(context, x.iJD/86400000.0);
drh7014aff2003-11-01 01:53:53 +0000767 }
768}
769
770/*
771** datetime( TIMESTRING, MOD, MOD, ...)
772**
773** Return YYYY-MM-DD HH:MM:SS
774*/
drhf9b596e2004-05-26 16:54:42 +0000775static void datetimeFunc(
776 sqlite3_context *context,
777 int argc,
778 sqlite3_value **argv
779){
drh7014aff2003-11-01 01:53:53 +0000780 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000781 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000782 char zBuf[100];
drhba212562004-01-08 02:17:31 +0000783 computeYMD_HMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000784 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
785 x.Y, x.M, x.D, 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** time( TIMESTRING, MOD, MOD, ...)
792**
793** Return HH:MM:SS
794*/
drhf9b596e2004-05-26 16:54:42 +0000795static void timeFunc(
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 computeHMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000804 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
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** date( TIMESTRING, MOD, MOD, ...)
811**
812** Return YYYY-MM-DD
813*/
drhf9b596e2004-05-26 16:54:42 +0000814static void dateFunc(
815 sqlite3_context *context,
816 int argc,
817 sqlite3_value **argv
818){
drh7014aff2003-11-01 01:53:53 +0000819 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000820 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000821 char zBuf[100];
822 computeYMD(&x);
drh5bb3eb92007-05-04 13:15:55 +0000823 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
danielk1977d8123362004-06-12 09:25:12 +0000824 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000825 }
826}
827
828/*
829** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
830**
831** Return a string described by FORMAT. Conversions as follows:
832**
833** %d day of month
834** %f ** fractional seconds SS.SSS
835** %H hour 00-24
836** %j day of year 000-366
837** %J ** Julian day number
838** %m month 01-12
839** %M minute 00-59
840** %s seconds since 1970-01-01
841** %S seconds 00-59
842** %w day of week 0-6 sunday==0
843** %W week of year 00-53
844** %Y year 0000-9999
845** %% %
846*/
drhf9b596e2004-05-26 16:54:42 +0000847static void strftimeFunc(
848 sqlite3_context *context,
849 int argc,
850 sqlite3_value **argv
851){
drh7014aff2003-11-01 01:53:53 +0000852 DateTime x;
drha0206bc2007-05-08 15:15:02 +0000853 u64 n;
shaneaef3af52008-12-09 04:59:00 +0000854 size_t i,j;
drh7014aff2003-11-01 01:53:53 +0000855 char *z;
drh633e6d52008-07-28 19:34:53 +0000856 sqlite3 *db;
drh2646da72005-12-09 20:02:05 +0000857 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
drh7014aff2003-11-01 01:53:53 +0000858 char zBuf[100];
danielk1977fee2d252007-08-18 10:59:19 +0000859 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
drh633e6d52008-07-28 19:34:53 +0000860 db = sqlite3_context_db_handle(context);
drh7014aff2003-11-01 01:53:53 +0000861 for(i=0, n=1; zFmt[i]; i++, n++){
862 if( zFmt[i]=='%' ){
863 switch( zFmt[i+1] ){
864 case 'd':
865 case 'H':
866 case 'm':
867 case 'M':
868 case 'S':
869 case 'W':
870 n++;
871 /* fall thru */
872 case 'w':
873 case '%':
874 break;
875 case 'f':
876 n += 8;
877 break;
878 case 'j':
879 n += 3;
880 break;
881 case 'Y':
882 n += 8;
883 break;
884 case 's':
885 case 'J':
886 n += 50;
887 break;
888 default:
889 return; /* ERROR. return a NULL */
890 }
891 i++;
892 }
893 }
drh67110022009-01-28 02:55:28 +0000894 testcase( n==sizeof(zBuf)-1 );
895 testcase( n==sizeof(zBuf) );
896 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
897 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
drh7014aff2003-11-01 01:53:53 +0000898 if( n<sizeof(zBuf) ){
899 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000900 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha0206bc2007-05-08 15:15:02 +0000901 sqlite3_result_error_toobig(context);
902 return;
drh7014aff2003-11-01 01:53:53 +0000903 }else{
shaneaef3af52008-12-09 04:59:00 +0000904 z = sqlite3DbMallocRaw(db, (int)n);
drh3334e942008-01-17 20:26:46 +0000905 if( z==0 ){
906 sqlite3_result_error_nomem(context);
907 return;
908 }
drh7014aff2003-11-01 01:53:53 +0000909 }
910 computeJD(&x);
drhba212562004-01-08 02:17:31 +0000911 computeYMD_HMS(&x);
drh7014aff2003-11-01 01:53:53 +0000912 for(i=j=0; zFmt[i]; i++){
913 if( zFmt[i]!='%' ){
914 z[j++] = zFmt[i];
915 }else{
916 i++;
917 switch( zFmt[i] ){
drh5bb3eb92007-05-04 13:15:55 +0000918 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000919 case 'f': {
drhb1f1e6e2006-09-25 18:01:31 +0000920 double s = x.s;
921 if( s>59.999 ) s = 59.999;
drh2ecad3b2007-03-29 17:57:21 +0000922 sqlite3_snprintf(7, &z[j],"%06.3f", s);
drhea678832008-12-10 19:26:22 +0000923 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000924 break;
925 }
drh5bb3eb92007-05-04 13:15:55 +0000926 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000927 case 'W': /* Fall thru */
928 case 'j': {
danielk1977f0113002006-01-24 12:09:17 +0000929 int nDay; /* Number of days since 1st day of year */
drh7014aff2003-11-01 01:53:53 +0000930 DateTime y = x;
931 y.validJD = 0;
932 y.M = 1;
933 y.D = 1;
934 computeJD(&y);
shaneaef3af52008-12-09 04:59:00 +0000935 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
drh7014aff2003-11-01 01:53:53 +0000936 if( zFmt[i]=='W' ){
drh1020d492004-07-18 22:22:43 +0000937 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
shaneaef3af52008-12-09 04:59:00 +0000938 wd = (int)(((x.iJD+43200000)/86400000)%7);
drh5bb3eb92007-05-04 13:15:55 +0000939 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
drh7014aff2003-11-01 01:53:53 +0000940 j += 2;
941 }else{
drh5bb3eb92007-05-04 13:15:55 +0000942 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
drh7014aff2003-11-01 01:53:53 +0000943 j += 3;
944 }
945 break;
946 }
drh5bb3eb92007-05-04 13:15:55 +0000947 case 'J': {
drh85f477a2008-06-12 16:35:38 +0000948 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
drhea678832008-12-10 19:26:22 +0000949 j+=sqlite3Strlen30(&z[j]);
drh5bb3eb92007-05-04 13:15:55 +0000950 break;
951 }
952 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
953 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000954 case 's': {
drh6eb41522009-04-01 20:44:13 +0000955 sqlite3_snprintf(30,&z[j],"%lld",
drh07758962009-04-03 12:04:36 +0000956 (i64)(x.iJD/1000 - 21086676*(i64)10000));
drhea678832008-12-10 19:26:22 +0000957 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000958 break;
959 }
drh5bb3eb92007-05-04 13:15:55 +0000960 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
drhea678832008-12-10 19:26:22 +0000961 case 'w': {
962 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
963 break;
964 }
965 case 'Y': {
966 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
967 break;
968 }
drh008e4762008-01-17 22:27:53 +0000969 default: z[j++] = '%'; break;
drh7014aff2003-11-01 01:53:53 +0000970 }
971 }
972 }
973 z[j] = 0;
drh3334e942008-01-17 20:26:46 +0000974 sqlite3_result_text(context, z, -1,
drh633e6d52008-07-28 19:34:53 +0000975 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
drh7014aff2003-11-01 01:53:53 +0000976}
977
danielk19777977a172004-11-09 12:44:37 +0000978/*
979** current_time()
980**
981** This function returns the same value as time('now').
982*/
983static void ctimeFunc(
984 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +0000985 int NotUsed,
986 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +0000987){
danielk197762c14b32008-11-19 09:05:26 +0000988 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +0000989 timeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000990}
drh7014aff2003-11-01 01:53:53 +0000991
danielk19777977a172004-11-09 12:44:37 +0000992/*
993** current_date()
994**
995** This function returns the same value as date('now').
996*/
997static void cdateFunc(
998 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +0000999 int NotUsed,
1000 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001001){
danielk197762c14b32008-11-19 09:05:26 +00001002 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001003 dateFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001004}
1005
1006/*
1007** current_timestamp()
1008**
1009** This function returns the same value as datetime('now').
1010*/
1011static void ctimestampFunc(
1012 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001013 int NotUsed,
1014 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001015){
danielk197762c14b32008-11-19 09:05:26 +00001016 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001017 datetimeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001018}
drh7014aff2003-11-01 01:53:53 +00001019#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1020
danielk1977752e6792004-11-09 16:13:33 +00001021#ifdef SQLITE_OMIT_DATETIME_FUNCS
1022/*
1023** If the library is compiled to omit the full-scale date and time
1024** handling (to get a smaller binary), the following minimal version
1025** of the functions current_time(), current_date() and current_timestamp()
1026** are included instead. This is to support column declarations that
1027** include "DEFAULT CURRENT_TIME" etc.
1028**
danielk19772df9fab2004-11-11 01:50:30 +00001029** This function uses the C-library functions time(), gmtime()
danielk1977752e6792004-11-09 16:13:33 +00001030** and strftime(). The format string to pass to strftime() is supplied
1031** as the user-data for the function.
1032*/
danielk1977752e6792004-11-09 16:13:33 +00001033static void currentTimeFunc(
1034 sqlite3_context *context,
1035 int argc,
1036 sqlite3_value **argv
1037){
1038 time_t t;
1039 char *zFormat = (char *)sqlite3_user_data(context);
drhfa4a4b92008-03-19 21:45:51 +00001040 sqlite3 *db;
drh8257f0c2008-03-19 20:18:27 +00001041 double rT;
danielk1977752e6792004-11-09 16:13:33 +00001042 char zBuf[20];
danielk1977752e6792004-11-09 16:13:33 +00001043
shanefbd60f82009-02-04 03:59:25 +00001044 UNUSED_PARAMETER(argc);
1045 UNUSED_PARAMETER(argv);
1046
drhfa4a4b92008-03-19 21:45:51 +00001047 db = sqlite3_context_db_handle(context);
1048 sqlite3OsCurrentTime(db->pVfs, &rT);
shanefbd60f82009-02-04 03:59:25 +00001049#ifndef SQLITE_OMIT_FLOATING_POINT
drh8257f0c2008-03-19 20:18:27 +00001050 t = 86400.0*(rT - 2440587.5) + 0.5;
shanefbd60f82009-02-04 03:59:25 +00001051#else
1052 /* without floating point support, rT will have
1053 ** already lost fractional day precision.
1054 */
1055 t = 86400 * (rT - 2440587) - 43200;
1056#endif
drh87595762006-09-08 12:49:43 +00001057#ifdef HAVE_GMTIME_R
1058 {
1059 struct tm sNow;
1060 gmtime_r(&t, &sNow);
1061 strftime(zBuf, 20, zFormat, &sNow);
1062 }
1063#else
1064 {
1065 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +00001066 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001067 pTm = gmtime(&t);
1068 strftime(zBuf, 20, zFormat, pTm);
danielk197759f8c082008-06-18 17:09:10 +00001069 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001070 }
1071#endif
danielk1977e6efa742004-11-10 11:55:10 +00001072
danielk1977752e6792004-11-09 16:13:33 +00001073 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1074}
1075#endif
1076
drh7014aff2003-11-01 01:53:53 +00001077/*
1078** This function registered all of the above C functions as SQL
1079** functions. This should be the only routine in this file with
1080** external linkage.
1081*/
drh777c5382008-08-21 20:21:34 +00001082void sqlite3RegisterDateTimeFunctions(void){
danielk1977075c23a2008-09-01 18:34:20 +00001083 static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
drhfd1f3942004-07-20 00:39:14 +00001084#ifndef SQLITE_OMIT_DATETIME_FUNCS
drh777c5382008-08-21 20:21:34 +00001085 FUNCTION(julianday, -1, 0, 0, juliandayFunc ),
1086 FUNCTION(date, -1, 0, 0, dateFunc ),
1087 FUNCTION(time, -1, 0, 0, timeFunc ),
1088 FUNCTION(datetime, -1, 0, 0, datetimeFunc ),
1089 FUNCTION(strftime, -1, 0, 0, strftimeFunc ),
1090 FUNCTION(current_time, 0, 0, 0, ctimeFunc ),
1091 FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
1092 FUNCTION(current_date, 0, 0, 0, cdateFunc ),
danielk1977752e6792004-11-09 16:13:33 +00001093#else
drh21717ed2008-10-13 15:35:08 +00001094 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
drh2b1e6902010-01-12 19:28:20 +00001095 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
1096 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
drh777c5382008-08-21 20:21:34 +00001097#endif
danielk1977752e6792004-11-09 16:13:33 +00001098 };
1099 int i;
danielk1977075c23a2008-09-01 18:34:20 +00001100 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
drh106cee52008-09-03 17:11:16 +00001101 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
danielk1977752e6792004-11-09 16:13:33 +00001102
drh777c5382008-08-21 20:21:34 +00001103 for(i=0; i<ArraySize(aDateTimeFuncs); i++){
danielk1977075c23a2008-09-01 18:34:20 +00001104 sqlite3FuncDefInsert(pHash, &aFunc[i]);
danielk1977752e6792004-11-09 16:13:33 +00001105 }
drh7014aff2003-11-01 01:53:53 +00001106}