blob: 68f33d7b2281a6fb4c704acb8c06bd5916505a34 [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**
drh777c5382008-08-21 20:21:34 +000019** $Id: date.c,v 1.88 2008/08/21 20:21:35 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 {
drh85f477a2008-06-12 16:35:38 +000078 sqlite3_int64 iJD; /* The julian day number times 86400000 */
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 iJD is valid */
86 char validTZ; /* True if tz is valid */
drh7014aff2003-11-01 01:53:53 +000087};
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);
drh85f477a2008-06-12 16:35:38 +0000259 p->iJD = (X1 + X2 + D + B - 1524.5)*86400000;
drh7014aff2003-11-01 01:53:53 +0000260 p->validJD = 1;
drh7014aff2003-11-01 01:53:53 +0000261 if( p->validHMS ){
drh85f477a2008-06-12 16:35:38 +0000262 p->iJD += p->h*3600000 + p->m*60000 + p->s*1000;
drh7014aff2003-11-01 01:53:53 +0000263 if( p->validTZ ){
drh85f477a2008-06-12 16:35:38 +0000264 p->iJD -= p->tz*60000;
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/*
drh3af5d682008-06-12 13:50:00 +0000317** Set the time to the current time reported by the VFS
318*/
319static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
320 double r;
321 sqlite3 *db = sqlite3_context_db_handle(context);
322 sqlite3OsCurrentTime(db->pVfs, &r);
drh85f477a2008-06-12 16:35:38 +0000323 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000324 p->validJD = 1;
325}
326
327/*
drh7014aff2003-11-01 01:53:53 +0000328** Attempt to parse the given string into a Julian Day Number. Return
329** the number of errors.
330**
331** The following are acceptable forms for the input string:
332**
333** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
334** DDDD.DD
335** now
336**
337** In the first form, the +/-HH:MM is always optional. The fractional
338** seconds extension (the ".FFF") is optional. The seconds portion
339** (":SS.FFF") is option. The year and date can be omitted as long
340** as there is a time string. The time string can be omitted as long
341** as there is a year and date.
342*/
danielk1977fee2d252007-08-18 10:59:19 +0000343static int parseDateOrTime(
344 sqlite3_context *context,
345 const char *zDate,
346 DateTime *p
347){
drh8eb2cce2004-02-21 03:28:18 +0000348 if( parseYyyyMmDd(zDate,p)==0 ){
drh7014aff2003-11-01 01:53:53 +0000349 return 0;
drh8eb2cce2004-02-21 03:28:18 +0000350 }else if( parseHhMmSs(zDate, p)==0 ){
351 return 0;
danielk19774adee202004-05-08 08:23:19 +0000352 }else if( sqlite3StrICmp(zDate,"now")==0){
drh3af5d682008-06-12 13:50:00 +0000353 setDateTimeToCurrent(context, p);
drh018d1a42005-01-15 01:52:31 +0000354 return 0;
danielk1977dc8453f2004-06-12 00:42:34 +0000355 }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
drh85f477a2008-06-12 16:35:38 +0000356 double r;
357 getValue(zDate, &r);
358 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh7014aff2003-11-01 01:53:53 +0000359 p->validJD = 1;
360 return 0;
361 }
362 return 1;
363}
364
365/*
366** Compute the Year, Month, and Day from the julian day number.
367*/
368static void computeYMD(DateTime *p){
369 int Z, A, B, C, D, E, X1;
370 if( p->validYMD ) return;
drh33a9ad22004-02-29 00:40:32 +0000371 if( !p->validJD ){
372 p->Y = 2000;
373 p->M = 1;
374 p->D = 1;
375 }else{
drh85f477a2008-06-12 16:35:38 +0000376 Z = (p->iJD + 43200000)/86400000;
drh33a9ad22004-02-29 00:40:32 +0000377 A = (Z - 1867216.25)/36524.25;
378 A = Z + 1 + A - (A/4);
379 B = A + 1524;
380 C = (B - 122.1)/365.25;
381 D = 365.25*C;
382 E = (B-D)/30.6001;
383 X1 = 30.6001*E;
384 p->D = B - D - X1;
385 p->M = E<14 ? E-1 : E-13;
386 p->Y = p->M>2 ? C - 4716 : C - 4715;
387 }
drh7014aff2003-11-01 01:53:53 +0000388 p->validYMD = 1;
389}
390
391/*
392** Compute the Hour, Minute, and Seconds from the julian day number.
393*/
394static void computeHMS(DateTime *p){
drh85f477a2008-06-12 16:35:38 +0000395 int s;
drh7014aff2003-11-01 01:53:53 +0000396 if( p->validHMS ) return;
drhf11c34d2006-09-08 12:27:36 +0000397 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000398 s = (p->iJD + 43200000) % 86400000;
399 p->s = s/1000.0;
drh7014aff2003-11-01 01:53:53 +0000400 s = p->s;
401 p->s -= s;
402 p->h = s/3600;
403 s -= p->h*3600;
404 p->m = s/60;
405 p->s += s - p->m*60;
406 p->validHMS = 1;
407}
408
409/*
drhba212562004-01-08 02:17:31 +0000410** Compute both YMD and HMS
411*/
412static void computeYMD_HMS(DateTime *p){
413 computeYMD(p);
414 computeHMS(p);
415}
416
417/*
418** Clear the YMD and HMS and the TZ
419*/
420static void clearYMD_HMS_TZ(DateTime *p){
421 p->validYMD = 0;
422 p->validHMS = 0;
423 p->validTZ = 0;
424}
425
drh66147c92008-06-12 12:51:37 +0000426#ifndef SQLITE_OMIT_LOCALTIME
drhba212562004-01-08 02:17:31 +0000427/*
drh85f477a2008-06-12 16:35:38 +0000428** Compute the difference (in milliseconds)
429** between localtime and UTC (a.k.a. GMT)
drh7091cb02003-12-23 16:22:18 +0000430** for the time value p where p is in UTC.
431*/
drh85f477a2008-06-12 16:35:38 +0000432static int localtimeOffset(DateTime *p){
drh7091cb02003-12-23 16:22:18 +0000433 DateTime x, y;
434 time_t t;
drh7091cb02003-12-23 16:22:18 +0000435 x = *p;
drhba212562004-01-08 02:17:31 +0000436 computeYMD_HMS(&x);
drh7091cb02003-12-23 16:22:18 +0000437 if( x.Y<1971 || x.Y>=2038 ){
438 x.Y = 2000;
439 x.M = 1;
440 x.D = 1;
441 x.h = 0;
442 x.m = 0;
443 x.s = 0.0;
444 } else {
445 int s = x.s + 0.5;
446 x.s = s;
447 }
448 x.tz = 0;
449 x.validJD = 0;
450 computeJD(&x);
drh85f477a2008-06-12 16:35:38 +0000451 t = x.iJD/1000 - 2440587.5*86400.0;
drh87595762006-09-08 12:49:43 +0000452#ifdef HAVE_LOCALTIME_R
453 {
454 struct tm sLocal;
455 localtime_r(&t, &sLocal);
456 y.Y = sLocal.tm_year + 1900;
457 y.M = sLocal.tm_mon + 1;
458 y.D = sLocal.tm_mday;
459 y.h = sLocal.tm_hour;
460 y.m = sLocal.tm_min;
461 y.s = sLocal.tm_sec;
462 }
shaneb8109ad2008-05-27 19:49:21 +0000463#elif defined(HAVE_LOCALTIME_S)
464 {
465 struct tm sLocal;
466 localtime_s(&sLocal, &t);
467 y.Y = sLocal.tm_year + 1900;
468 y.M = sLocal.tm_mon + 1;
469 y.D = sLocal.tm_mday;
470 y.h = sLocal.tm_hour;
471 y.m = sLocal.tm_min;
472 y.s = sLocal.tm_sec;
473 }
drh87595762006-09-08 12:49:43 +0000474#else
475 {
476 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +0000477 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000478 pTm = localtime(&t);
479 y.Y = pTm->tm_year + 1900;
480 y.M = pTm->tm_mon + 1;
481 y.D = pTm->tm_mday;
482 y.h = pTm->tm_hour;
483 y.m = pTm->tm_min;
484 y.s = pTm->tm_sec;
danielk197759f8c082008-06-18 17:09:10 +0000485 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +0000486 }
487#endif
drh7091cb02003-12-23 16:22:18 +0000488 y.validYMD = 1;
489 y.validHMS = 1;
490 y.validJD = 0;
491 y.validTZ = 0;
492 computeJD(&y);
drh85f477a2008-06-12 16:35:38 +0000493 return y.iJD - x.iJD;
drh7091cb02003-12-23 16:22:18 +0000494}
drh66147c92008-06-12 12:51:37 +0000495#endif /* SQLITE_OMIT_LOCALTIME */
drh7091cb02003-12-23 16:22:18 +0000496
497/*
drh7014aff2003-11-01 01:53:53 +0000498** Process a modifier to a date-time stamp. The modifiers are
499** as follows:
500**
501** NNN days
502** NNN hours
503** NNN minutes
504** NNN.NNNN seconds
505** NNN months
506** NNN years
507** start of month
508** start of year
509** start of week
510** start of day
511** weekday N
512** unixepoch
drh7091cb02003-12-23 16:22:18 +0000513** localtime
514** utc
drh7014aff2003-11-01 01:53:53 +0000515**
516** Return 0 on success and 1 if there is any kind of error.
517*/
518static int parseModifier(const char *zMod, DateTime *p){
519 int rc = 1;
520 int n;
521 double r;
drh4d5b8362004-01-17 01:16:21 +0000522 char *z, zBuf[30];
523 z = zBuf;
524 for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
drh7014aff2003-11-01 01:53:53 +0000525 z[n] = tolower(zMod[n]);
526 }
527 z[n] = 0;
528 switch( z[0] ){
drh66147c92008-06-12 12:51:37 +0000529#ifndef SQLITE_OMIT_LOCALTIME
drh7091cb02003-12-23 16:22:18 +0000530 case 'l': {
531 /* localtime
532 **
533 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
534 ** show local time.
535 */
536 if( strcmp(z, "localtime")==0 ){
537 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000538 p->iJD += localtimeOffset(p);
drhba212562004-01-08 02:17:31 +0000539 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000540 rc = 0;
541 }
542 break;
543 }
drh66147c92008-06-12 12:51:37 +0000544#endif
drh7014aff2003-11-01 01:53:53 +0000545 case 'u': {
546 /*
547 ** unixepoch
548 **
drh85f477a2008-06-12 16:35:38 +0000549 ** Treat the current value of p->iJD as the number of
drh7014aff2003-11-01 01:53:53 +0000550 ** seconds since 1970. Convert to a real julian day number.
551 */
552 if( strcmp(z, "unixepoch")==0 && p->validJD ){
drh85f477a2008-06-12 16:35:38 +0000553 p->iJD = p->iJD/86400.0 + 2440587.5*86400000.0;
drhba212562004-01-08 02:17:31 +0000554 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000555 rc = 0;
drh66cccd92008-07-25 16:39:24 +0000556 }
557#ifndef SQLITE_OMIT_LOCALTIME
558 else if( strcmp(z, "utc")==0 ){
drh7091cb02003-12-23 16:22:18 +0000559 double c1;
560 computeJD(p);
561 c1 = localtimeOffset(p);
drh85f477a2008-06-12 16:35:38 +0000562 p->iJD -= c1;
drhba212562004-01-08 02:17:31 +0000563 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000564 p->iJD += c1 - localtimeOffset(p);
drh7091cb02003-12-23 16:22:18 +0000565 rc = 0;
drh7014aff2003-11-01 01:53:53 +0000566 }
drh66cccd92008-07-25 16:39:24 +0000567#endif
drh7014aff2003-11-01 01:53:53 +0000568 break;
569 }
570 case 'w': {
571 /*
572 ** weekday N
573 **
drh181fc992004-08-17 10:42:54 +0000574 ** Move the date to the same time on the next occurrence of
drh7014aff2003-11-01 01:53:53 +0000575 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
drhc5dd9fa2004-01-07 03:29:16 +0000576 ** date is already on the appropriate weekday, this is a no-op.
drh7014aff2003-11-01 01:53:53 +0000577 */
578 if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
579 && (n=r)==r && n>=0 && r<7 ){
drh85f477a2008-06-12 16:35:38 +0000580 sqlite3_int64 Z;
drhba212562004-01-08 02:17:31 +0000581 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000582 p->validTZ = 0;
583 p->validJD = 0;
584 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000585 Z = ((p->iJD + 129600000)/86400000) % 7;
drh7014aff2003-11-01 01:53:53 +0000586 if( Z>n ) Z -= 7;
drh85f477a2008-06-12 16:35:38 +0000587 p->iJD += (n - Z)*86400000;
drhba212562004-01-08 02:17:31 +0000588 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000589 rc = 0;
590 }
591 break;
592 }
593 case 's': {
594 /*
595 ** start of TTTTT
596 **
597 ** Move the date backwards to the beginning of the current day,
598 ** or month or year.
599 */
600 if( strncmp(z, "start of ", 9)!=0 ) break;
drh4d5b8362004-01-17 01:16:21 +0000601 z += 9;
drh7014aff2003-11-01 01:53:53 +0000602 computeYMD(p);
603 p->validHMS = 1;
604 p->h = p->m = 0;
605 p->s = 0.0;
606 p->validTZ = 0;
607 p->validJD = 0;
drh4d5b8362004-01-17 01:16:21 +0000608 if( strcmp(z,"month")==0 ){
drh7014aff2003-11-01 01:53:53 +0000609 p->D = 1;
610 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000611 }else if( strcmp(z,"year")==0 ){
drh7014aff2003-11-01 01:53:53 +0000612 computeYMD(p);
613 p->M = 1;
614 p->D = 1;
615 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000616 }else if( strcmp(z,"day")==0 ){
drh7014aff2003-11-01 01:53:53 +0000617 rc = 0;
618 }
619 break;
620 }
621 case '+':
622 case '-':
623 case '0':
624 case '1':
625 case '2':
626 case '3':
627 case '4':
628 case '5':
629 case '6':
630 case '7':
631 case '8':
632 case '9': {
633 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;
drh4c755c02004-08-08 20:22:17 +0000644 if( !isdigit(*(u8*)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;
drh4c755c02004-08-08 20:22:17 +0000659 while( isspace(*(u8*)z) ) z++;
drh4d5b8362004-01-17 01:16:21 +0000660 n = strlen(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;
665 if( n==3 && strcmp(z,"day")==0 ){
drh85f477a2008-06-12 16:35:38 +0000666 p->iJD += r*86400000.0 + 0.5;
drh7014aff2003-11-01 01:53:53 +0000667 }else if( n==4 && strcmp(z,"hour")==0 ){
drh85f477a2008-06-12 16:35:38 +0000668 p->iJD += r*(86400000.0/24.0) + 0.5;
drh7014aff2003-11-01 01:53:53 +0000669 }else if( n==6 && strcmp(z,"minute")==0 ){
drh85f477a2008-06-12 16:35:38 +0000670 p->iJD += r*(86400000.0/(24.0*60.0)) + 0.5;
drh7014aff2003-11-01 01:53:53 +0000671 }else if( n==6 && strcmp(z,"second")==0 ){
drh85f477a2008-06-12 16:35:38 +0000672 p->iJD += r*(86400000.0/(24.0*60.0*60.0)) + 0.5;
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);
drh7014aff2003-11-01 01:53:53 +0000676 p->M += r;
677 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);
682 y = r;
683 if( y!=r ){
drh85f477a2008-06-12 16:35:38 +0000684 p->iJD += (r - y)*30.0*86400000.0 + 0.5;
drh7014aff2003-11-01 01:53:53 +0000685 }
686 }else if( n==4 && strcmp(z,"year")==0 ){
drhba212562004-01-08 02:17:31 +0000687 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000688 p->Y += r;
689 p->validJD = 0;
690 computeJD(p);
691 }else{
692 rc = 1;
693 }
drhba212562004-01-08 02:17:31 +0000694 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000695 break;
696 }
697 default: {
698 break;
699 }
700 }
701 return rc;
702}
703
704/*
705** Process time function arguments. argv[0] is a date-time stamp.
706** argv[1] and following are modifiers. Parse them all and write
707** the resulting time into the DateTime structure p. Return 0
708** on success and 1 if there are any errors.
drh008e4762008-01-17 22:27:53 +0000709**
710** If there are zero parameters (if even argv[0] is undefined)
711** then assume a default value of "now" for argv[0].
drh7014aff2003-11-01 01:53:53 +0000712*/
danielk1977fee2d252007-08-18 10:59:19 +0000713static int isDate(
714 sqlite3_context *context,
715 int argc,
716 sqlite3_value **argv,
717 DateTime *p
718){
drh7014aff2003-11-01 01:53:53 +0000719 int i;
drh7a521cf2007-04-25 18:23:52 +0000720 const unsigned char *z;
drh85f477a2008-06-12 16:35:38 +0000721 int eType;
drh3af5d682008-06-12 13:50:00 +0000722 memset(p, 0, sizeof(*p));
drh008e4762008-01-17 22:27:53 +0000723 if( argc==0 ){
drh3af5d682008-06-12 13:50:00 +0000724 setDateTimeToCurrent(context, p);
drh85f477a2008-06-12 16:35:38 +0000725 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
726 || eType==SQLITE_INTEGER ){
727 p->iJD = sqlite3_value_double(argv[0])*86400000.0 + 0.5;
drh3af5d682008-06-12 13:50:00 +0000728 p->validJD = 1;
drh008e4762008-01-17 22:27:53 +0000729 }else{
730 z = sqlite3_value_text(argv[0]);
drh3af5d682008-06-12 13:50:00 +0000731 if( !z || parseDateOrTime(context, (char*)z, p) ){
732 return 1;
733 }
drh7a521cf2007-04-25 18:23:52 +0000734 }
drh7014aff2003-11-01 01:53:53 +0000735 for(i=1; i<argc; i++){
drh7a521cf2007-04-25 18:23:52 +0000736 if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
737 return 1;
738 }
drh7014aff2003-11-01 01:53:53 +0000739 }
740 return 0;
741}
742
743
744/*
745** The following routines implement the various date and time functions
746** of SQLite.
747*/
748
749/*
750** julianday( TIMESTRING, MOD, MOD, ...)
751**
752** Return the julian day number of the date specified in the arguments
753*/
drhf9b596e2004-05-26 16:54:42 +0000754static void juliandayFunc(
755 sqlite3_context *context,
756 int argc,
757 sqlite3_value **argv
758){
drh7014aff2003-11-01 01:53:53 +0000759 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000760 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000761 computeJD(&x);
drh85f477a2008-06-12 16:35:38 +0000762 sqlite3_result_double(context, x.iJD/86400000.0);
drh7014aff2003-11-01 01:53:53 +0000763 }
764}
765
766/*
767** datetime( TIMESTRING, MOD, MOD, ...)
768**
769** Return YYYY-MM-DD HH:MM:SS
770*/
drhf9b596e2004-05-26 16:54:42 +0000771static void datetimeFunc(
772 sqlite3_context *context,
773 int argc,
774 sqlite3_value **argv
775){
drh7014aff2003-11-01 01:53:53 +0000776 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000777 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000778 char zBuf[100];
drhba212562004-01-08 02:17:31 +0000779 computeYMD_HMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000780 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
781 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
danielk1977d8123362004-06-12 09:25:12 +0000782 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000783 }
784}
785
786/*
787** time( TIMESTRING, MOD, MOD, ...)
788**
789** Return HH:MM:SS
790*/
drhf9b596e2004-05-26 16:54:42 +0000791static void timeFunc(
792 sqlite3_context *context,
793 int argc,
794 sqlite3_value **argv
795){
drh7014aff2003-11-01 01:53:53 +0000796 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000797 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000798 char zBuf[100];
799 computeHMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000800 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
danielk1977d8123362004-06-12 09:25:12 +0000801 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000802 }
803}
804
805/*
806** date( TIMESTRING, MOD, MOD, ...)
807**
808** Return YYYY-MM-DD
809*/
drhf9b596e2004-05-26 16:54:42 +0000810static void dateFunc(
811 sqlite3_context *context,
812 int argc,
813 sqlite3_value **argv
814){
drh7014aff2003-11-01 01:53:53 +0000815 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000816 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000817 char zBuf[100];
818 computeYMD(&x);
drh5bb3eb92007-05-04 13:15:55 +0000819 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
danielk1977d8123362004-06-12 09:25:12 +0000820 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000821 }
822}
823
824/*
825** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
826**
827** Return a string described by FORMAT. Conversions as follows:
828**
829** %d day of month
830** %f ** fractional seconds SS.SSS
831** %H hour 00-24
832** %j day of year 000-366
833** %J ** Julian day number
834** %m month 01-12
835** %M minute 00-59
836** %s seconds since 1970-01-01
837** %S seconds 00-59
838** %w day of week 0-6 sunday==0
839** %W week of year 00-53
840** %Y year 0000-9999
841** %% %
842*/
drhf9b596e2004-05-26 16:54:42 +0000843static void strftimeFunc(
844 sqlite3_context *context,
845 int argc,
846 sqlite3_value **argv
847){
drh7014aff2003-11-01 01:53:53 +0000848 DateTime x;
drha0206bc2007-05-08 15:15:02 +0000849 u64 n;
850 int i, j;
drh7014aff2003-11-01 01:53:53 +0000851 char *z;
drh633e6d52008-07-28 19:34:53 +0000852 sqlite3 *db;
drh2646da72005-12-09 20:02:05 +0000853 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
drh7014aff2003-11-01 01:53:53 +0000854 char zBuf[100];
danielk1977fee2d252007-08-18 10:59:19 +0000855 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
drh633e6d52008-07-28 19:34:53 +0000856 db = sqlite3_context_db_handle(context);
drh7014aff2003-11-01 01:53:53 +0000857 for(i=0, n=1; zFmt[i]; i++, n++){
858 if( zFmt[i]=='%' ){
859 switch( zFmt[i+1] ){
860 case 'd':
861 case 'H':
862 case 'm':
863 case 'M':
864 case 'S':
865 case 'W':
866 n++;
867 /* fall thru */
868 case 'w':
869 case '%':
870 break;
871 case 'f':
872 n += 8;
873 break;
874 case 'j':
875 n += 3;
876 break;
877 case 'Y':
878 n += 8;
879 break;
880 case 's':
881 case 'J':
882 n += 50;
883 break;
884 default:
885 return; /* ERROR. return a NULL */
886 }
887 i++;
888 }
889 }
890 if( n<sizeof(zBuf) ){
891 z = zBuf;
drh633e6d52008-07-28 19:34:53 +0000892 }else if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha0206bc2007-05-08 15:15:02 +0000893 sqlite3_result_error_toobig(context);
894 return;
drh7014aff2003-11-01 01:53:53 +0000895 }else{
drh633e6d52008-07-28 19:34:53 +0000896 z = sqlite3DbMallocRaw(db, n);
drh3334e942008-01-17 20:26:46 +0000897 if( z==0 ){
898 sqlite3_result_error_nomem(context);
899 return;
900 }
drh7014aff2003-11-01 01:53:53 +0000901 }
902 computeJD(&x);
drhba212562004-01-08 02:17:31 +0000903 computeYMD_HMS(&x);
drh7014aff2003-11-01 01:53:53 +0000904 for(i=j=0; zFmt[i]; i++){
905 if( zFmt[i]!='%' ){
906 z[j++] = zFmt[i];
907 }else{
908 i++;
909 switch( zFmt[i] ){
drh5bb3eb92007-05-04 13:15:55 +0000910 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000911 case 'f': {
drhb1f1e6e2006-09-25 18:01:31 +0000912 double s = x.s;
913 if( s>59.999 ) s = 59.999;
drh2ecad3b2007-03-29 17:57:21 +0000914 sqlite3_snprintf(7, &z[j],"%06.3f", s);
drh7014aff2003-11-01 01:53:53 +0000915 j += strlen(&z[j]);
916 break;
917 }
drh5bb3eb92007-05-04 13:15:55 +0000918 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000919 case 'W': /* Fall thru */
920 case 'j': {
danielk1977f0113002006-01-24 12:09:17 +0000921 int nDay; /* Number of days since 1st day of year */
drh7014aff2003-11-01 01:53:53 +0000922 DateTime y = x;
923 y.validJD = 0;
924 y.M = 1;
925 y.D = 1;
926 computeJD(&y);
drh85f477a2008-06-12 16:35:38 +0000927 nDay = (x.iJD - y.iJD)/86400000.0 + 0.5;
drh7014aff2003-11-01 01:53:53 +0000928 if( zFmt[i]=='W' ){
drh1020d492004-07-18 22:22:43 +0000929 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
drh85f477a2008-06-12 16:35:38 +0000930 wd = ((x.iJD+43200000)/86400000) % 7;
drh5bb3eb92007-05-04 13:15:55 +0000931 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
drh7014aff2003-11-01 01:53:53 +0000932 j += 2;
933 }else{
drh5bb3eb92007-05-04 13:15:55 +0000934 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
drh7014aff2003-11-01 01:53:53 +0000935 j += 3;
936 }
937 break;
938 }
drh5bb3eb92007-05-04 13:15:55 +0000939 case 'J': {
drh85f477a2008-06-12 16:35:38 +0000940 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
drh5bb3eb92007-05-04 13:15:55 +0000941 j+=strlen(&z[j]);
942 break;
943 }
944 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
945 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000946 case 's': {
drh5bb3eb92007-05-04 13:15:55 +0000947 sqlite3_snprintf(30,&z[j],"%d",
drh85f477a2008-06-12 16:35:38 +0000948 (int)(x.iJD/1000.0 - 210866760000.0));
drh7014aff2003-11-01 01:53:53 +0000949 j += strlen(&z[j]);
950 break;
951 }
drh5bb3eb92007-05-04 13:15:55 +0000952 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
drh85f477a2008-06-12 16:35:38 +0000953 case 'w': z[j++] = (((x.iJD+129600000)/86400000) % 7) + '0'; break;
drh5bb3eb92007-05-04 13:15:55 +0000954 case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break;
drh008e4762008-01-17 22:27:53 +0000955 default: z[j++] = '%'; break;
drh7014aff2003-11-01 01:53:53 +0000956 }
957 }
958 }
959 z[j] = 0;
drh3334e942008-01-17 20:26:46 +0000960 sqlite3_result_text(context, z, -1,
drh633e6d52008-07-28 19:34:53 +0000961 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
drh7014aff2003-11-01 01:53:53 +0000962}
963
danielk19777977a172004-11-09 12:44:37 +0000964/*
965** current_time()
966**
967** This function returns the same value as time('now').
968*/
969static void ctimeFunc(
970 sqlite3_context *context,
971 int argc,
972 sqlite3_value **argv
973){
drh008e4762008-01-17 22:27:53 +0000974 timeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000975}
drh7014aff2003-11-01 01:53:53 +0000976
danielk19777977a172004-11-09 12:44:37 +0000977/*
978** current_date()
979**
980** This function returns the same value as date('now').
981*/
982static void cdateFunc(
983 sqlite3_context *context,
984 int argc,
985 sqlite3_value **argv
986){
drh008e4762008-01-17 22:27:53 +0000987 dateFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +0000988}
989
990/*
991** current_timestamp()
992**
993** This function returns the same value as datetime('now').
994*/
995static void ctimestampFunc(
996 sqlite3_context *context,
997 int argc,
998 sqlite3_value **argv
999){
drh008e4762008-01-17 22:27:53 +00001000 datetimeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001001}
drh7014aff2003-11-01 01:53:53 +00001002#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1003
danielk1977752e6792004-11-09 16:13:33 +00001004#ifdef SQLITE_OMIT_DATETIME_FUNCS
1005/*
1006** If the library is compiled to omit the full-scale date and time
1007** handling (to get a smaller binary), the following minimal version
1008** of the functions current_time(), current_date() and current_timestamp()
1009** are included instead. This is to support column declarations that
1010** include "DEFAULT CURRENT_TIME" etc.
1011**
danielk19772df9fab2004-11-11 01:50:30 +00001012** This function uses the C-library functions time(), gmtime()
danielk1977752e6792004-11-09 16:13:33 +00001013** and strftime(). The format string to pass to strftime() is supplied
1014** as the user-data for the function.
1015*/
danielk1977752e6792004-11-09 16:13:33 +00001016static void currentTimeFunc(
1017 sqlite3_context *context,
1018 int argc,
1019 sqlite3_value **argv
1020){
1021 time_t t;
1022 char *zFormat = (char *)sqlite3_user_data(context);
drhfa4a4b92008-03-19 21:45:51 +00001023 sqlite3 *db;
drh8257f0c2008-03-19 20:18:27 +00001024 double rT;
danielk1977752e6792004-11-09 16:13:33 +00001025 char zBuf[20];
danielk1977752e6792004-11-09 16:13:33 +00001026
drhfa4a4b92008-03-19 21:45:51 +00001027 db = sqlite3_context_db_handle(context);
1028 sqlite3OsCurrentTime(db->pVfs, &rT);
drh8257f0c2008-03-19 20:18:27 +00001029 t = 86400.0*(rT - 2440587.5) + 0.5;
drh87595762006-09-08 12:49:43 +00001030#ifdef HAVE_GMTIME_R
1031 {
1032 struct tm sNow;
1033 gmtime_r(&t, &sNow);
1034 strftime(zBuf, 20, zFormat, &sNow);
1035 }
1036#else
1037 {
1038 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +00001039 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001040 pTm = gmtime(&t);
1041 strftime(zBuf, 20, zFormat, pTm);
danielk197759f8c082008-06-18 17:09:10 +00001042 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001043 }
1044#endif
danielk1977e6efa742004-11-10 11:55:10 +00001045
danielk1977752e6792004-11-09 16:13:33 +00001046 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1047}
1048#endif
1049
drh7014aff2003-11-01 01:53:53 +00001050/*
1051** This function registered all of the above C functions as SQL
1052** functions. This should be the only routine in this file with
1053** external linkage.
1054*/
drh777c5382008-08-21 20:21:34 +00001055void sqlite3RegisterDateTimeFunctions(void){
1056 static FuncDef aDateTimeFuncs[] = {
drhfd1f3942004-07-20 00:39:14 +00001057#ifndef SQLITE_OMIT_DATETIME_FUNCS
drh777c5382008-08-21 20:21:34 +00001058 FUNCTION(julianday, -1, 0, 0, juliandayFunc ),
1059 FUNCTION(date, -1, 0, 0, dateFunc ),
1060 FUNCTION(time, -1, 0, 0, timeFunc ),
1061 FUNCTION(datetime, -1, 0, 0, datetimeFunc ),
1062 FUNCTION(strftime, -1, 0, 0, strftimeFunc ),
1063 FUNCTION(current_time, 0, 0, 0, ctimeFunc ),
1064 FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
1065 FUNCTION(current_date, 0, 0, 0, cdateFunc ),
danielk1977752e6792004-11-09 16:13:33 +00001066#else
drh777c5382008-08-21 20:21:34 +00001067 FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
1068 FUNCTION(current_timestamp, 0, "%Y-%m-%d", 0, currentTimeFunc),
1069 FUNCTION(current_date, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
1070#endif
danielk1977752e6792004-11-09 16:13:33 +00001071 };
1072 int i;
1073
drh777c5382008-08-21 20:21:34 +00001074 for(i=0; i<ArraySize(aDateTimeFuncs); i++){
1075 sqlite3FuncDefInsert(&sqlite3GlobalFunctions, &aDateTimeFuncs[i]);
danielk1977752e6792004-11-09 16:13:33 +00001076 }
drh7014aff2003-11-01 01:53:53 +00001077}