blob: f9411ea5b368213de13a64b4d14e6aee29188277 [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
shaneb8109ad2008-05-27 19:49:21 +000053
54/*
drh7014aff2003-11-01 01:53:53 +000055** A structure for holding a single date and time.
56*/
57typedef struct DateTime DateTime;
58struct DateTime {
drh85f477a2008-06-12 16:35:38 +000059 sqlite3_int64 iJD; /* The julian day number times 86400000 */
60 int Y, M, D; /* Year, month, and day */
61 int h, m; /* Hour and minutes */
62 int tz; /* Timezone offset in minutes */
63 double s; /* Seconds */
shaneaef3af52008-12-09 04:59:00 +000064 char validYMD; /* True (1) if Y,M,D are valid */
65 char validHMS; /* True (1) if h,m,s are valid */
66 char validJD; /* True (1) if iJD is valid */
67 char validTZ; /* True (1) if tz is valid */
drh7014aff2003-11-01 01:53:53 +000068};
69
70
71/*
drheb9a9e82004-02-22 17:49:32 +000072** Convert zDate into one or more integers. Additional arguments
73** come in groups of 5 as follows:
74**
75** N number of digits in the integer
76** min minimum allowed value of the integer
77** max maximum allowed value of the integer
78** nextC first character after the integer
79** pVal where to write the integers value.
80**
81** Conversions continue until one with nextC==0 is encountered.
82** The function returns the number of successful conversions.
drh7014aff2003-11-01 01:53:53 +000083*/
drheb9a9e82004-02-22 17:49:32 +000084static int getDigits(const char *zDate, ...){
85 va_list ap;
86 int val;
87 int N;
88 int min;
89 int max;
90 int nextC;
91 int *pVal;
92 int cnt = 0;
93 va_start(ap, zDate);
94 do{
95 N = va_arg(ap, int);
96 min = va_arg(ap, int);
97 max = va_arg(ap, int);
98 nextC = va_arg(ap, int);
99 pVal = va_arg(ap, int*);
100 val = 0;
101 while( N-- ){
danielk197778ca0e72009-01-20 16:53:39 +0000102 if( !sqlite3Isdigit(*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000103 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000104 }
105 val = val*10 + *zDate - '0';
106 zDate++;
107 }
108 if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
drh029b44b2006-01-15 00:13:15 +0000109 goto end_getDigits;
drheb9a9e82004-02-22 17:49:32 +0000110 }
111 *pVal = val;
drh7014aff2003-11-01 01:53:53 +0000112 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000113 cnt++;
114 }while( nextC );
drh029b44b2006-01-15 00:13:15 +0000115end_getDigits:
drh15b9a152006-01-31 20:49:13 +0000116 va_end(ap);
drheb9a9e82004-02-22 17:49:32 +0000117 return cnt;
drh7014aff2003-11-01 01:53:53 +0000118}
119
120/*
drh7014aff2003-11-01 01:53:53 +0000121** Parse a timezone extension on the end of a date-time.
122** The extension is of the form:
123**
124** (+/-)HH:MM
125**
drh1cfdc902008-02-21 20:40:43 +0000126** Or the "zulu" notation:
127**
128** Z
129**
drh7014aff2003-11-01 01:53:53 +0000130** If the parse is successful, write the number of minutes
drh1cfdc902008-02-21 20:40:43 +0000131** of change in p->tz and return 0. If a parser error occurs,
132** return non-zero.
drh7014aff2003-11-01 01:53:53 +0000133**
134** A missing specifier is not considered an error.
135*/
136static int parseTimezone(const char *zDate, DateTime *p){
137 int sgn = 0;
138 int nHr, nMn;
drh1cfdc902008-02-21 20:40:43 +0000139 int c;
danielk197778ca0e72009-01-20 16:53:39 +0000140 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000141 p->tz = 0;
drh1cfdc902008-02-21 20:40:43 +0000142 c = *zDate;
143 if( c=='-' ){
drh7014aff2003-11-01 01:53:53 +0000144 sgn = -1;
drh1cfdc902008-02-21 20:40:43 +0000145 }else if( c=='+' ){
drh7014aff2003-11-01 01:53:53 +0000146 sgn = +1;
drh1cfdc902008-02-21 20:40:43 +0000147 }else if( c=='Z' || c=='z' ){
148 zDate++;
149 goto zulu_time;
drh7014aff2003-11-01 01:53:53 +0000150 }else{
drh1cfdc902008-02-21 20:40:43 +0000151 return c!=0;
drh7014aff2003-11-01 01:53:53 +0000152 }
153 zDate++;
drheb9a9e82004-02-22 17:49:32 +0000154 if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
155 return 1;
156 }
157 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000158 p->tz = sgn*(nMn + nHr*60);
drh1cfdc902008-02-21 20:40:43 +0000159zulu_time:
danielk197778ca0e72009-01-20 16:53:39 +0000160 while( sqlite3Isspace(*zDate) ){ zDate++; }
drh7014aff2003-11-01 01:53:53 +0000161 return *zDate!=0;
162}
163
164/*
165** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
166** The HH, MM, and SS must each be exactly 2 digits. The
167** fractional seconds FFFF can be one or more digits.
168**
169** Return 1 if there is a parsing error and 0 on success.
170*/
171static int parseHhMmSs(const char *zDate, DateTime *p){
172 int h, m, s;
173 double ms = 0.0;
drheb9a9e82004-02-22 17:49:32 +0000174 if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
175 return 1;
176 }
177 zDate += 5;
drh7014aff2003-11-01 01:53:53 +0000178 if( *zDate==':' ){
drheb9a9e82004-02-22 17:49:32 +0000179 zDate++;
180 if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
181 return 1;
182 }
183 zDate += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000184 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
drh7014aff2003-11-01 01:53:53 +0000185 double rScale = 1.0;
186 zDate++;
danielk197778ca0e72009-01-20 16:53:39 +0000187 while( sqlite3Isdigit(*zDate) ){
drh7014aff2003-11-01 01:53:53 +0000188 ms = ms*10.0 + *zDate - '0';
189 rScale *= 10.0;
190 zDate++;
191 }
192 ms /= rScale;
193 }
194 }else{
195 s = 0;
196 }
197 p->validJD = 0;
198 p->validHMS = 1;
199 p->h = h;
200 p->m = m;
201 p->s = s + ms;
202 if( parseTimezone(zDate, p) ) return 1;
shaneaef3af52008-12-09 04:59:00 +0000203 p->validTZ = (p->tz!=0)?1:0;
drh7014aff2003-11-01 01:53:53 +0000204 return 0;
205}
206
207/*
208** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
209** that the YYYY-MM-DD is according to the Gregorian calendar.
210**
211** Reference: Meeus page 61
212*/
213static void computeJD(DateTime *p){
214 int Y, M, D, A, B, X1, X2;
215
216 if( p->validJD ) return;
217 if( p->validYMD ){
218 Y = p->Y;
219 M = p->M;
220 D = p->D;
221 }else{
drhba212562004-01-08 02:17:31 +0000222 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
drh7014aff2003-11-01 01:53:53 +0000223 M = 1;
224 D = 1;
225 }
226 if( M<=2 ){
227 Y--;
228 M += 12;
229 }
230 A = Y/100;
231 B = 2 - A + (A/4);
shaneaef3af52008-12-09 04:59:00 +0000232 X1 = 36525*(Y+4716)/100;
233 X2 = 306001*(M+1)/10000;
234 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
drh7014aff2003-11-01 01:53:53 +0000235 p->validJD = 1;
drh7014aff2003-11-01 01:53:53 +0000236 if( p->validHMS ){
shaneaef3af52008-12-09 04:59:00 +0000237 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
drh7014aff2003-11-01 01:53:53 +0000238 if( p->validTZ ){
drh85f477a2008-06-12 16:35:38 +0000239 p->iJD -= p->tz*60000;
drhf11c34d2006-09-08 12:27:36 +0000240 p->validYMD = 0;
drh7014aff2003-11-01 01:53:53 +0000241 p->validHMS = 0;
242 p->validTZ = 0;
243 }
244 }
245}
246
247/*
248** Parse dates of the form
249**
250** YYYY-MM-DD HH:MM:SS.FFF
251** YYYY-MM-DD HH:MM:SS
252** YYYY-MM-DD HH:MM
253** YYYY-MM-DD
254**
255** Write the result into the DateTime structure and return 0
256** on success and 1 if the input string is not a well-formed
257** date.
258*/
259static int parseYyyyMmDd(const char *zDate, DateTime *p){
drh8eb2cce2004-02-21 03:28:18 +0000260 int Y, M, D, neg;
drh7014aff2003-11-01 01:53:53 +0000261
drh8eb2cce2004-02-21 03:28:18 +0000262 if( zDate[0]=='-' ){
263 zDate++;
264 neg = 1;
265 }else{
266 neg = 0;
267 }
drheb9a9e82004-02-22 17:49:32 +0000268 if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
269 return 1;
270 }
271 zDate += 10;
danielk197778ca0e72009-01-20 16:53:39 +0000272 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
drheb9a9e82004-02-22 17:49:32 +0000273 if( parseHhMmSs(zDate, p)==0 ){
274 /* We got the time */
drh7014aff2003-11-01 01:53:53 +0000275 }else if( *zDate==0 ){
276 p->validHMS = 0;
277 }else{
278 return 1;
279 }
280 p->validJD = 0;
281 p->validYMD = 1;
drh8eb2cce2004-02-21 03:28:18 +0000282 p->Y = neg ? -Y : Y;
drh7014aff2003-11-01 01:53:53 +0000283 p->M = M;
284 p->D = D;
285 if( p->validTZ ){
286 computeJD(p);
287 }
288 return 0;
289}
290
291/*
drh3af5d682008-06-12 13:50:00 +0000292** Set the time to the current time reported by the VFS
293*/
294static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
drh3af5d682008-06-12 13:50:00 +0000295 sqlite3 *db = sqlite3_context_db_handle(context);
drhb7e8ea22010-05-03 14:32:30 +0000296 sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
drh3af5d682008-06-12 13:50:00 +0000297 p->validJD = 1;
298}
299
300/*
drh7014aff2003-11-01 01:53:53 +0000301** Attempt to parse the given string into a Julian Day Number. Return
302** the number of errors.
303**
304** The following are acceptable forms for the input string:
305**
306** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
307** DDDD.DD
308** now
309**
310** In the first form, the +/-HH:MM is always optional. The fractional
311** seconds extension (the ".FFF") is optional. The seconds portion
312** (":SS.FFF") is option. The year and date can be omitted as long
313** as there is a time string. The time string can be omitted as long
314** as there is a year and date.
315*/
danielk1977fee2d252007-08-18 10:59:19 +0000316static int parseDateOrTime(
317 sqlite3_context *context,
318 const char *zDate,
319 DateTime *p
320){
drh9339da12010-09-30 00:50:49 +0000321 double r;
drh8eb2cce2004-02-21 03:28:18 +0000322 if( parseYyyyMmDd(zDate,p)==0 ){
drh7014aff2003-11-01 01:53:53 +0000323 return 0;
drh8eb2cce2004-02-21 03:28:18 +0000324 }else if( parseHhMmSs(zDate, p)==0 ){
325 return 0;
danielk19774adee202004-05-08 08:23:19 +0000326 }else if( sqlite3StrICmp(zDate,"now")==0){
drh3af5d682008-06-12 13:50:00 +0000327 setDateTimeToCurrent(context, p);
drh018d1a42005-01-15 01:52:31 +0000328 return 0;
drh9339da12010-09-30 00:50:49 +0000329 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
drh85f477a2008-06-12 16:35:38 +0000330 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
drh7014aff2003-11-01 01:53:53 +0000331 p->validJD = 1;
332 return 0;
333 }
334 return 1;
335}
336
337/*
338** Compute the Year, Month, and Day from the julian day number.
339*/
340static void computeYMD(DateTime *p){
341 int Z, A, B, C, D, E, X1;
342 if( p->validYMD ) return;
drh33a9ad22004-02-29 00:40:32 +0000343 if( !p->validJD ){
344 p->Y = 2000;
345 p->M = 1;
346 p->D = 1;
347 }else{
shaneaef3af52008-12-09 04:59:00 +0000348 Z = (int)((p->iJD + 43200000)/86400000);
349 A = (int)((Z - 1867216.25)/36524.25);
drh33a9ad22004-02-29 00:40:32 +0000350 A = Z + 1 + A - (A/4);
351 B = A + 1524;
shaneaef3af52008-12-09 04:59:00 +0000352 C = (int)((B - 122.1)/365.25);
353 D = (36525*C)/100;
354 E = (int)((B-D)/30.6001);
355 X1 = (int)(30.6001*E);
drh33a9ad22004-02-29 00:40:32 +0000356 p->D = B - D - X1;
357 p->M = E<14 ? E-1 : E-13;
358 p->Y = p->M>2 ? C - 4716 : C - 4715;
359 }
drh7014aff2003-11-01 01:53:53 +0000360 p->validYMD = 1;
361}
362
363/*
364** Compute the Hour, Minute, and Seconds from the julian day number.
365*/
366static void computeHMS(DateTime *p){
drh85f477a2008-06-12 16:35:38 +0000367 int s;
drh7014aff2003-11-01 01:53:53 +0000368 if( p->validHMS ) return;
drhf11c34d2006-09-08 12:27:36 +0000369 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000370 s = (int)((p->iJD + 43200000) % 86400000);
drh85f477a2008-06-12 16:35:38 +0000371 p->s = s/1000.0;
shaneaef3af52008-12-09 04:59:00 +0000372 s = (int)p->s;
drh7014aff2003-11-01 01:53:53 +0000373 p->s -= s;
374 p->h = s/3600;
375 s -= p->h*3600;
376 p->m = s/60;
377 p->s += s - p->m*60;
378 p->validHMS = 1;
379}
380
381/*
drhba212562004-01-08 02:17:31 +0000382** Compute both YMD and HMS
383*/
384static void computeYMD_HMS(DateTime *p){
385 computeYMD(p);
386 computeHMS(p);
387}
388
389/*
390** Clear the YMD and HMS and the TZ
391*/
392static void clearYMD_HMS_TZ(DateTime *p){
393 p->validYMD = 0;
394 p->validHMS = 0;
395 p->validTZ = 0;
396}
397
drha924aca2011-06-21 15:01:25 +0000398/*
399** On recent Windows platforms, the localtime_s() function is available
400** as part of the "Secure CRT". It is essentially equivalent to
401** localtime_r() available under most POSIX platforms, except that the
402** order of the parameters is reversed.
403**
404** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
405**
406** If the user has not indicated to use localtime_r() or localtime_s()
407** already, check for an MSVC build environment that provides
408** localtime_s().
409*/
410#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
411 defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
412#define HAVE_LOCALTIME_S 1
413#endif
414
drh66147c92008-06-12 12:51:37 +0000415#ifndef SQLITE_OMIT_LOCALTIME
drhba212562004-01-08 02:17:31 +0000416/*
drh8720aeb2011-06-21 14:35:30 +0000417** The following routine implements the rough equivalent of localtime_r()
418** using whatever operating-system specific localtime facility that
419** is available. This routine returns 0 on success and
420** non-zero on any kind of error.
danc17d6962011-06-21 12:47:30 +0000421**
drh8720aeb2011-06-21 14:35:30 +0000422** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
423** routine will always fail.
drh7091cb02003-12-23 16:22:18 +0000424*/
drh1f93a082011-06-21 15:54:24 +0000425static int osLocaltime(time_t *t, struct tm *pTm){
drh8720aeb2011-06-21 14:35:30 +0000426 int rc;
drha924aca2011-06-21 15:01:25 +0000427#if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
428 && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
429 struct tm *pX;
drhdf3aa162011-06-24 11:29:51 +0000430#if SQLITE_THREADSAFE>0
drha924aca2011-06-21 15:01:25 +0000431 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhdf3aa162011-06-24 11:29:51 +0000432#endif
drha924aca2011-06-21 15:01:25 +0000433 sqlite3_mutex_enter(mutex);
434 pX = localtime(t);
435#ifndef SQLITE_OMIT_BUILTIN_TEST
436 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
437#endif
438 if( pX ) *pTm = *pX;
439 sqlite3_mutex_leave(mutex);
440 rc = pX==0;
441#else
danc17d6962011-06-21 12:47:30 +0000442#ifndef SQLITE_OMIT_BUILTIN_TEST
danc17d6962011-06-21 12:47:30 +0000443 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
danc17d6962011-06-21 12:47:30 +0000444#endif
drha924aca2011-06-21 15:01:25 +0000445#if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
drh8720aeb2011-06-21 14:35:30 +0000446 rc = localtime_r(t, pTm)==0;
danc17d6962011-06-21 12:47:30 +0000447#else
drha924aca2011-06-21 15:01:25 +0000448 rc = localtime_s(pTm, t);
449#endif /* HAVE_LOCALTIME_R */
450#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
drh8720aeb2011-06-21 14:35:30 +0000451 return rc;
452}
453#endif /* SQLITE_OMIT_LOCALTIME */
danc17d6962011-06-21 12:47:30 +0000454
455
drh8720aeb2011-06-21 14:35:30 +0000456#ifndef SQLITE_OMIT_LOCALTIME
danc17d6962011-06-21 12:47:30 +0000457/*
458** Compute the difference (in milliseconds) between localtime and UTC
459** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
460** return this value and set *pRc to SQLITE_OK.
461**
462** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
463** is undefined in this case.
464*/
465static sqlite3_int64 localtimeOffset(
466 DateTime *p, /* Date at which to calculate offset */
467 sqlite3_context *pCtx, /* Write error here if one occurs */
468 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
469){
drh7091cb02003-12-23 16:22:18 +0000470 DateTime x, y;
471 time_t t;
drh8720aeb2011-06-21 14:35:30 +0000472 struct tm sLocal;
473
dan0d37f582011-06-21 15:38:05 +0000474 /* Initialize the contents of sLocal to avoid a compiler warning. */
475 memset(&sLocal, 0, sizeof(sLocal));
476
drh7091cb02003-12-23 16:22:18 +0000477 x = *p;
drhba212562004-01-08 02:17:31 +0000478 computeYMD_HMS(&x);
drh7091cb02003-12-23 16:22:18 +0000479 if( x.Y<1971 || x.Y>=2038 ){
480 x.Y = 2000;
481 x.M = 1;
482 x.D = 1;
483 x.h = 0;
484 x.m = 0;
485 x.s = 0.0;
486 } else {
shaneaef3af52008-12-09 04:59:00 +0000487 int s = (int)(x.s + 0.5);
drh7091cb02003-12-23 16:22:18 +0000488 x.s = s;
489 }
490 x.tz = 0;
491 x.validJD = 0;
492 computeJD(&x);
shane11bb41f2009-09-10 20:23:30 +0000493 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
drh8720aeb2011-06-21 14:35:30 +0000494 if( osLocaltime(&t, &sLocal) ){
495 sqlite3_result_error(pCtx, "local time unavailable", -1);
496 *pRc = SQLITE_ERROR;
497 return 0;
drh87595762006-09-08 12:49:43 +0000498 }
drh8720aeb2011-06-21 14:35:30 +0000499 y.Y = sLocal.tm_year + 1900;
500 y.M = sLocal.tm_mon + 1;
501 y.D = sLocal.tm_mday;
502 y.h = sLocal.tm_hour;
503 y.m = sLocal.tm_min;
504 y.s = sLocal.tm_sec;
drh7091cb02003-12-23 16:22:18 +0000505 y.validYMD = 1;
506 y.validHMS = 1;
507 y.validJD = 0;
508 y.validTZ = 0;
509 computeJD(&y);
danc17d6962011-06-21 12:47:30 +0000510 *pRc = SQLITE_OK;
drh85f477a2008-06-12 16:35:38 +0000511 return y.iJD - x.iJD;
drh7091cb02003-12-23 16:22:18 +0000512}
drh66147c92008-06-12 12:51:37 +0000513#endif /* SQLITE_OMIT_LOCALTIME */
drh7091cb02003-12-23 16:22:18 +0000514
515/*
drh7014aff2003-11-01 01:53:53 +0000516** Process a modifier to a date-time stamp. The modifiers are
517** as follows:
518**
519** NNN days
520** NNN hours
521** NNN minutes
522** NNN.NNNN seconds
523** NNN months
524** NNN years
525** start of month
526** start of year
527** start of week
528** start of day
529** weekday N
530** unixepoch
drh7091cb02003-12-23 16:22:18 +0000531** localtime
532** utc
drh7014aff2003-11-01 01:53:53 +0000533**
danc17d6962011-06-21 12:47:30 +0000534** Return 0 on success and 1 if there is any kind of error. If the error
535** is in a system call (i.e. localtime()), then an error message is written
536** to context pCtx. If the error is an unrecognized modifier, no error is
537** written to pCtx.
drh7014aff2003-11-01 01:53:53 +0000538*/
danc17d6962011-06-21 12:47:30 +0000539static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
drh7014aff2003-11-01 01:53:53 +0000540 int rc = 1;
541 int n;
542 double r;
drh4d5b8362004-01-17 01:16:21 +0000543 char *z, zBuf[30];
544 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000545 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
drh1bd10f82008-12-10 21:19:56 +0000546 z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
drh7014aff2003-11-01 01:53:53 +0000547 }
548 z[n] = 0;
549 switch( z[0] ){
drh66147c92008-06-12 12:51:37 +0000550#ifndef SQLITE_OMIT_LOCALTIME
drh7091cb02003-12-23 16:22:18 +0000551 case 'l': {
552 /* localtime
553 **
554 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
555 ** show local time.
556 */
557 if( strcmp(z, "localtime")==0 ){
558 computeJD(p);
danc17d6962011-06-21 12:47:30 +0000559 p->iJD += localtimeOffset(p, pCtx, &rc);
drhba212562004-01-08 02:17:31 +0000560 clearYMD_HMS_TZ(p);
drh7091cb02003-12-23 16:22:18 +0000561 }
562 break;
563 }
drh66147c92008-06-12 12:51:37 +0000564#endif
drh7014aff2003-11-01 01:53:53 +0000565 case 'u': {
566 /*
567 ** unixepoch
568 **
drh85f477a2008-06-12 16:35:38 +0000569 ** Treat the current value of p->iJD as the number of
drh7014aff2003-11-01 01:53:53 +0000570 ** seconds since 1970. Convert to a real julian day number.
571 */
572 if( strcmp(z, "unixepoch")==0 && p->validJD ){
drh7fee3602009-04-16 12:58:03 +0000573 p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
drhba212562004-01-08 02:17:31 +0000574 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000575 rc = 0;
drh66cccd92008-07-25 16:39:24 +0000576 }
577#ifndef SQLITE_OMIT_LOCALTIME
578 else if( strcmp(z, "utc")==0 ){
shaneaef3af52008-12-09 04:59:00 +0000579 sqlite3_int64 c1;
drh7091cb02003-12-23 16:22:18 +0000580 computeJD(p);
danc17d6962011-06-21 12:47:30 +0000581 c1 = localtimeOffset(p, pCtx, &rc);
582 if( rc==SQLITE_OK ){
583 p->iJD -= c1;
584 clearYMD_HMS_TZ(p);
585 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
586 }
drh7014aff2003-11-01 01:53:53 +0000587 }
drh66cccd92008-07-25 16:39:24 +0000588#endif
drh7014aff2003-11-01 01:53:53 +0000589 break;
590 }
591 case 'w': {
592 /*
593 ** weekday N
594 **
drh181fc992004-08-17 10:42:54 +0000595 ** Move the date to the same time on the next occurrence of
drh7014aff2003-11-01 01:53:53 +0000596 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
drhc5dd9fa2004-01-07 03:29:16 +0000597 ** date is already on the appropriate weekday, this is a no-op.
drh7014aff2003-11-01 01:53:53 +0000598 */
drh9339da12010-09-30 00:50:49 +0000599 if( strncmp(z, "weekday ", 8)==0
600 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
601 && (n=(int)r)==r && n>=0 && r<7 ){
drh85f477a2008-06-12 16:35:38 +0000602 sqlite3_int64 Z;
drhba212562004-01-08 02:17:31 +0000603 computeYMD_HMS(p);
drh7014aff2003-11-01 01:53:53 +0000604 p->validTZ = 0;
605 p->validJD = 0;
606 computeJD(p);
drh85f477a2008-06-12 16:35:38 +0000607 Z = ((p->iJD + 129600000)/86400000) % 7;
drh7014aff2003-11-01 01:53:53 +0000608 if( Z>n ) Z -= 7;
drh85f477a2008-06-12 16:35:38 +0000609 p->iJD += (n - Z)*86400000;
drhba212562004-01-08 02:17:31 +0000610 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000611 rc = 0;
612 }
613 break;
614 }
615 case 's': {
616 /*
617 ** start of TTTTT
618 **
619 ** Move the date backwards to the beginning of the current day,
620 ** or month or year.
621 */
622 if( strncmp(z, "start of ", 9)!=0 ) break;
drh4d5b8362004-01-17 01:16:21 +0000623 z += 9;
drh7014aff2003-11-01 01:53:53 +0000624 computeYMD(p);
625 p->validHMS = 1;
626 p->h = p->m = 0;
627 p->s = 0.0;
628 p->validTZ = 0;
629 p->validJD = 0;
drh4d5b8362004-01-17 01:16:21 +0000630 if( strcmp(z,"month")==0 ){
drh7014aff2003-11-01 01:53:53 +0000631 p->D = 1;
632 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000633 }else if( strcmp(z,"year")==0 ){
drh7014aff2003-11-01 01:53:53 +0000634 computeYMD(p);
635 p->M = 1;
636 p->D = 1;
637 rc = 0;
drh4d5b8362004-01-17 01:16:21 +0000638 }else if( strcmp(z,"day")==0 ){
drh7014aff2003-11-01 01:53:53 +0000639 rc = 0;
640 }
641 break;
642 }
643 case '+':
644 case '-':
645 case '0':
646 case '1':
647 case '2':
648 case '3':
649 case '4':
650 case '5':
651 case '6':
652 case '7':
653 case '8':
654 case '9': {
drhc531a222009-01-30 17:27:44 +0000655 double rRounder;
drh9339da12010-09-30 00:50:49 +0000656 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
657 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
658 rc = 1;
659 break;
660 }
drh33a9ad22004-02-29 00:40:32 +0000661 if( z[n]==':' ){
662 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
663 ** specified number of hours, minutes, seconds, and fractional seconds
664 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
665 ** omitted.
666 */
667 const char *z2 = z;
668 DateTime tx;
drh85f477a2008-06-12 16:35:38 +0000669 sqlite3_int64 day;
danielk197778ca0e72009-01-20 16:53:39 +0000670 if( !sqlite3Isdigit(*z2) ) z2++;
drh33a9ad22004-02-29 00:40:32 +0000671 memset(&tx, 0, sizeof(tx));
672 if( parseHhMmSs(z2, &tx) ) break;
673 computeJD(&tx);
drh85f477a2008-06-12 16:35:38 +0000674 tx.iJD -= 43200000;
675 day = tx.iJD/86400000;
676 tx.iJD -= day*86400000;
677 if( z[0]=='-' ) tx.iJD = -tx.iJD;
drh0d131ab2004-02-29 01:08:17 +0000678 computeJD(p);
679 clearYMD_HMS_TZ(p);
drh85f477a2008-06-12 16:35:38 +0000680 p->iJD += tx.iJD;
drh33a9ad22004-02-29 00:40:32 +0000681 rc = 0;
682 break;
683 }
drh4d5b8362004-01-17 01:16:21 +0000684 z += n;
danielk197778ca0e72009-01-20 16:53:39 +0000685 while( sqlite3Isspace(*z) ) z++;
drhea678832008-12-10 19:26:22 +0000686 n = sqlite3Strlen30(z);
drh7014aff2003-11-01 01:53:53 +0000687 if( n>10 || n<3 ) break;
drh7014aff2003-11-01 01:53:53 +0000688 if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
689 computeJD(p);
690 rc = 0;
drhc531a222009-01-30 17:27:44 +0000691 rRounder = r<0 ? -0.5 : +0.5;
drh7014aff2003-11-01 01:53:53 +0000692 if( n==3 && strcmp(z,"day")==0 ){
drhc531a222009-01-30 17:27:44 +0000693 p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000694 }else if( n==4 && strcmp(z,"hour")==0 ){
drhc531a222009-01-30 17:27:44 +0000695 p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000696 }else if( n==6 && strcmp(z,"minute")==0 ){
drhc531a222009-01-30 17:27:44 +0000697 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000698 }else if( n==6 && strcmp(z,"second")==0 ){
drhc531a222009-01-30 17:27:44 +0000699 p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
drh7014aff2003-11-01 01:53:53 +0000700 }else if( n==5 && strcmp(z,"month")==0 ){
701 int x, y;
drhba212562004-01-08 02:17:31 +0000702 computeYMD_HMS(p);
shaneaef3af52008-12-09 04:59:00 +0000703 p->M += (int)r;
drh7014aff2003-11-01 01:53:53 +0000704 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
705 p->Y += x;
706 p->M -= x*12;
707 p->validJD = 0;
708 computeJD(p);
shaneaef3af52008-12-09 04:59:00 +0000709 y = (int)r;
drh7014aff2003-11-01 01:53:53 +0000710 if( y!=r ){
drhc531a222009-01-30 17:27:44 +0000711 p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
drh7014aff2003-11-01 01:53:53 +0000712 }
713 }else if( n==4 && strcmp(z,"year")==0 ){
drhc531a222009-01-30 17:27:44 +0000714 int y = (int)r;
drhba212562004-01-08 02:17:31 +0000715 computeYMD_HMS(p);
drhc531a222009-01-30 17:27:44 +0000716 p->Y += y;
drh7014aff2003-11-01 01:53:53 +0000717 p->validJD = 0;
718 computeJD(p);
drhc531a222009-01-30 17:27:44 +0000719 if( y!=r ){
720 p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
721 }
drh7014aff2003-11-01 01:53:53 +0000722 }else{
723 rc = 1;
724 }
drhba212562004-01-08 02:17:31 +0000725 clearYMD_HMS_TZ(p);
drh7014aff2003-11-01 01:53:53 +0000726 break;
727 }
728 default: {
729 break;
730 }
731 }
732 return rc;
733}
734
735/*
736** Process time function arguments. argv[0] is a date-time stamp.
737** argv[1] and following are modifiers. Parse them all and write
738** the resulting time into the DateTime structure p. Return 0
739** on success and 1 if there are any errors.
drh008e4762008-01-17 22:27:53 +0000740**
741** If there are zero parameters (if even argv[0] is undefined)
742** then assume a default value of "now" for argv[0].
drh7014aff2003-11-01 01:53:53 +0000743*/
danielk1977fee2d252007-08-18 10:59:19 +0000744static int isDate(
745 sqlite3_context *context,
746 int argc,
747 sqlite3_value **argv,
748 DateTime *p
749){
drh7014aff2003-11-01 01:53:53 +0000750 int i;
drh7a521cf2007-04-25 18:23:52 +0000751 const unsigned char *z;
drh85f477a2008-06-12 16:35:38 +0000752 int eType;
drh3af5d682008-06-12 13:50:00 +0000753 memset(p, 0, sizeof(*p));
drh008e4762008-01-17 22:27:53 +0000754 if( argc==0 ){
drh3af5d682008-06-12 13:50:00 +0000755 setDateTimeToCurrent(context, p);
drh85f477a2008-06-12 16:35:38 +0000756 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
757 || eType==SQLITE_INTEGER ){
shaneaef3af52008-12-09 04:59:00 +0000758 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
drh3af5d682008-06-12 13:50:00 +0000759 p->validJD = 1;
drh008e4762008-01-17 22:27:53 +0000760 }else{
761 z = sqlite3_value_text(argv[0]);
drh3af5d682008-06-12 13:50:00 +0000762 if( !z || parseDateOrTime(context, (char*)z, p) ){
763 return 1;
764 }
drh7a521cf2007-04-25 18:23:52 +0000765 }
drh7014aff2003-11-01 01:53:53 +0000766 for(i=1; i<argc; i++){
danc17d6962011-06-21 12:47:30 +0000767 z = sqlite3_value_text(argv[i]);
768 if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
drh7014aff2003-11-01 01:53:53 +0000769 }
770 return 0;
771}
772
773
774/*
775** The following routines implement the various date and time functions
776** of SQLite.
777*/
778
779/*
780** julianday( TIMESTRING, MOD, MOD, ...)
781**
782** Return the julian day number of the date specified in the arguments
783*/
drhf9b596e2004-05-26 16:54:42 +0000784static void juliandayFunc(
785 sqlite3_context *context,
786 int argc,
787 sqlite3_value **argv
788){
drh7014aff2003-11-01 01:53:53 +0000789 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000790 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000791 computeJD(&x);
drh85f477a2008-06-12 16:35:38 +0000792 sqlite3_result_double(context, x.iJD/86400000.0);
drh7014aff2003-11-01 01:53:53 +0000793 }
794}
795
796/*
797** datetime( TIMESTRING, MOD, MOD, ...)
798**
799** Return YYYY-MM-DD HH:MM:SS
800*/
drhf9b596e2004-05-26 16:54:42 +0000801static void datetimeFunc(
802 sqlite3_context *context,
803 int argc,
804 sqlite3_value **argv
805){
drh7014aff2003-11-01 01:53:53 +0000806 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000807 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000808 char zBuf[100];
drhba212562004-01-08 02:17:31 +0000809 computeYMD_HMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000810 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
811 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
danielk1977d8123362004-06-12 09:25:12 +0000812 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000813 }
814}
815
816/*
817** time( TIMESTRING, MOD, MOD, ...)
818**
819** Return HH:MM:SS
820*/
drhf9b596e2004-05-26 16:54:42 +0000821static void timeFunc(
822 sqlite3_context *context,
823 int argc,
824 sqlite3_value **argv
825){
drh7014aff2003-11-01 01:53:53 +0000826 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000827 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000828 char zBuf[100];
829 computeHMS(&x);
drh5bb3eb92007-05-04 13:15:55 +0000830 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
danielk1977d8123362004-06-12 09:25:12 +0000831 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000832 }
833}
834
835/*
836** date( TIMESTRING, MOD, MOD, ...)
837**
838** Return YYYY-MM-DD
839*/
drhf9b596e2004-05-26 16:54:42 +0000840static void dateFunc(
841 sqlite3_context *context,
842 int argc,
843 sqlite3_value **argv
844){
drh7014aff2003-11-01 01:53:53 +0000845 DateTime x;
danielk1977fee2d252007-08-18 10:59:19 +0000846 if( isDate(context, argc, argv, &x)==0 ){
drh7014aff2003-11-01 01:53:53 +0000847 char zBuf[100];
848 computeYMD(&x);
drh5bb3eb92007-05-04 13:15:55 +0000849 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
danielk1977d8123362004-06-12 09:25:12 +0000850 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
drh7014aff2003-11-01 01:53:53 +0000851 }
852}
853
854/*
855** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
856**
857** Return a string described by FORMAT. Conversions as follows:
858**
859** %d day of month
860** %f ** fractional seconds SS.SSS
861** %H hour 00-24
862** %j day of year 000-366
863** %J ** Julian day number
864** %m month 01-12
865** %M minute 00-59
866** %s seconds since 1970-01-01
867** %S seconds 00-59
868** %w day of week 0-6 sunday==0
869** %W week of year 00-53
870** %Y year 0000-9999
871** %% %
872*/
drhf9b596e2004-05-26 16:54:42 +0000873static void strftimeFunc(
874 sqlite3_context *context,
875 int argc,
876 sqlite3_value **argv
877){
drh7014aff2003-11-01 01:53:53 +0000878 DateTime x;
drha0206bc2007-05-08 15:15:02 +0000879 u64 n;
shaneaef3af52008-12-09 04:59:00 +0000880 size_t i,j;
drh7014aff2003-11-01 01:53:53 +0000881 char *z;
drh633e6d52008-07-28 19:34:53 +0000882 sqlite3 *db;
drh2646da72005-12-09 20:02:05 +0000883 const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
drh7014aff2003-11-01 01:53:53 +0000884 char zBuf[100];
danielk1977fee2d252007-08-18 10:59:19 +0000885 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
drh633e6d52008-07-28 19:34:53 +0000886 db = sqlite3_context_db_handle(context);
drh7014aff2003-11-01 01:53:53 +0000887 for(i=0, n=1; zFmt[i]; i++, n++){
888 if( zFmt[i]=='%' ){
889 switch( zFmt[i+1] ){
890 case 'd':
891 case 'H':
892 case 'm':
893 case 'M':
894 case 'S':
895 case 'W':
896 n++;
897 /* fall thru */
898 case 'w':
899 case '%':
900 break;
901 case 'f':
902 n += 8;
903 break;
904 case 'j':
905 n += 3;
906 break;
907 case 'Y':
908 n += 8;
909 break;
910 case 's':
911 case 'J':
912 n += 50;
913 break;
914 default:
915 return; /* ERROR. return a NULL */
916 }
917 i++;
918 }
919 }
drh67110022009-01-28 02:55:28 +0000920 testcase( n==sizeof(zBuf)-1 );
921 testcase( n==sizeof(zBuf) );
922 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
923 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
drh7014aff2003-11-01 01:53:53 +0000924 if( n<sizeof(zBuf) ){
925 z = zBuf;
danielk197700e13612008-11-17 19:18:54 +0000926 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha0206bc2007-05-08 15:15:02 +0000927 sqlite3_result_error_toobig(context);
928 return;
drh7014aff2003-11-01 01:53:53 +0000929 }else{
shaneaef3af52008-12-09 04:59:00 +0000930 z = sqlite3DbMallocRaw(db, (int)n);
drh3334e942008-01-17 20:26:46 +0000931 if( z==0 ){
932 sqlite3_result_error_nomem(context);
933 return;
934 }
drh7014aff2003-11-01 01:53:53 +0000935 }
936 computeJD(&x);
drhba212562004-01-08 02:17:31 +0000937 computeYMD_HMS(&x);
drh7014aff2003-11-01 01:53:53 +0000938 for(i=j=0; zFmt[i]; i++){
939 if( zFmt[i]!='%' ){
940 z[j++] = zFmt[i];
941 }else{
942 i++;
943 switch( zFmt[i] ){
drh5bb3eb92007-05-04 13:15:55 +0000944 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000945 case 'f': {
drhb1f1e6e2006-09-25 18:01:31 +0000946 double s = x.s;
947 if( s>59.999 ) s = 59.999;
drh2ecad3b2007-03-29 17:57:21 +0000948 sqlite3_snprintf(7, &z[j],"%06.3f", s);
drhea678832008-12-10 19:26:22 +0000949 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000950 break;
951 }
drh5bb3eb92007-05-04 13:15:55 +0000952 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000953 case 'W': /* Fall thru */
954 case 'j': {
danielk1977f0113002006-01-24 12:09:17 +0000955 int nDay; /* Number of days since 1st day of year */
drh7014aff2003-11-01 01:53:53 +0000956 DateTime y = x;
957 y.validJD = 0;
958 y.M = 1;
959 y.D = 1;
960 computeJD(&y);
shaneaef3af52008-12-09 04:59:00 +0000961 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
drh7014aff2003-11-01 01:53:53 +0000962 if( zFmt[i]=='W' ){
drh1020d492004-07-18 22:22:43 +0000963 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
shaneaef3af52008-12-09 04:59:00 +0000964 wd = (int)(((x.iJD+43200000)/86400000)%7);
drh5bb3eb92007-05-04 13:15:55 +0000965 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
drh7014aff2003-11-01 01:53:53 +0000966 j += 2;
967 }else{
drh5bb3eb92007-05-04 13:15:55 +0000968 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
drh7014aff2003-11-01 01:53:53 +0000969 j += 3;
970 }
971 break;
972 }
drh5bb3eb92007-05-04 13:15:55 +0000973 case 'J': {
drh85f477a2008-06-12 16:35:38 +0000974 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
drhea678832008-12-10 19:26:22 +0000975 j+=sqlite3Strlen30(&z[j]);
drh5bb3eb92007-05-04 13:15:55 +0000976 break;
977 }
978 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
979 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
drh7014aff2003-11-01 01:53:53 +0000980 case 's': {
drh6eb41522009-04-01 20:44:13 +0000981 sqlite3_snprintf(30,&z[j],"%lld",
drh07758962009-04-03 12:04:36 +0000982 (i64)(x.iJD/1000 - 21086676*(i64)10000));
drhea678832008-12-10 19:26:22 +0000983 j += sqlite3Strlen30(&z[j]);
drh7014aff2003-11-01 01:53:53 +0000984 break;
985 }
drh5bb3eb92007-05-04 13:15:55 +0000986 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
drhea678832008-12-10 19:26:22 +0000987 case 'w': {
988 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
989 break;
990 }
991 case 'Y': {
992 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
993 break;
994 }
drh008e4762008-01-17 22:27:53 +0000995 default: z[j++] = '%'; break;
drh7014aff2003-11-01 01:53:53 +0000996 }
997 }
998 }
999 z[j] = 0;
drh3334e942008-01-17 20:26:46 +00001000 sqlite3_result_text(context, z, -1,
drh633e6d52008-07-28 19:34:53 +00001001 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
drh7014aff2003-11-01 01:53:53 +00001002}
1003
danielk19777977a172004-11-09 12:44:37 +00001004/*
1005** current_time()
1006**
1007** This function returns the same value as time('now').
1008*/
1009static void ctimeFunc(
1010 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001011 int NotUsed,
1012 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001013){
danielk197762c14b32008-11-19 09:05:26 +00001014 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001015 timeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001016}
drh7014aff2003-11-01 01:53:53 +00001017
danielk19777977a172004-11-09 12:44:37 +00001018/*
1019** current_date()
1020**
1021** This function returns the same value as date('now').
1022*/
1023static void cdateFunc(
1024 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001025 int NotUsed,
1026 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001027){
danielk197762c14b32008-11-19 09:05:26 +00001028 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001029 dateFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001030}
1031
1032/*
1033** current_timestamp()
1034**
1035** This function returns the same value as datetime('now').
1036*/
1037static void ctimestampFunc(
1038 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +00001039 int NotUsed,
1040 sqlite3_value **NotUsed2
danielk19777977a172004-11-09 12:44:37 +00001041){
danielk197762c14b32008-11-19 09:05:26 +00001042 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drh008e4762008-01-17 22:27:53 +00001043 datetimeFunc(context, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001044}
drh7014aff2003-11-01 01:53:53 +00001045#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1046
danielk1977752e6792004-11-09 16:13:33 +00001047#ifdef SQLITE_OMIT_DATETIME_FUNCS
1048/*
1049** If the library is compiled to omit the full-scale date and time
1050** handling (to get a smaller binary), the following minimal version
1051** of the functions current_time(), current_date() and current_timestamp()
1052** are included instead. This is to support column declarations that
1053** include "DEFAULT CURRENT_TIME" etc.
1054**
danielk19772df9fab2004-11-11 01:50:30 +00001055** This function uses the C-library functions time(), gmtime()
danielk1977752e6792004-11-09 16:13:33 +00001056** and strftime(). The format string to pass to strftime() is supplied
1057** as the user-data for the function.
1058*/
danielk1977752e6792004-11-09 16:13:33 +00001059static void currentTimeFunc(
1060 sqlite3_context *context,
1061 int argc,
1062 sqlite3_value **argv
1063){
1064 time_t t;
1065 char *zFormat = (char *)sqlite3_user_data(context);
drhfa4a4b92008-03-19 21:45:51 +00001066 sqlite3 *db;
drhb7e8ea22010-05-03 14:32:30 +00001067 sqlite3_int64 iT;
danielk1977752e6792004-11-09 16:13:33 +00001068 char zBuf[20];
danielk1977752e6792004-11-09 16:13:33 +00001069
shanefbd60f82009-02-04 03:59:25 +00001070 UNUSED_PARAMETER(argc);
1071 UNUSED_PARAMETER(argv);
1072
drhfa4a4b92008-03-19 21:45:51 +00001073 db = sqlite3_context_db_handle(context);
drhb7e8ea22010-05-03 14:32:30 +00001074 sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
drhd5e6e402010-05-03 19:17:01 +00001075 t = iT/1000 - 10000*(sqlite3_int64)21086676;
drh87595762006-09-08 12:49:43 +00001076#ifdef HAVE_GMTIME_R
1077 {
1078 struct tm sNow;
1079 gmtime_r(&t, &sNow);
1080 strftime(zBuf, 20, zFormat, &sNow);
1081 }
1082#else
1083 {
1084 struct tm *pTm;
danielk197759f8c082008-06-18 17:09:10 +00001085 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001086 pTm = gmtime(&t);
1087 strftime(zBuf, 20, zFormat, pTm);
danielk197759f8c082008-06-18 17:09:10 +00001088 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
drh87595762006-09-08 12:49:43 +00001089 }
1090#endif
danielk1977e6efa742004-11-10 11:55:10 +00001091
danielk1977752e6792004-11-09 16:13:33 +00001092 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1093}
1094#endif
1095
drh7014aff2003-11-01 01:53:53 +00001096/*
1097** This function registered all of the above C functions as SQL
1098** functions. This should be the only routine in this file with
1099** external linkage.
1100*/
drh777c5382008-08-21 20:21:34 +00001101void sqlite3RegisterDateTimeFunctions(void){
danielk1977075c23a2008-09-01 18:34:20 +00001102 static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
drhfd1f3942004-07-20 00:39:14 +00001103#ifndef SQLITE_OMIT_DATETIME_FUNCS
drh777c5382008-08-21 20:21:34 +00001104 FUNCTION(julianday, -1, 0, 0, juliandayFunc ),
1105 FUNCTION(date, -1, 0, 0, dateFunc ),
1106 FUNCTION(time, -1, 0, 0, timeFunc ),
1107 FUNCTION(datetime, -1, 0, 0, datetimeFunc ),
1108 FUNCTION(strftime, -1, 0, 0, strftimeFunc ),
1109 FUNCTION(current_time, 0, 0, 0, ctimeFunc ),
1110 FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
1111 FUNCTION(current_date, 0, 0, 0, cdateFunc ),
danielk1977752e6792004-11-09 16:13:33 +00001112#else
drh21717ed2008-10-13 15:35:08 +00001113 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
drh2b1e6902010-01-12 19:28:20 +00001114 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
1115 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
drh777c5382008-08-21 20:21:34 +00001116#endif
danielk1977752e6792004-11-09 16:13:33 +00001117 };
1118 int i;
danielk1977075c23a2008-09-01 18:34:20 +00001119 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
drh106cee52008-09-03 17:11:16 +00001120 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
danielk1977752e6792004-11-09 16:13:33 +00001121
drh777c5382008-08-21 20:21:34 +00001122 for(i=0; i<ArraySize(aDateTimeFuncs); i++){
danielk1977075c23a2008-09-01 18:34:20 +00001123 sqlite3FuncDefInsert(pHash, &aFunc[i]);
danielk1977752e6792004-11-09 16:13:33 +00001124 }
drh7014aff2003-11-01 01:53:53 +00001125}