blob: a1768040fe2cabd998456c71d972be0985974e20 [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**
drh7fee3602009-04-16 12:58:03 +000019** $Id: date.c,v 1.106 2009/04/16 12:58:03 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 <stdlib.h>
50#include <assert.h>
drh7091cb02003-12-23 16:22:18 +000051#include <time.h>
drh7014aff2003-11-01 01:53:53 +000052
drh4bc05852004-02-10 13:19:35 +000053#ifndef SQLITE_OMIT_DATETIME_FUNCS
54
drh7014aff2003-11-01 01:53:53 +000055/*
shaneb8109ad2008-05-27 19:49:21 +000056** On recent Windows platforms, the localtime_s() function is available
57** as part of the "Secure CRT". It is essentially equivalent to
58** localtime_r() available under most POSIX platforms, except that the
59** order of the parameters is reversed.
60**
61** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
62**
63** If the user has not indicated to use localtime_r() or localtime_s()
64** already, check for an MSVC build environment that provides
65** localtime_s().
66*/
67#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
68 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
69#define HAVE_LOCALTIME_S 1
70#endif
71
72/*
drh7014aff2003-11-01 01:53:53 +000073** A structure for holding a single date and time.
74*/
75typedef struct DateTime DateTime;
76struct DateTime {
drh85f477a2008-06-12 16:35:38 +000077 sqlite3_int64 iJD; /* The julian day number times 86400000 */
78 int Y, M, D; /* Year, month, and day */
79 int h, m; /* Hour and minutes */
80 int tz; /* Timezone offset in minutes */
81 double s; /* Seconds */
shaneaef3af52008-12-09 04:59:00 +000082 char validYMD; /* True (1) if Y,M,D are valid */
83 char validHMS; /* True (1) if h,m,s are valid */
84 char validJD; /* True (1) if iJD is valid */
85 char validTZ; /* True (1) if tz is valid */
drh7014aff2003-11-01 01:53:53 +000086};
87
88
89/*
drheb9a9e82004-02-22 17:49:32 +000090** Convert zDate into one or more integers. Additional arguments
91** come in groups of 5 as follows:
92**
93** N number of digits in the integer
94** min minimum allowed value of the integer
95** max maximum allowed value of the integer
96** nextC first character after the integer
97** pVal where to write the integers value.
98**
99** Conversions continue until one with nextC==0 is encountered.
100** The function returns the number of successful conversions.
drh7014aff2003-11-01 01:53:53 +0000101*/
drheb9a9e82004-02-22 17:49:32 +0000102static int getDigits(const char *zDate, ...){
103 va_list ap;
104 int val;
105 int N;
106 int min;
107 int max;
108 int nextC;
109 int *pVal;
110 int cnt = 0;
111 va_start(ap, zDate);
112 do{
113 N = va_arg(ap, int);
114 min = va_arg(ap, int);
115 max = va_arg(ap, int);
116 nextC = va_arg(ap, int);
117 pVal = va_arg(ap, int*);
118 val = 0;
119 while( N-- ){
danielk197778ca0e72009-01-20 16:53:39 +0000120 if( !sqlite3Isdigit(*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000121 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000122 }
123 val = val*10 + *zDate - '0';
124 zDate++;
125 }
126 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000127 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000128 }
129 *pVal = val;
drh7014aff2003-11-01 01:53:53 +0000130 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000131 cnt++;
132 }while( nextC );
drh029b44b2006-01-15 00:13:15 +0000133end_getDigits:
drh15b9a152006-01-31 20:49:13 +0000134 va_end(ap);
drheb9a9e82004-02-22 17:49:32 +0000135 return cnt;
drh7014aff2003-11-01 01:53:53 +0000136}
137
138/*
139** Read text from z[] and convert into a floating point number. Return
140** the number of digits converted.
141*/
drh487e2622005-06-25 18:42:14 +0000142#define getValue sqlite3AtoF
drh7014aff2003-11-01 01:53:53 +0000143
144/*
145** Parse a timezone extension on the end of a date-time.
146** The extension is of the form:
147**
148** (+/-)HH:MM
149**
drh1cfdc902008-02-21 20:40:43 +0000150** Or the "zulu" notation:
151**
152** Z
153**
drh7014aff2003-11-01 01:53:53 +0000154** If the parse is successful, write the number of minutes
drh1cfdc902008-02-21 20:40:43 +0000155** of change in p->tz and return 0. If a parser error occurs,
156** return non-zero.
drh7014aff2003-11-01 01:53:53 +0000157**
158** A missing specifier is not considered an error.
159*/
160static int parseTimezone(const char *zDate, DateTime *p){
161 int sgn = 0;
162 int nHr, nMn;
drh1cfdc902008-02-21 20:40:43 +0000163 int c;
danielk197778ca0e72009-01-20 16:53:39 +0000164 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000165 p->tz = 0;
drh1cfdc902008-02-21 20:40:43 +0000166 c = *zDate;
167 if( c=='-' ){
drh7014aff2003-11-01 01:53:53 +0000168 sgn = -1;
drh1cfdc902008-02-21 20:40:43 +0000169 }else if( c=='+' ){
drh7014aff2003-11-01 01:53:53 +0000170 sgn = +1;
drh1cfdc902008-02-21 20:40:43 +0000171 }else if( c=='Z' || c=='z' ){
172 zDate++;
173 goto zulu_time;
drh7014aff2003-11-01 01:53:53 +0000174 }else{
drh1cfdc902008-02-21 20:40:43 +0000175 return c!=0;
drh7014aff2003-11-01 01:53:53 +0000176 }
177 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000178 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
179 return 1;
180 }
181 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000182 p->tz = sgn*(nMn + nHr*60);
drh1cfdc902008-02-21 20:40:43 +0000183zulu_time:
danielk197778ca0e72009-01-20 16:53:39 +0000184 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000185 return *zDate!=0;
186}
187
188/*
189** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
190** The HH, MM, and SS must each be exactly 2 digits. The
191** fractional seconds FFFF can be one or more digits.
192**
193** Return 1 if there is a parsing error and 0 on success.
194*/
195static int parseHhMmSs(const char *zDate, DateTime *p){
196 int h, m, s;
197 double ms = 0.0;
drheb9a9e82004-02-22 17:49:32 +0000198 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
199 return 1;
200 }
201 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000202 if( *zDate==':' ){
drheb9a9e82004-02-22 17:49:32 +0000203 zDate++;
204 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
205 return 1;
206 }
207 zDate += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000208 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
drh7014aff2003-11-01 01:53:53 +0000209 double rScale = 1.0;
210 zDate++;
danielk197778ca0e72009-01-20 16:53:39 +0000211 while( sqlite3Isdigit(*zDate) ){
drh7014aff2003-11-01 01:53:53 +0000212 ms = ms*10.0 + *zDate - '0';
213 rScale *= 10.0;
214 zDate++;
215 }
216 ms /= rScale;
217 }
218 }else{
219 s = 0;
220 }
221 p->validJD = 0;
222 p->validHMS = 1;
223 p->h = h;
224 p->m = m;
225 p->s = s + ms;
226 if( parseTimezone(zDate, p) ) return 1;
shaneaef3af52008-12-09 04:59:00 +0000227 p->validTZ = (p->tz!=0)?1:0;
drh7014aff2003-11-01 01:53:53 +0000228 return 0;
229}
230
231/*
232** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
233** that the YYYY-MM-DD is according to the Gregorian calendar.
234**
235** Reference: Meeus page 61
236*/
237static void computeJD(DateTime *p){
238 int Y, M, D, A, B, X1, X2;
239
240 if( p->validJD ) return;
241 if( p->validYMD ){
242 Y = p->Y;
243 M = p->M;
244 D = p->D;
245 }else{
drhba212562004-01-08 02:17:31 +0000246 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
drh7014aff2003-11-01 01:53:53 +0000247 M = 1;
248 D = 1;
249 }
250 if( M<=2 ){
251 Y--;
252 M += 12;
253 }
254 A = Y/100;
255 B = 2 - A + (A/4);
shaneaef3af52008-12-09 04:59:00 +0000256 X1 = 36525*(Y+4716)/100;
257 X2 = 306001*(M+1)/10000;
258 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
drh7014aff2003-11-01 01:53:53 +0000259 p->validJD = 1;
drh7014aff2003-11-01 01:53:53 +0000260 if( p->validHMS ){
shaneaef3af52008-12-09 04:59:00 +0000261 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
drh7014aff2003-11-01 01:53:53 +0000262 if( p->validTZ ){
drh85f477a2008-06-12 16:35:38 +0000263 p->iJD -= p->tz*60000;
drhf11c34d2006-09-08 12:27:36 +0000264 p->validYMD = 0;
drh7014aff2003-11-01 01:53:53 +0000265 p->validHMS = 0;
266 p->validTZ = 0;
267 }
268 }
269}
270
271/*
272** Parse dates of the form
273**
274** YYYY-MM-DD HH:MM:SS.FFF
275** YYYY-MM-DD HH:MM:SS
276** YYYY-MM-DD HH:MM
277** YYYY-MM-DD
278**
279** Write the result into the DateTime structure and return 0
280** on success and 1 if the input string is not a well-formed
281** date.
282*/
283static int parseYyyyMmDd(const char *zDate, DateTime *p){
drh8eb2cce2004-02-21 03:28:18 +0000284 int Y, M, D, neg;
drh7014aff2003-11-01 01:53:53 +0000285
drh8eb2cce2004-02-21 03:28:18 +0000286 if( zDate[0]=='-' ){
287 zDate++;
288 neg = 1;
289 }else{
290 neg = 0;
291 }
drheb9a9e82004-02-22 17:49:32 +0000292 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
293 return 1;
294 }
295 zDate += 10;
danielk197778ca0e72009-01-20 16:53:39 +0000296 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
drheb9a9e82004-02-22 17:49:32 +0000297 if( parseHhMmSs(zDate, p)==0 ){
298 /* We got the time */
drh7014aff2003-11-01 01:53:53 +0000299 }else if( *zDate==0 ){
300 p->validHMS = 0;
301 }else{
302 return 1;
303 }
304 p->validJD = 0;
305 p->validYMD = 1;
drh8eb2cce2004-02-21 03:28:18 +0000306 p->Y = neg ? -Y : Y;
drh7014aff2003-11-01 01:53:53 +0000307 p->M = M;
308 p->D = D;
309 if( p->validTZ ){
310 computeJD(p);
311 }
312 return 0;
313}
314
315/*
drh3af5d682008-06-12 13:50:00 +0000316** Set the time to the current time reported by the VFS
317*/
318static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
319 double r;
320 sqlite3 *db = sqlite3_context_db_handle(context);
321 sqlite3OsCurrentTime(db->pVfs, &r);
drh85f477a2008-06-12 16:35:38 +0000322 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000323 p->validJD = 1;
324}
325
326/*
drh7014aff2003-11-01 01:53:53 +0000327** Attempt to parse the given string into a Julian Day Number. Return
328** the number of errors.
329**
330** The following are acceptable forms for the input string:
331**
332** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
333** DDDD.DD
334** now
335**
336** In the first form, the +/-HH:MM is always optional. The fractional
337** seconds extension (the ".FFF") is optional. The seconds portion
338** (":SS.FFF") is option. The year and date can be omitted as long
339** as there is a time string. The time string can be omitted as long
340** as there is a year and date.
341*/
danielk1977fee2d252007-08-18 10:59:19 +0000342static int parseDateOrTime(
343 sqlite3_context *context,
344 const char *zDate,
345 DateTime *p
346){
drh8eb2cce2004-02-21 03:28:18 +0000347 if( parseYyyyMmDd(zDate,p)==0 ){
drh7014aff2003-11-01 01:53:53 +0000348 return 0;
drh8eb2cce2004-02-21 03:28:18 +0000349 }else if( parseHhMmSs(zDate, p)==0 ){
350 return 0;
danielk19774adee202004-05-08 08:23:19 +0000351 }else if( sqlite3StrICmp(zDate,"now")==0){
drh3af5d682008-06-12 13:50:00 +0000352 setDateTimeToCurrent(context, p);
drh018d1a42005-01-15 01:52:31 +0000353 return 0;
danielk1977dc8453f2004-06-12 00:42:34 +0000354 }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
drh85f477a2008-06-12 16:35:38 +0000355 double r;
356 getValue(zDate, &r);
357 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh7014aff2003-11-01 01:53:53 +0000358 p->validJD = 1;
359 return 0;
360 }
361 return 1;
362}
363
364/*
365** Compute the Year, Month, and Day from the julian day number.
366*/
367static void computeYMD(DateTime *p){
368 int Z, A, B, C, D, E, X1;
369 if( p->validYMD ) return;
drh33a9ad22004-02-29 00:40:32 +0000370 if( !p->validJD ){
371 p->Y = 2000;
372 p->M = 1;
373 p->D = 1;
374 }else{
shaneaef3af52008-12-09 04:59:00 +0000375 Z = (int)((p->iJD + 43200000)/86400000);
376 A = (int)((Z - 1867216.25)/36524.25);
drh33a9ad22004-02-29 00:40:32 +0000377 A = Z + 1 + A - (A/4);
378 B = A + 1524;
shaneaef3af52008-12-09 04:59:00 +0000379 C = (int)((B - 122.1)/365.25);
380 D = (36525*C)/100;
381 E = (int)((B-D)/30.6001);
382 X1 = (int)(30.6001*E);
drh33a9ad22004-02-29 00:40:32 +0000383 p->D = B - D - X1;
384 p->M = E<14 ? E-1 : E-13;
385 p->Y = p->M>2 ? C - 4716 : C - 4715;
386 }
drh7014aff2003-11-01 01:53:53 +0000387 p->validYMD = 1;
388}
389
390/*
391** Compute the Hour, Minute, and Seconds from the julian day number.
392*/
393static void computeHMS(DateTime *p){
drh85f477a2008-06-12 16:35:38 +0000394 int s;
drh7014aff2003-11-01 01:53:53 +0000395 if( p->validHMS ) return;
drhf11c34d2006-09-08 12:27:36 +0000396 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000397 s = (int)((p->iJD + 43200000) % 86400000);
drh85f477a2008-06-12 16:35:38 +0000398 p->s = s/1000.0;
shaneaef3af52008-12-09 04:59:00 +0000399 s = (int)p->s;
drh7014aff2003-11-01 01:53:53 +0000400 p->s -= s;
401 p->h = s/3600;
402 s -= p->h*3600;
403 p->m = s/60;
404 p->s += s - p->m*60;
405 p->validHMS = 1;
406}
407
408/*
drhba212562004-01-08 02:17:31 +0000409** Compute both YMD and HMS
410*/
411static void computeYMD_HMS(DateTime *p){
412 computeYMD(p);
413 computeHMS(p);
414}
415
416/*
417** Clear the YMD and HMS and the TZ
418*/
419static void clearYMD_HMS_TZ(DateTime *p){
420 p->validYMD = 0;
421 p->validHMS = 0;
422 p->validTZ = 0;
423}
424
drh66147c92008-06-12 12:51:37 +0000425#ifndef SQLITE_OMIT_LOCALTIME
drhba212562004-01-08 02:17:31 +0000426/*
drh85f477a2008-06-12 16:35:38 +0000427** Compute the difference (in milliseconds)
428** between localtime and UTC (a.k.a. GMT)
drh7091cb02003-12-23 16:22:18 +0000429** for the time value p where p is in UTC.
430*/
shaneaef3af52008-12-09 04:59:00 +0000431static sqlite3_int64 localtimeOffset(DateTime *p){
drh7091cb02003-12-23 16:22:18 +0000432 DateTime x, y;
433 time_t t;
drh7091cb02003-12-23 16:22:18 +0000434 x = *p;
drhba212562004-01-08 02:17:31 +0000435 computeYMD_HMS(&x);
drh7091cb02003-12-23 16:22:18 +0000436 if( x.Y<1971 || x.Y>=2038 ){
437 x.Y = 2000;
438 x.M = 1;
439 x.D = 1;
440 x.h = 0;
441 x.m = 0;
442 x.s = 0.0;
443 } else {
shaneaef3af52008-12-09 04:59:00 +0000444 int s = (int)(x.s + 0.5);
drh7091cb02003-12-23 16:22:18 +0000445 x.s = s;
446 }
447 x.tz = 0;
448 x.validJD = 0;
449 computeJD(&x);
drh7f1e8a02008-12-20 13:18:50 +0000450 t = x.iJD/1000 - 21086676*(i64)10000;
drh87595762006-09-08 12:49:43 +0000451#ifdef HAVE_LOCALTIME_R
452 {
453 struct tm sLocal;
454 localtime_r(&t, &sLocal);
455 y.Y = sLocal.tm_year + 1900;
456 y.M = sLocal.tm_mon + 1;
457 y.D = sLocal.tm_mday;
458 y.h = sLocal.tm_hour;
459 y.m = sLocal.tm_min;
460 y.s = sLocal.tm_sec;
461 }
shaneb8109ad2008-05-27 19:49:21 +0000462#elif defined(HAVE_LOCALTIME_S)
463 {
464 struct tm sLocal;
465 localtime_s(&sLocal, &t);
466 y.Y = sLocal.tm_year + 1900;
467 y.M = sLocal.tm_mon + 1;
468 y.D = sLocal.tm_mday;
469 y.h = sLocal.tm_hour;
470 y.m = sLocal.tm_min;
471 y.s = sLocal.tm_sec;
472 }
drh87595762006-09-08 12:49:43 +0000473#else
474 {
475 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +0000476 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000477 pTm = localtime(&t);
478 y.Y = pTm->tm_year + 1900;
479 y.M = pTm->tm_mon + 1;
480 y.D = pTm->tm_mday;
481 y.h = pTm->tm_hour;
482 y.m = pTm->tm_min;
483 y.s = pTm->tm_sec;
danielk197759f8c082008-06-18 17:09:10 +0000484 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000485 }
486#endif
drh7091cb02003-12-23 16:22:18 +0000487 y.validYMD = 1;
488 y.validHMS = 1;
489 y.validJD = 0;
490 y.validTZ = 0;
491 computeJD(&y);
drh85f477a2008-06-12 16:35:38 +0000492 return y.iJD - x.iJD;
drh7091cb02003-12-23 16:22:18 +0000493}
drh66147c92008-06-12 12:51:37 +0000494#endif /* SQLITE_OMIT_LOCALTIME */
drh7091cb02003-12-23 16:22:18 +0000495
496/*
drh7014aff2003-11-01 01:53:53 +0000497** Process a modifier to a date-time stamp. The modifiers are
498** as follows:
499**
500** NNN days
501** NNN hours
502** NNN minutes
503** NNN.NNNN seconds
504** NNN months
505** NNN years
506** start of month
507** start of year
508** start of week
509** start of day
510** weekday N
511** unixepoch
drh7091cb02003-12-23 16:22:18 +0000512** localtime
513** utc
drh7014aff2003-11-01 01:53:53 +0000514**
515** Return 0 on success and 1 if there is any kind of error.
516*/
517static int parseModifier(const char *zMod, DateTime *p){
518 int rc = 1;
519 int n;
520 double r;
drh4d5b8362004-01-17 01:16:21 +0000521 char *z, zBuf[30];
522 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000523 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
drh1bd10f82008-12-10 21:19:56 +0000524 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
drh7014aff2003-11-01 01:53:53 +0000525 }
526 z[n] = 0;
527 switch( z[0] ){
drh66147c92008-06-12 12:51:37 +0000528#ifndef SQLITE_OMIT_LOCALTIME
drh7091cb02003-12-23 16:22:18 +0000529 case 'l': {
530 /* localtime
531 **
532 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
533 ** show local time.
534 */
535 if( strcmp(z, "localtime")==0 ){
536 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000537 p->iJD += localtimeOffset(p);
drhba212562004-01-08 02:17:31 +0000538 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000539 rc = 0;
540 }
541 break;
542 }
drh66147c92008-06-12 12:51:37 +0000543#endif
drh7014aff2003-11-01 01:53:53 +0000544 case 'u': {
545 /*
546 ** unixepoch
547 **
drh85f477a2008-06-12 16:35:38 +0000548 ** Treat the current value of p->iJD as the number of
drh7014aff2003-11-01 01:53:53 +0000549 ** seconds since 1970. Convert to a real julian day number.
550 */
551 if( strcmp(z, "unixepoch")==0 && p->validJD ){
drh7fee3602009-04-16 12:58:03 +0000552 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
drhba212562004-01-08 02:17:31 +0000553 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000554 rc = 0;
drh66cccd92008-07-25 16:39:24 +0000555 }
556#ifndef SQLITE_OMIT_LOCALTIME
557 else if( strcmp(z, "utc")==0 ){
shaneaef3af52008-12-09 04:59:00 +0000558 sqlite3_int64 c1;
drh7091cb02003-12-23 16:22:18 +0000559 computeJD(p);
560 c1 = localtimeOffset(p);
drh85f477a2008-06-12 16:35:38 +0000561 p->iJD -= c1;
drhba212562004-01-08 02:17:31 +0000562 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000563 p->iJD += c1 - localtimeOffset(p);
drh7091cb02003-12-23 16:22:18 +0000564 rc = 0;
drh7014aff2003-11-01 01:53:53 +0000565 }
drh66cccd92008-07-25 16:39:24 +0000566#endif
drh7014aff2003-11-01 01:53:53 +0000567 break;
568 }
569 case 'w': {
570 /*
571 ** weekday N
572 **
drh181fc992004-08-17 10:42:54 +0000573 ** Move the date to the same time on the next occurrence of
drh7014aff2003-11-01 01:53:53 +0000574 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
drhc5dd9fa2004-01-07 03:29:16 +0000575 ** date is already on the appropriate weekday, this is a no-op.
drh7014aff2003-11-01 01:53:53 +0000576 */
577 if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
shaneaef3af52008-12-09 04:59:00 +0000578 && (n=(int)r)==r && n>=0 && r<7 ){
drh85f477a2008-06-12 16:35:38 +0000579 sqlite3_int64 Z;
drhba212562004-01-08 02:17:31 +0000580 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000581 p->validTZ = 0;
582 p->validJD = 0;
583 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000584 Z = ((p->iJD + 129600000)/86400000) % 7;
drh7014aff2003-11-01 01:53:53 +0000585 if( Z>n ) Z -= 7;
drh85f477a2008-06-12 16:35:38 +0000586 p->iJD += (n - Z)*86400000;
drhba212562004-01-08 02:17:31 +0000587 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000588 rc = 0;
589 }
590 break;
591 }
592 case 's': {
593 /*
594 ** start of TTTTT
595 **
596 ** Move the date backwards to the beginning of the current day,
597 ** or month or year.
598 */
599 if( strncmp(z, "start of ", 9)!=0 ) break;
drh4d5b8362004-01-17 01:16:21 +0000600 z += 9;
drh7014aff2003-11-01 01:53:53 +0000601 computeYMD(p);
602 p->validHMS = 1;
603 p->h = p->m = 0;
604 p->s = 0.0;
605 p->validTZ = 0;
606 p->validJD = 0;
drh4d5b8362004-01-17 01:16:21 +0000607 if( strcmp(z,"month")==0 ){
drh7014aff2003-11-01 01:53:53 +0000608 p->D = 1;
609 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000610 }else if( strcmp(z,"year")==0 ){
drh7014aff2003-11-01 01:53:53 +0000611 computeYMD(p);
612 p->M = 1;
613 p->D = 1;
614 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000615 }else if( strcmp(z,"day")==0 ){
drh7014aff2003-11-01 01:53:53 +0000616 rc = 0;
617 }
618 break;
619 }
620 case '+':
621 case '-':
622 case '0':
623 case '1':
624 case '2':
625 case '3':
626 case '4':
627 case '5':
628 case '6':
629 case '7':
630 case '8':
631 case '9': {
drhc531a222009-01-30 17:27:44 +0000632 double rRounder;
drh7014aff2003-11-01 01:53:53 +0000633 n = getValue(z, &r);
drh05f7c192007-04-06 02:32:33 +0000634 assert( n>=1 );
drh33a9ad22004-02-29 00:40:32 +0000635 if( z[n]==':' ){
636 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
637 ** specified number of hours, minutes, seconds, and fractional seconds
638 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
639 ** omitted.
640 */
641 const char *z2 = z;
642 DateTime tx;
drh85f477a2008-06-12 16:35:38 +0000643 sqlite3_int64 day;
danielk197778ca0e72009-01-20 16:53:39 +0000644 if( !sqlite3Isdigit(*z2) ) z2++;
drh33a9ad22004-02-29 00:40:32 +0000645 memset(&tx, 0, sizeof(tx));
646 if( parseHhMmSs(z2, &tx) ) break;
647 computeJD(&tx);
drh85f477a2008-06-12 16:35:38 +0000648 tx.iJD -= 43200000;
649 day = tx.iJD/86400000;
650 tx.iJD -= day*86400000;
651 if( z[0]=='-' ) tx.iJD = -tx.iJD;
drh0d131ab2004-02-29 01:08:17 +0000652 computeJD(p);
653 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000654 p->iJD += tx.iJD;
drh33a9ad22004-02-29 00:40:32 +0000655 rc = 0;
656 break;
657 }
drh4d5b8362004-01-17 01:16:21 +0000658 z += n;
danielk197778ca0e72009-01-20 16:53:39 +0000659 while( sqlite3Isspace(*z) ) z++;
drhea678832008-12-10 19:26:22 +0000660 n = sqlite3Strlen30(z);
drh7014aff2003-11-01 01:53:53 +0000661 if( n>10 || n<3 ) break;
drh7014aff2003-11-01 01:53:53 +0000662 if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
663 computeJD(p);
664 rc = 0;
drhc531a222009-01-30 17:27:44 +0000665 rRounder = r<0 ? -0.5 : +0.5;
drh7014aff2003-11-01 01:53:53 +0000666 if( n==3 && strcmp(z,"day")==0 ){
drhc531a222009-01-30 17:27:44 +0000667 p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000668 }else if( n==4 && strcmp(z,"hour")==0 ){
drhc531a222009-01-30 17:27:44 +0000669 p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000670 }else if( n==6 && strcmp(z,"minute")==0 ){
drhc531a222009-01-30 17:27:44 +0000671 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000672 }else if( n==6 && strcmp(z,"second")==0 ){
drhc531a222009-01-30 17:27:44 +0000673 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000674 }else if( n==5 && strcmp(z,"month")==0 ){
675 int x, y;
drhba212562004-01-08 02:17:31 +0000676 computeYMD_HMS(p);
shaneaef3af52008-12-09 04:59:00 +0000677 p->M += (int)r;
drh7014aff2003-11-01 01:53:53 +0000678 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
679 p->Y += x;
680 p->M -= x*12;
681 p->validJD = 0;
682 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000683 y = (int)r;
drh7014aff2003-11-01 01:53:53 +0000684 if( y!=r ){
drhc531a222009-01-30 17:27:44 +0000685 p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000686 }
687 }else if( n==4 && strcmp(z,"year")==0 ){
drhc531a222009-01-30 17:27:44 +0000688 int y = (int)r;
drhba212562004-01-08 02:17:31 +0000689 computeYMD_HMS(p);
drhc531a222009-01-30 17:27:44 +0000690 p->Y += y;
drh7014aff2003-11-01 01:53:53 +0000691 p->validJD = 0;
692 computeJD(p);
drhc531a222009-01-30 17:27:44 +0000693 if( y!=r ){
694 p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
695 }
drh7014aff2003-11-01 01:53:53 +0000696 }else{
697 rc = 1;
698 }
drhba212562004-01-08 02:17:31 +0000699 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000700 break;
701 }
702 default: {
703 break;
704 }
705 }
706 return rc;
707}
708
709/*
710** Process time function arguments. argv[0] is a date-time stamp.
711** argv[1] and following are modifiers. Parse them all and write
712** the resulting time into the DateTime structure p. Return 0
713** on success and 1 if there are any errors.
drh008e4762008-01-17 22:27:53 +0000714**
715** If there are zero parameters (if even argv[0] is undefined)
716** then assume a default value of "now" for argv[0].
drh7014aff2003-11-01 01:53:53 +0000717*/
danielk1977fee2d252007-08-18 10:59:19 +0000718static int isDate(
719 sqlite3_context *context,
720 int argc,
721 sqlite3_value **argv,
722 DateTime *p
723){
drh7014aff2003-11-01 01:53:53 +0000724 int i;
drh7a521cf2007-04-25 18:23:52 +0000725 const unsigned char *z;
drh85f477a2008-06-12 16:35:38 +0000726 int eType;
drh3af5d682008-06-12 13:50:00 +0000727 memset(p, 0, sizeof(*p));
drh008e4762008-01-17 22:27:53 +0000728 if( argc==0 ){
drh3af5d682008-06-12 13:50:00 +0000729 setDateTimeToCurrent(context, p);
drh85f477a2008-06-12 16:35:38 +0000730 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
731 || eType==SQLITE_INTEGER ){
shaneaef3af52008-12-09 04:59:00 +0000732 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000733 p->validJD = 1;
drh008e4762008-01-17 22:27:53 +0000734 }else{
735 z = sqlite3_value_text(argv[0]);
drh3af5d682008-06-12 13:50:00 +0000736 if( !z || parseDateOrTime(context, (char*)z, p) ){
737 return 1;
738 }
drh7a521cf2007-04-25 18:23:52 +0000739 }
drh7014aff2003-11-01 01:53:53 +0000740 for(i=1; i<argc; i++){
drh7a521cf2007-04-25 18:23:52 +0000741 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
742 return 1;
743 }
drh7014aff2003-11-01 01:53:53 +0000744 }
745 return 0;
746}
747
748
749/*
750** The following routines implement the various date and time functions
751** of SQLite.
752*/
753
754/*
755** julianday( TIMESTRING, MOD, MOD, ...)
756**
757** Return the julian day number of the date specified in the arguments
758*/
drhf9b596e2004-05-26 16:54:42 +0000759static void juliandayFunc(
760 sqlite3_context *context,
761 int argc,
762 sqlite3_value **argv
763){
drh7014aff2003-11-01 01:53:53 +0000764 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000765 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000766 computeJD(&x);
drh85f477a2008-06-12 16:35:38 +0000767 sqlite3_result_double(context, x.iJD/86400000.0);
drh7014aff2003-11-01 01:53:53 +0000768 }
769}
770
771/*
772** datetime( TIMESTRING, MOD, MOD, ...)
773**
774** Return YYYY-MM-DD HH:MM:SS
775*/
drhf9b596e2004-05-26 16:54:42 +0000776static void datetimeFunc(
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];
drhba212562004-01-08 02:17:31 +0000784 computeYMD_HMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000785 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
786 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
danielk1977d8123362004-06-12 09:25:12 +0000787 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000788 }
789}
790
791/*
792** time( TIMESTRING, MOD, MOD, ...)
793**
794** Return HH:MM:SS
795*/
drhf9b596e2004-05-26 16:54:42 +0000796static void timeFunc(
797 sqlite3_context *context,
798 int argc,
799 sqlite3_value **argv
800){
drh7014aff2003-11-01 01:53:53 +0000801 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000802 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000803 char zBuf[100];
804 computeHMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000805 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
danielk1977d8123362004-06-12 09:25:12 +0000806 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000807 }
808}
809
810/*
811** date( TIMESTRING, MOD, MOD, ...)
812**
813** Return YYYY-MM-DD
814*/
drhf9b596e2004-05-26 16:54:42 +0000815static void dateFunc(
816 sqlite3_context *context,
817 int argc,
818 sqlite3_value **argv
819){
drh7014aff2003-11-01 01:53:53 +0000820 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000821 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000822 char zBuf[100];
823 computeYMD(&x);
drh5bb3eb92007-05-04 13:15:55 +0000824 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
danielk1977d8123362004-06-12 09:25:12 +0000825 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000826 }
827}
828
829/*
830** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
831**
832** Return a string described by FORMAT. Conversions as follows:
833**
834** %d day of month
835** %f ** fractional seconds SS.SSS
836** %H hour 00-24
837** %j day of year 000-366
838** %J ** Julian day number
839** %m month 01-12
840** %M minute 00-59
841** %s seconds since 1970-01-01
842** %S seconds 00-59
843** %w day of week 0-6 sunday==0
844** %W week of year 00-53
845** %Y year 0000-9999
846** %% %
847*/
drhf9b596e2004-05-26 16:54:42 +0000848static void strftimeFunc(
849 sqlite3_context *context,
850 int argc,
851 sqlite3_value **argv
852){
drh7014aff2003-11-01 01:53:53 +0000853 DateTime x;
drha0206bc2007-05-08 15:15:02 +0000854 u64 n;
shaneaef3af52008-12-09 04:59:00 +0000855 size_t i,j;
drh7014aff2003-11-01 01:53:53 +0000856 char *z;
drh633e6d52008-07-28 19:34:53 +0000857 sqlite3 *db;
drh2646da72005-12-09 20:02:05 +0000858 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
drh7014aff2003-11-01 01:53:53 +0000859 char zBuf[100];
danielk1977fee2d252007-08-18 10:59:19 +0000860 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
drh633e6d52008-07-28 19:34:53 +0000861 db = sqlite3_context_db_handle(context);
drh7014aff2003-11-01 01:53:53 +0000862 for(i=0, n=1; zFmt[i]; i++, n++){
863 if( zFmt[i]=='%' ){
864 switch( zFmt[i+1] ){
865 case 'd':
866 case 'H':
867 case 'm':
868 case 'M':
869 case 'S':
870 case 'W':
871 n++;
872 /* fall thru */
873 case 'w':
874 case '%':
875 break;
876 case 'f':
877 n += 8;
878 break;
879 case 'j':
880 n += 3;
881 break;
882 case 'Y':
883 n += 8;
884 break;
885 case 's':
886 case 'J':
887 n += 50;
888 break;
889 default:
890 return; /* ERROR. return a NULL */
891 }
892 i++;
893 }
894 }
drh67110022009-01-28 02:55:28 +0000895 testcase( n==sizeof(zBuf)-1 );
896 testcase( n==sizeof(zBuf) );
897 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
898 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
drh7014aff2003-11-01 01:53:53 +0000899 if( n<sizeof(zBuf) ){
900 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000901 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha0206bc2007-05-08 15:15:02 +0000902 sqlite3_result_error_toobig(context);
903 return;
drh7014aff2003-11-01 01:53:53 +0000904 }else{
shaneaef3af52008-12-09 04:59:00 +0000905 z = sqlite3DbMallocRaw(db, (int)n);
drh3334e942008-01-17 20:26:46 +0000906 if( z==0 ){
907 sqlite3_result_error_nomem(context);
908 return;
909 }
drh7014aff2003-11-01 01:53:53 +0000910 }
911 computeJD(&x);
drhba212562004-01-08 02:17:31 +0000912 computeYMD_HMS(&x);
drh7014aff2003-11-01 01:53:53 +0000913 for(i=j=0; zFmt[i]; i++){
914 if( zFmt[i]!='%' ){
915 z[j++] = zFmt[i];
916 }else{
917 i++;
918 switch( zFmt[i] ){
drh5bb3eb92007-05-04 13:15:55 +0000919 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000920 case 'f': {
drhb1f1e6e2006-09-25 18:01:31 +0000921 double s = x.s;
922 if( s>59.999 ) s = 59.999;
drh2ecad3b2007-03-29 17:57:21 +0000923 sqlite3_snprintf(7, &z[j],"%06.3f", s);
drhea678832008-12-10 19:26:22 +0000924 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000925 break;
926 }
drh5bb3eb92007-05-04 13:15:55 +0000927 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000928 case 'W': /* Fall thru */
929 case 'j': {
danielk1977f0113002006-01-24 12:09:17 +0000930 int nDay; /* Number of days since 1st day of year */
drh7014aff2003-11-01 01:53:53 +0000931 DateTime y = x;
932 y.validJD = 0;
933 y.M = 1;
934 y.D = 1;
935 computeJD(&y);
shaneaef3af52008-12-09 04:59:00 +0000936 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
drh7014aff2003-11-01 01:53:53 +0000937 if( zFmt[i]=='W' ){
drh1020d492004-07-18 22:22:43 +0000938 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
shaneaef3af52008-12-09 04:59:00 +0000939 wd = (int)(((x.iJD+43200000)/86400000)%7);
drh5bb3eb92007-05-04 13:15:55 +0000940 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
drh7014aff2003-11-01 01:53:53 +0000941 j += 2;
942 }else{
drh5bb3eb92007-05-04 13:15:55 +0000943 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
drh7014aff2003-11-01 01:53:53 +0000944 j += 3;
945 }
946 break;
947 }
drh5bb3eb92007-05-04 13:15:55 +0000948 case 'J': {
drh85f477a2008-06-12 16:35:38 +0000949 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
drhea678832008-12-10 19:26:22 +0000950 j+=sqlite3Strlen30(&z[j]);
drh5bb3eb92007-05-04 13:15:55 +0000951 break;
952 }
953 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
954 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000955 case 's': {
drh6eb41522009-04-01 20:44:13 +0000956 sqlite3_snprintf(30,&z[j],"%lld",
drh07758962009-04-03 12:04:36 +0000957 (i64)(x.iJD/1000 - 21086676*(i64)10000));
drhea678832008-12-10 19:26:22 +0000958 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000959 break;
960 }
drh5bb3eb92007-05-04 13:15:55 +0000961 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
drhea678832008-12-10 19:26:22 +0000962 case 'w': {
963 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
964 break;
965 }
966 case 'Y': {
967 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
968 break;
969 }
drh008e4762008-01-17 22:27:53 +0000970 default: z[j++] = '%'; break;
drh7014aff2003-11-01 01:53:53 +0000971 }
972 }
973 }
974 z[j] = 0;
drh3334e942008-01-17 20:26:46 +0000975 sqlite3_result_text(context, z, -1,
drh633e6d52008-07-28 19:34:53 +0000976 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
drh7014aff2003-11-01 01:53:53 +0000977}
978
danielk19777977a172004-11-09 12:44:37 +0000979/*
980** current_time()
981**
982** This function returns the same value as time('now').
983*/
984static void ctimeFunc(
985 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +0000986 int NotUsed,
987 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +0000988){
danielk197762c14b32008-11-19 09:05:26 +0000989 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +0000990 timeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000991}
drh7014aff2003-11-01 01:53:53 +0000992
danielk19777977a172004-11-09 12:44:37 +0000993/*
994** current_date()
995**
996** This function returns the same value as date('now').
997*/
998static void cdateFunc(
999 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001000 int NotUsed,
1001 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001002){
danielk197762c14b32008-11-19 09:05:26 +00001003 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001004 dateFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001005}
1006
1007/*
1008** current_timestamp()
1009**
1010** This function returns the same value as datetime('now').
1011*/
1012static void ctimestampFunc(
1013 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001014 int NotUsed,
1015 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001016){
danielk197762c14b32008-11-19 09:05:26 +00001017 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001018 datetimeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001019}
drh7014aff2003-11-01 01:53:53 +00001020#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1021
danielk1977752e6792004-11-09 16:13:33 +00001022#ifdef SQLITE_OMIT_DATETIME_FUNCS
1023/*
1024** If the library is compiled to omit the full-scale date and time
1025** handling (to get a smaller binary), the following minimal version
1026** of the functions current_time(), current_date() and current_timestamp()
1027** are included instead. This is to support column declarations that
1028** include "DEFAULT CURRENT_TIME" etc.
1029**
danielk19772df9fab2004-11-11 01:50:30 +00001030** This function uses the C-library functions time(), gmtime()
danielk1977752e6792004-11-09 16:13:33 +00001031** and strftime(). The format string to pass to strftime() is supplied
1032** as the user-data for the function.
1033*/
danielk1977752e6792004-11-09 16:13:33 +00001034static void currentTimeFunc(
1035 sqlite3_context *context,
1036 int argc,
1037 sqlite3_value **argv
1038){
1039 time_t t;
1040 char *zFormat = (char *)sqlite3_user_data(context);
drhfa4a4b92008-03-19 21:45:51 +00001041 sqlite3 *db;
drh8257f0c2008-03-19 20:18:27 +00001042 double rT;
danielk1977752e6792004-11-09 16:13:33 +00001043 char zBuf[20];
danielk1977752e6792004-11-09 16:13:33 +00001044
shanefbd60f82009-02-04 03:59:25 +00001045 UNUSED_PARAMETER(argc);
1046 UNUSED_PARAMETER(argv);
1047
drhfa4a4b92008-03-19 21:45:51 +00001048 db = sqlite3_context_db_handle(context);
1049 sqlite3OsCurrentTime(db->pVfs, &rT);
shanefbd60f82009-02-04 03:59:25 +00001050#ifndef SQLITE_OMIT_FLOATING_POINT
drh8257f0c2008-03-19 20:18:27 +00001051 t = 86400.0*(rT - 2440587.5) + 0.5;
shanefbd60f82009-02-04 03:59:25 +00001052#else
1053 /* without floating point support, rT will have
1054 ** already lost fractional day precision.
1055 */
1056 t = 86400 * (rT - 2440587) - 43200;
1057#endif
drh87595762006-09-08 12:49:43 +00001058#ifdef HAVE_GMTIME_R
1059 {
1060 struct tm sNow;
1061 gmtime_r(&t, &sNow);
1062 strftime(zBuf, 20, zFormat, &sNow);
1063 }
1064#else
1065 {
1066 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +00001067 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001068 pTm = gmtime(&t);
1069 strftime(zBuf, 20, zFormat, pTm);
danielk197759f8c082008-06-18 17:09:10 +00001070 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001071 }
1072#endif
danielk1977e6efa742004-11-10 11:55:10 +00001073
danielk1977752e6792004-11-09 16:13:33 +00001074 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1075}
1076#endif
1077
drh7014aff2003-11-01 01:53:53 +00001078/*
1079** This function registered all of the above C functions as SQL
1080** functions. This should be the only routine in this file with
1081** external linkage.
1082*/
drh777c5382008-08-21 20:21:34 +00001083void sqlite3RegisterDateTimeFunctions(void){
danielk1977075c23a2008-09-01 18:34:20 +00001084 static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
drhfd1f3942004-07-20 00:39:14 +00001085#ifndef SQLITE_OMIT_DATETIME_FUNCS
drh777c5382008-08-21 20:21:34 +00001086 FUNCTION(julianday, -1, 0, 0, juliandayFunc ),
1087 FUNCTION(date, -1, 0, 0, dateFunc ),
1088 FUNCTION(time, -1, 0, 0, timeFunc ),
1089 FUNCTION(datetime, -1, 0, 0, datetimeFunc ),
1090 FUNCTION(strftime, -1, 0, 0, strftimeFunc ),
1091 FUNCTION(current_time, 0, 0, 0, ctimeFunc ),
1092 FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
1093 FUNCTION(current_date, 0, 0, 0, cdateFunc ),
danielk1977752e6792004-11-09 16:13:33 +00001094#else
drh21717ed2008-10-13 15:35:08 +00001095 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
1096 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d", 0, currentTimeFunc),
1097 STR_FUNCTION(current_date, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
drh777c5382008-08-21 20:21:34 +00001098#endif
danielk1977752e6792004-11-09 16:13:33 +00001099 };
1100 int i;
danielk1977075c23a2008-09-01 18:34:20 +00001101 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
drh106cee52008-09-03 17:11:16 +00001102 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
danielk1977752e6792004-11-09 16:13:33 +00001103
drh777c5382008-08-21 20:21:34 +00001104 for(i=0; i<ArraySize(aDateTimeFuncs); i++){
danielk1977075c23a2008-09-01 18:34:20 +00001105 sqlite3FuncDefInsert(pHash, &aFunc[i]);
danielk1977752e6792004-11-09 16:13:33 +00001106 }
drh7014aff2003-11-01 01:53:53 +00001107}