date.c

filter: Statementstmt 1.4% 6 / 425 Branchbran 1.4% 3 / 208 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 1.4% 9 / 633 SCAR 0
CC
0
Cov
0%
CRAP 0
« trigger.c ↑ Summary pragma.c »
1 /*
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
16 ** sqliteRegisterDateTimeFunctions() found at the bottom of the file.
17 ** All other code has file scope.
18 **
19 ** $Id: date.c,v 1.1.1.1 2004/08/08 15:03:57 matt Exp $
20 **
21 ** NOTES:
22 **
23 ** SQLite processes all times and dates as Julian Day numbers. The
24 ** dates and times are stored as the number of days since noon
25 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
26 ** calendar system.
27 **
28 ** 1970-01-01 00:00:00 is JD 2440587.5
29 ** 2000-01-01 00:00:00 is JD 2451544.5
30 **
31 ** This implemention requires years to be expressed as a 4-digit number
32 ** which means that only dates between 0000-01-01 and 9999-12-31 can
33 ** be represented, even though julian day numbers allow a much wider
34 ** range of dates.
35 **
36 ** The Gregorian calendar system is used for all dates and times,
37 ** even those that predate the Gregorian calendar. Historians usually
38 ** use the Julian calendar for dates prior to 1582-10-15 and for some
39 ** dates afterwards, depending on locale. Beware of this difference.
40 **
41 ** The conversion algorithms are implemented based on descriptions
42 ** in the following text:
43 **
44 ** Jean Meeus
45 ** Astronomical Algorithms, 2nd Edition, 1998
46 ** ISBM 0-943396-61-1
47 ** Willmann-Bell, Inc
48 ** Richmond, Virginia (USA)
49 */
50 #include "os.h"
51 #include "sqliteInt.h"
52 #include <ctype.h>
53 #include <stdlib.h>
54 #include <assert.h>
55 #include <time.h>
56  
57 #ifndef SQLITE_OMIT_DATETIME_FUNCS
58  
59 /*
60 ** A structure for holding a single date and time.
61 */
62 typedef struct DateTime DateTime;
63 struct DateTime {
64 double rJD; /* The julian day number */
65 int Y, M, D; /* Year, month, and day */
66 int h, m; /* Hour and minutes */
67 int tz; /* Timezone offset in minutes */
68 double s; /* Seconds */
69 char validYMD; /* True if Y,M,D are valid */
70 char validHMS; /* True if h,m,s are valid */
71 char validJD; /* True if rJD is valid */
72 char validTZ; /* True if tz is valid */
73 };
74  
75  
76 /*
77 ** Convert zDate into one or more integers. Additional arguments
78 ** come in groups of 5 as follows:
79 **
80 ** N number of digits in the integer
81 ** min minimum allowed value of the integer
82 ** max maximum allowed value of the integer
83 ** nextC first character after the integer
84 ** pVal where to write the integers value.
85 **
86 ** Conversions continue until one with nextC==0 is encountered.
87 ** The function returns the number of successful conversions.
88 */
89 0 static int getDigits(const char *zDate, ...){
90 va_list ap;
91 int val;
92 int N;
93 int min;
94 int max;
95 int nextC;
96 int *pVal;
97 0 int cnt = 0;
98 0 va_start(ap, zDate);
99 do{
100 0 N = va_arg(ap, int);
101 0 min = va_arg(ap, int);
102 0 max = va_arg(ap, int);
103 0 nextC = va_arg(ap, int);
104 0 pVal = va_arg(ap, int*);
105 0 val = 0;
106 0 ▶ while( N-- ){
Branch: 1 branch
BranchTrueFalseTotal
while( N-- ){ 0 0 0
107 0 ▶ if( !isdigit(*zDate) ){
Branch: 1 branch
BranchTrueFalseTotal
if( !isdigit(*zDate) ){ 0 0 0
108 0 return cnt;
109 }
110 0 val = val*10 + *zDate - '0';
111 0 zDate++;
112 }
113 0 ▶ if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
Branch: 4 branches
BranchTrueFalseTotal
if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ 0 0 0
if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ 0 0 0
if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ 0 0 0
if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ 0 0 0
114 0 return cnt;
115 }
116 0 *pVal = val;
117 0 zDate++;
118 0 cnt++;
119 0 ▶ }while( nextC );
Branch: 1 branch
BranchTrueFalseTotal
}while( nextC ); 0 0 0
120 0 return cnt;
121 }
122  
123 /*
124 ** Read text from z[] and convert into a floating point number. Return
125 ** the number of digits converted.
126 */
127 0 static int getValue(const char *z, double *pR){
128 const char *zEnd;
129 0 *pR = sqliteAtoF(z, &zEnd);
130 0 return zEnd - z;
131 }
132  
133 /*
134 ** Parse a timezone extension on the end of a date-time.
135 ** The extension is of the form:
136 **
137 ** (+/-)HH:MM
138 **
139 ** If the parse is successful, write the number of minutes
140 ** of change in *pnMin and return 0. If a parser error occurs,
141 ** return 0.
142 **
143 ** A missing specifier is not considered an error.
144 */
145 0 static int parseTimezone(const char *zDate, DateTime *p){
146 0 int sgn = 0;
147 int nHr, nMn;
148 0 ▶ while( isspace(*zDate) ){ zDate++; }
Branch: 1 branch
BranchTrueFalseTotal
while( isspace(*zDate) ){ zDate++; } 0 0 0
149 0 p->tz = 0;
150 0 ▶ if( *zDate=='-' ){
Branch: 1 branch
BranchTrueFalseTotal
if( *zDate=='-' ){ 0 0 0
151 0 sgn = -1;
152 0 ▶ }else if( *zDate=='+' ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( *zDate=='+' ){ 0 0 0
153 0 sgn = +1;
154 }else{
155 0 return *zDate!=0;
156 }
157 0 zDate++;
158 0 ▶ if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ 0 0 0
159 0 return 1;
160 }
161 0 zDate += 5;
162 0 p->tz = sgn*(nMn + nHr*60);
163 0 ▶ while( isspace(*zDate) ){ zDate++; }
Branch: 1 branch
BranchTrueFalseTotal
while( isspace(*zDate) ){ zDate++; } 0 0 0
164 0 return *zDate!=0;
165 }
166  
167 /*
168 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
169 ** The HH, MM, and SS must each be exactly 2 digits. The
170 ** fractional seconds FFFF can be one or more digits.
171 **
172 ** Return 1 if there is a parsing error and 0 on success.
173 */
174 0 static int parseHhMmSs(const char *zDate, DateTime *p){
175 int h, m, s;
176 0 double ms = 0.0;
177 0 ▶ if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ 0 0 0
178 0 return 1;
179 }
180 0 zDate += 5;
181 0 ▶ if( *zDate==':' ){
Branch: 1 branch
BranchTrueFalseTotal
if( *zDate==':' ){ 0 0 0
182 0 zDate++;
183 0 ▶ if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ 0 0 0
184 0 return 1;
185 }
186 0 zDate += 2;
187 0 ▶ if( *zDate=='.' && isdigit(zDate[1]) ){
Branch: 2 branches
BranchTrueFalseTotal
if( *zDate=='.' && isdigit(zDate[1]) ){ 0 0 0
if( *zDate=='.' && isdigit(zDate[1]) ){ 0 0 0
188 0 double rScale = 1.0;
189 0 zDate++;
190 0 ▶ while( isdigit(*zDate) ){
Branch: 1 branch
BranchTrueFalseTotal
while( isdigit(*zDate) ){ 0 0 0
191 0 ms = ms*10.0 + *zDate - '0';
192 0 rScale *= 10.0;
193 0 zDate++;
194 }
195 0 ms /= rScale;
196 }
197 }else{
198 0 s = 0;
199 }
200 0 p->validJD = 0;
201 0 p->validHMS = 1;
202 0 p->h = h;
203 0 p->m = m;
204 0 p->s = s + ms;
205 0 ▶ if( parseTimezone(zDate, p) ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( parseTimezone(zDate, p) ) return 1; 0 0 0
206 0 p->validTZ = p->tz!=0;
207 0 return 0;
208 }
209  
210 /*
211 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
212 ** that the YYYY-MM-DD is according to the Gregorian calendar.
213 **
214 ** Reference: Meeus page 61
215 */
216 0 static void computeJD(DateTime *p){
217 int Y, M, D, A, B, X1, X2;
218  
219 0 ▶ if( p->validJD ) return;
Branch: 1 branch
BranchTrueFalseTotal
if( p->validJD ) return; 0 0 0
220 0 ▶ if( p->validYMD ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->validYMD ){ 0 0 0
221 0 Y = p->Y;
222 0 M = p->M;
223 0 D = p->D;
224 }else{
225 0 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
226 0 M = 1;
227 0 D = 1;
228 }
229 0 ▶ if( M<=2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( M<=2 ){ 0 0 0
230 0 Y--;
231 0 M += 12;
232 }
233 0 A = Y/100;
234 0 B = 2 - A + (A/4);
235 0 X1 = 365.25*(Y+4716);
236 0 X2 = 30.6001*(M+1);
237 0 p->rJD = X1 + X2 + D + B - 1524.5;
238 0 p->validJD = 1;
239 0 p->validYMD = 0;
240 0 ▶ if( p->validHMS ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->validHMS ){ 0 0 0
241 0 p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0;
242 0 ▶ if( p->validTZ ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->validTZ ){ 0 0 0
243 0 p->rJD += p->tz*60/86400.0;
244 0 p->validHMS = 0;
245 0 p->validTZ = 0;
246 }
247 }
248 }
249  
250 /*
251 ** Parse dates of the form
252 **
253 ** YYYY-MM-DD HH:MM:SS.FFF
254 ** YYYY-MM-DD HH:MM:SS
255 ** YYYY-MM-DD HH:MM
256 ** YYYY-MM-DD
257 **
258 ** Write the result into the DateTime structure and return 0
259 ** on success and 1 if the input string is not a well-formed
260 ** date.
261 */
262 0 static int parseYyyyMmDd(const char *zDate, DateTime *p){
263 int Y, M, D, neg;
264  
265 0 ▶ if( zDate[0]=='-' ){
Branch: 1 branch
BranchTrueFalseTotal
if( zDate[0]=='-' ){ 0 0 0
266 0 zDate++;
267 0 neg = 1;
268 }else{
269 0 neg = 0;
270 }
271 0 ▶ if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
Branch: 1 branch
BranchTrueFalseTotal
if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ 0 0 0
272 0 return 1;
273 }
274 0 zDate += 10;
275 0 ▶ while( isspace(*zDate) ){ zDate++; }
Branch: 1 branch
BranchTrueFalseTotal
while( isspace(*zDate) ){ zDate++; } 0 0 0
276 0 ▶ if( parseHhMmSs(zDate, p)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( parseHhMmSs(zDate, p)==0 ){ 0 0 0
277 /* We got the time */
278 0 ▶ }else if( *zDate==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( *zDate==0 ){ 0 0 0
279 0 p->validHMS = 0;
280 }else{
281 0 return 1;
282 }
283 0 p->validJD = 0;
284 0 p->validYMD = 1;
285 0 ▶ p->Y = neg ? -Y : Y;
Branch: 1 branch
BranchTrueFalseTotal
p->Y = neg ? -Y : Y; 0 0 0
286 0 p->M = M;
287 0 p->D = D;
288 0 ▶ if( p->validTZ ){
Branch: 1 branch
BranchTrueFalseTotal
if( p->validTZ ){ 0 0 0
289 0 computeJD(p);
290 }
291 0 return 0;
292 }
293  
294 /*
295 ** Attempt to parse the given string into a Julian Day Number. Return
296 ** the number of errors.
297 **
298 ** The following are acceptable forms for the input string:
299 **
300 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
301 ** DDDD.DD
302 ** now
303 **
304 ** In the first form, the +/-HH:MM is always optional. The fractional
305 ** seconds extension (the ".FFF") is optional. The seconds portion
306 ** (":SS.FFF") is option. The year and date can be omitted as long
307 ** as there is a time string. The time string can be omitted as long
308 ** as there is a year and date.
309 */
310 0 static int parseDateOrTime(const char *zDate, DateTime *p){
311 0 memset(p, 0, sizeof(*p));
312 0 ▶ if( parseYyyyMmDd(zDate,p)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( parseYyyyMmDd(zDate,p)==0 ){ 0 0 0
313 0 return 0;
314 0 ▶ }else if( parseHhMmSs(zDate, p)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( parseHhMmSs(zDate, p)==0 ){ 0 0 0
315 0 return 0;
316 0 ▶ }else if( sqliteStrICmp(zDate,"now")==0){
Branch: 1 branch
BranchTrueFalseTotal
}else if( sqliteStrICmp(zDate,"now")==0){ 0 0 0
317 double r;
318 0 ▶ if( sqliteOsCurrentTime(&r)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( sqliteOsCurrentTime(&r)==0 ){ 0 0 0
319 0 p->rJD = r;
320 0 p->validJD = 1;
321 0 return 0;
322 }
323 0 return 1;
324 0 ▶ }else if( sqliteIsNumber(zDate) ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( sqliteIsNumber(zDate) ){ 0 0 0
325 0 p->rJD = sqliteAtoF(zDate, 0);
326 0 p->validJD = 1;
327 0 return 0;
328 }
329 0 return 1;
330 }
331  
332 /*
333 ** Compute the Year, Month, and Day from the julian day number.
334 */
335 0 static void computeYMD(DateTime *p){
336 int Z, A, B, C, D, E, X1;
337 0 ▶ if( p->validYMD ) return;
Branch: 1 branch
BranchTrueFalseTotal
if( p->validYMD ) return; 0 0 0
338 0 ▶ if( !p->validJD ){
Branch: 1 branch
BranchTrueFalseTotal
if( !p->validJD ){ 0 0 0
339 0 p->Y = 2000;
340 0 p->M = 1;
341 0 p->D = 1;
342 }else{
343 0 Z = p->rJD + 0.5;
344 0 A = (Z - 1867216.25)/36524.25;
345 0 A = Z + 1 + A - (A/4);
346 0 B = A + 1524;
347 0 C = (B - 122.1)/365.25;
348 0 D = 365.25*C;
349 0 E = (B-D)/30.6001;
350 0 X1 = 30.6001*E;
351 0 p->D = B - D - X1;
352 0 ▶ p->M = E<14 ? E-1 : E-13;
Branch: 1 branch
BranchTrueFalseTotal
p->M = E<14 ? E-1 : E-13; 0 0 0
353 0 ▶ p->Y = p->M>2 ? C - 4716 : C - 4715;
Branch: 1 branch
BranchTrueFalseTotal
p->Y = p->M>2 ? C - 4716 : C - 4715; 0 0 0
354 }
355 0 p->validYMD = 1;
356 }
357  
358 /*
359 ** Compute the Hour, Minute, and Seconds from the julian day number.
360 */
361 0 static void computeHMS(DateTime *p){
362 int Z, s;
363 0 ▶ if( p->validHMS ) return;
Branch: 1 branch
BranchTrueFalseTotal
if( p->validHMS ) return; 0 0 0
364 0 Z = p->rJD + 0.5;
365 0 s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5;
366 0 p->s = 0.001*s;
367 0 s = p->s;
368 0 p->s -= s;
369 0 p->h = s/3600;
370 0 s -= p->h*3600;
371 0 p->m = s/60;
372 0 p->s += s - p->m*60;
373 0 p->validHMS = 1;
374 }
375  
376 /*
377 ** Compute both YMD and HMS
378 */
379 0 static void computeYMD_HMS(DateTime *p){
380 0 computeYMD(p);
381 0 computeHMS(p);
382 0 }
383  
384 /*
385 ** Clear the YMD and HMS and the TZ
386 */
387 0 static void clearYMD_HMS_TZ(DateTime *p){
388 0 p->validYMD = 0;
389 0 p->validHMS = 0;
390 0 p->validTZ = 0;
391 0 }
392  
393 /*
394 ** Compute the difference (in days) between localtime and UTC (a.k.a. GMT)
395 ** for the time value p where p is in UTC.
396 */
397 0 static double localtimeOffset(DateTime *p){
398 DateTime x, y;
399 time_t t;
400 struct tm *pTm;
401 0 x = *p;
402 0 computeYMD_HMS(&x);
403 0 ▶ if( x.Y<1971 || x.Y>=2038 ){
Branch: 2 branches
BranchTrueFalseTotal
if( x.Y<1971 || x.Y>=2038 ){ 0 0 0
if( x.Y<1971 || x.Y>=2038 ){ 0 0 0
404 0 x.Y = 2000;
405 0 x.M = 1;
406 0 x.D = 1;
407 0 x.h = 0;
408 0 x.m = 0;
409 0 x.s = 0.0;
410 } else {
411 0 int s = x.s + 0.5;
412 0 x.s = s;
413 }
414 0 x.tz = 0;
415 0 x.validJD = 0;
416 0 computeJD(&x);
417 0 t = (x.rJD-2440587.5)*86400.0 + 0.5;
418 0 sqliteOsEnterMutex();
419 0 pTm = localtime(&t);
420 0 y.Y = pTm->tm_year + 1900;
421 0 y.M = pTm->tm_mon + 1;
422 0 y.D = pTm->tm_mday;
423 0 y.h = pTm->tm_hour;
424 0 y.m = pTm->tm_min;
425 0 y.s = pTm->tm_sec;
426 0 sqliteOsLeaveMutex();
427 0 y.validYMD = 1;
428 0 y.validHMS = 1;
429 0 y.validJD = 0;
430 0 y.validTZ = 0;
431 0 computeJD(&y);
432 0 return y.rJD - x.rJD;
433 }
434  
435 /*
436 ** Process a modifier to a date-time stamp. The modifiers are
437 ** as follows:
438 **
439 ** NNN days
440 ** NNN hours
441 ** NNN minutes
442 ** NNN.NNNN seconds
443 ** NNN months
444 ** NNN years
445 ** start of month
446 ** start of year
447 ** start of week
448 ** start of day
449 ** weekday N
450 ** unixepoch
451 ** localtime
452 ** utc
453 **
454 ** Return 0 on success and 1 if there is any kind of error.
455 */
456 0 static int parseModifier(const char *zMod, DateTime *p){
457 0 int rc = 1;
458 int n;
459 double r;
460 char *z, zBuf[30];
461 0 z = zBuf;
462 0 ▶ for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
Branch: 2 branches
BranchTrueFalseTotal
for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){ 0 0 0
for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){ 0 0 0
463 0 z[n] = tolower(zMod[n]);
464 }
465 0 z[n] = 0;
466 0 switch( z[0] ){
467 0 case 'l': {
468 /* localtime
469 **
470 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
471 ** show local time.
472 */
473 0 ▶ if( strcmp(z, "localtime")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( strcmp(z, "localtime")==0 ){ 0 0 0
474 0 computeJD(p);
475 0 p->rJD += localtimeOffset(p);
476 0 clearYMD_HMS_TZ(p);
477 0 rc = 0;
478 }
479 0 break;
480 }
481 0 case 'u': {
482 /*
483 ** unixepoch
484 **
485 ** Treat the current value of p->rJD as the number of
486 ** seconds since 1970. Convert to a real julian day number.
487 */
488 0 ▶ if( strcmp(z, "unixepoch")==0 && p->validJD ){
Branch: 2 branches
BranchTrueFalseTotal
if( strcmp(z, "unixepoch")==0 && p->validJD ){ 0 0 0
if( strcmp(z, "unixepoch")==0 && p->validJD ){ 0 0 0
489 0 p->rJD = p->rJD/86400.0 + 2440587.5;
490 0 clearYMD_HMS_TZ(p);
491 0 rc = 0;
492 0 ▶ }else if( strcmp(z, "utc")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( strcmp(z, "utc")==0 ){ 0 0 0
493 double c1;
494 0 computeJD(p);
495 0 c1 = localtimeOffset(p);
496 0 p->rJD -= c1;
497 0 clearYMD_HMS_TZ(p);
498 0 p->rJD += c1 - localtimeOffset(p);
499 0 rc = 0;
500 }
501 0 break;
502 }
503 0 case 'w': {
504 /*
505 ** weekday N
506 **
507 ** Move the date to the same time on the next occurrance of
508 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
509 ** date is already on the appropriate weekday, this is a no-op.
510 */
511 0 ▶ if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0
Branch: 2 branches
BranchTrueFalseTotal
if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0 0 0 0
if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0 0 0 0
512 0 ▶ && (n=r)==r && n>=0 && r<7 ){
Branch: 3 branches
BranchTrueFalseTotal
&& (n=r)==r && n>=0 && r<7 ){ 0 0 0
&& (n=r)==r && n>=0 && r<7 ){ 0 0 0
&& (n=r)==r && n>=0 && r<7 ){ 0 0 0
513 int Z;
514 0 computeYMD_HMS(p);
515 0 p->validTZ = 0;
516 0 p->validJD = 0;
517 0 computeJD(p);
518 0 Z = p->rJD + 1.5;
519 0 Z %= 7;
520 0 ▶ if( Z>n ) Z -= 7;
Branch: 1 branch
BranchTrueFalseTotal
if( Z>n ) Z -= 7; 0 0 0
521 0 p->rJD += n - Z;
522 0 clearYMD_HMS_TZ(p);
523 0 rc = 0;
524 }
525 0 break;
526 }
527 0 case 's': {
528 /*
529 ** start of TTTTT
530 **
531 ** Move the date backwards to the beginning of the current day,
532 ** or month or year.
533 */
534 0 ▶ if( strncmp(z, "start of ", 9)!=0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( strncmp(z, "start of ", 9)!=0 ) break; 0 0 0
535 0 z += 9;
536 0 computeYMD(p);
537 0 p->validHMS = 1;
538 0 p->h = p->m = 0;
539 0 p->s = 0.0;
540 0 p->validTZ = 0;
541 0 p->validJD = 0;
542 0 ▶ if( strcmp(z,"month")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( strcmp(z,"month")==0 ){ 0 0 0
543 0 p->D = 1;
544 0 rc = 0;
545 0 ▶ }else if( strcmp(z,"year")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( strcmp(z,"year")==0 ){ 0 0 0
546 0 computeYMD(p);
547 0 p->M = 1;
548 0 p->D = 1;
549 0 rc = 0;
550 0 ▶ }else if( strcmp(z,"day")==0 ){
Branch: 1 branch
BranchTrueFalseTotal
}else if( strcmp(z,"day")==0 ){ 0 0 0
551 0 rc = 0;
552 }
553 0 break;
554 }
555 0 case '+':
556 case '-':
557 case '0':
558 case '1':
559 case '2':
560 case '3':
561 case '4':
562 case '5':
563 case '6':
564 case '7':
565 case '8':
566 case '9': {
567 0 n = getValue(z, &r);
568 0 ▶ if( n<=0 ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( n<=0 ) break; 0 0 0
569 0 ▶ if( z[n]==':' ){
Branch: 1 branch
BranchTrueFalseTotal
if( z[n]==':' ){ 0 0 0
570 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
571 ** specified number of hours, minutes, seconds, and fractional seconds
572 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
573 ** omitted.
574 */
575 0 const char *z2 = z;
576 DateTime tx;
577 int day;
578 0 ▶ if( !isdigit(*z2) ) z2++;
Branch: 1 branch
BranchTrueFalseTotal
if( !isdigit(*z2) ) z2++; 0 0 0
579 0 memset(&tx, 0, sizeof(tx));
580 0 ▶ if( parseHhMmSs(z2, &tx) ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( parseHhMmSs(z2, &tx) ) break; 0 0 0
581 0 computeJD(&tx);
582 0 tx.rJD -= 0.5;
583 0 day = (int)tx.rJD;
584 0 tx.rJD -= day;
585 0 ▶ if( z[0]=='-' ) tx.rJD = -tx.rJD;
Branch: 1 branch
BranchTrueFalseTotal
if( z[0]=='-' ) tx.rJD = -tx.rJD; 0 0 0
586 0 computeJD(p);
587 0 clearYMD_HMS_TZ(p);
588 0 p->rJD += tx.rJD;
589 0 rc = 0;
590 0 break;
591 }
592 0 z += n;
593 0 ▶ while( isspace(z[0]) ) z++;
Branch: 1 branch
BranchTrueFalseTotal
while( isspace(z[0]) ) z++; 0 0 0
594 0 n = strlen(z);
595 0 ▶ if( n>10 || n<3 ) break;
Branch: 2 branches
BranchTrueFalseTotal
if( n>10 || n<3 ) break; 0 0 0
if( n>10 || n<3 ) break; 0 0 0
596 0 ▶ if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
Branch: 1 branch
BranchTrueFalseTotal
if( z[n-1]=='s' ){ z[n-1] = 0; n--; } 0 0 0
597 0 computeJD(p);
598 0 rc = 0;
599 0 ▶ if( n==3 && strcmp(z,"day")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( n==3 && strcmp(z,"day")==0 ){ 0 0 0
if( n==3 && strcmp(z,"day")==0 ){ 0 0 0
600 0 p->rJD += r;
601 0 ▶ }else if( n==4 && strcmp(z,"hour")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( n==4 && strcmp(z,"hour")==0 ){ 0 0 0
}else if( n==4 && strcmp(z,"hour")==0 ){ 0 0 0
602 0 p->rJD += r/24.0;
603 0 ▶ }else if( n==6 && strcmp(z,"minute")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( n==6 && strcmp(z,"minute")==0 ){ 0 0 0
}else if( n==6 && strcmp(z,"minute")==0 ){ 0 0 0
604 0 p->rJD += r/(24.0*60.0);
605 0 ▶ }else if( n==6 && strcmp(z,"second")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( n==6 && strcmp(z,"second")==0 ){ 0 0 0
}else if( n==6 && strcmp(z,"second")==0 ){ 0 0 0
606 0 p->rJD += r/(24.0*60.0*60.0);
607 0 ▶ }else if( n==5 && strcmp(z,"month")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( n==5 && strcmp(z,"month")==0 ){ 0 0 0
}else if( n==5 && strcmp(z,"month")==0 ){ 0 0 0
608 int x, y;
609 0 computeYMD_HMS(p);
610 0 p->M += r;
611 0 ▶ x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
Branch: 1 branch
BranchTrueFalseTotal
x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; 0 0 0
612 0 p->Y += x;
613 0 p->M -= x*12;
614 0 p->validJD = 0;
615 0 computeJD(p);
616 0 y = r;
617 0 ▶ if( y!=r ){
Branch: 1 branch
BranchTrueFalseTotal
if( y!=r ){ 0 0 0
618 0 p->rJD += (r - y)*30.0;
619 }
620 0 ▶ }else if( n==4 && strcmp(z,"year")==0 ){
Branch: 2 branches
BranchTrueFalseTotal
}else if( n==4 && strcmp(z,"year")==0 ){ 0 0 0
}else if( n==4 && strcmp(z,"year")==0 ){ 0 0 0
621 0 computeYMD_HMS(p);
622 0 p->Y += r;
623 0 p->validJD = 0;
624 0 computeJD(p);
625 }else{
626 0 rc = 1;
627 }
628 0 clearYMD_HMS_TZ(p);
629 0 break;
630 }
631 0 default: {
632 0 break;
633 }
634 }
635 0 return rc;
636 }
637  
638 /*
639 ** Process time function arguments. argv[0] is a date-time stamp.
640 ** argv[1] and following are modifiers. Parse them all and write
641 ** the resulting time into the DateTime structure p. Return 0
642 ** on success and 1 if there are any errors.
643 */
644 0 static int isDate(int argc, const char **argv, DateTime *p){
645 int i;
646 0 ▶ if( argc==0 ) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if( argc==0 ) return 1; 0 0 0
647 0 ▶ if( argv[0]==0 || parseDateOrTime(argv[0], p) ) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if( argv[0]==0 || parseDateOrTime(argv[0], p) ) return 1; 0 0 0
if( argv[0]==0 || parseDateOrTime(argv[0], p) ) return 1; 0 0 0
648 0 ▶ for(i=1; i<argc; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=1; i<argc; i++){ 0 0 0
649 0 ▶ if( argv[i]==0 || parseModifier(argv[i], p) ) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if( argv[i]==0 || parseModifier(argv[i], p) ) return 1; 0 0 0
if( argv[i]==0 || parseModifier(argv[i], p) ) return 1; 0 0 0
650 }
651 0 return 0;
652 }
653  
654  
655 /*
656 ** The following routines implement the various date and time functions
657 ** of SQLite.
658 */
659  
660 /*
661 ** julianday( TIMESTRING, MOD, MOD, ...)
662 **
663 ** Return the julian day number of the date specified in the arguments
664 */
665 0 static void juliandayFunc(sqlite_func *context, int argc, const char **argv){
666 DateTime x;
667 0 ▶ if( isDate(argc, argv, &x)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( isDate(argc, argv, &x)==0 ){ 0 0 0
668 0 computeJD(&x);
669 0 sqlite_set_result_double(context, x.rJD);
670 }
671 0 }
672  
673 /*
674 ** datetime( TIMESTRING, MOD, MOD, ...)
675 **
676 ** Return YYYY-MM-DD HH:MM:SS
677 */
678 0 static void datetimeFunc(sqlite_func *context, int argc, const char **argv){
679 DateTime x;
680 0 ▶ if( isDate(argc, argv, &x)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( isDate(argc, argv, &x)==0 ){ 0 0 0
681 char zBuf[100];
682 0 computeYMD_HMS(&x);
683 0 sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
684 0 (int)(x.s));
685 0 sqlite_set_result_string(context, zBuf, -1);
686 }
687 0 }
688  
689 /*
690 ** time( TIMESTRING, MOD, MOD, ...)
691 **
692 ** Return HH:MM:SS
693 */
694 0 static void timeFunc(sqlite_func *context, int argc, const char **argv){
695 DateTime x;
696 0 ▶ if( isDate(argc, argv, &x)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( isDate(argc, argv, &x)==0 ){ 0 0 0
697 char zBuf[100];
698 0 computeHMS(&x);
699 0 sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
700 0 sqlite_set_result_string(context, zBuf, -1);
701 }
702 0 }
703  
704 /*
705 ** date( TIMESTRING, MOD, MOD, ...)
706 **
707 ** Return YYYY-MM-DD
708 */
709 0 static void dateFunc(sqlite_func *context, int argc, const char **argv){
710 DateTime x;
711 0 ▶ if( isDate(argc, argv, &x)==0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( isDate(argc, argv, &x)==0 ){ 0 0 0
712 char zBuf[100];
713 0 computeYMD(&x);
714 0 sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
715 0 sqlite_set_result_string(context, zBuf, -1);
716 }
717 0 }
718  
719 /*
720 ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
721 **
722 ** Return a string described by FORMAT. Conversions as follows:
723 **
724 ** %d day of month
725 ** %f ** fractional seconds SS.SSS
726 ** %H hour 00-24
727 ** %j day of year 000-366
728 ** %J ** Julian day number
729 ** %m month 01-12
730 ** %M minute 00-59
731 ** %s seconds since 1970-01-01
732 ** %S seconds 00-59
733 ** %w day of week 0-6 sunday==0
734 ** %W week of year 00-53
735 ** %Y year 0000-9999
736 ** %% %
737 */
738 0 static void strftimeFunc(sqlite_func *context, int argc, const char **argv){
739 DateTime x;
740 int n, i, j;
741 char *z;
742 0 const char *zFmt = argv[0];
743 char zBuf[100];
744 0 ▶ if( argv[0]==0 || isDate(argc-1, argv+1, &x) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if( argv[0]==0 || isDate(argc-1, argv+1, &x) ) return; 0 0 0
if( argv[0]==0 || isDate(argc-1, argv+1, &x) ) return; 0 0 0
745 0 ▶ for(i=0, n=1; zFmt[i]; i++, n++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0, n=1; zFmt[i]; i++, n++){ 0 0 0
746 0 ▶ if( zFmt[i]=='%' ){
Branch: 1 branch
BranchTrueFalseTotal
if( zFmt[i]=='%' ){ 0 0 0
747 0 switch( zFmt[i+1] ){
748 0 case 'd':
749 case 'H':
750 case 'm':
751 case 'M':
752 case 'S':
753 case 'W':
754 0 n++;
755 /* fall thru */
756 0 case 'w':
757 case '%':
758 0 break;
759 0 case 'f':
760 0 n += 8;
761 0 break;
762 0 case 'j':
763 0 n += 3;
764 0 break;
765 0 case 'Y':
766 0 n += 8;
767 0 break;
768 0 case 's':
769 case 'J':
770 0 n += 50;
771 0 break;
772 0 default:
773 0 return; /* ERROR. return a NULL */
774 }
775 0 i++;
776 }
777 }
778 0 ▶ if( n<sizeof(zBuf) ){
Branch: 1 branch
BranchTrueFalseTotal
if( n<sizeof(zBuf) ){ 0 0 0
779 0 z = zBuf;
780 }else{
781 0 z = sqliteMalloc( n );
782 0 ▶ if( z==0 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if( z==0 ) return; 0 0 0
783 }
784 0 computeJD(&x);
785 0 computeYMD_HMS(&x);
786 0 ▶ for(i=j=0; zFmt[i]; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=j=0; zFmt[i]; i++){ 0 0 0
787 0 ▶ if( zFmt[i]!='%' ){
Branch: 1 branch
BranchTrueFalseTotal
if( zFmt[i]!='%' ){ 0 0 0
788 0 z[j++] = zFmt[i];
789 }else{
790 0 i++;
791 0 switch( zFmt[i] ){
792 0 case 'd': sprintf(&z[j],"%02d",x.D); j+=2; break;
793 0 case 'f': {
794 0 int s = x.s;
795 0 int ms = (x.s - s)*1000.0;
796 0 sprintf(&z[j],"%02d.%03d",s,ms);
797 0 j += strlen(&z[j]);
798 0 break;
799 }
800 0 case 'H': sprintf(&z[j],"%02d",x.h); j+=2; break;
801 0 case 'W': /* Fall thru */
802 case 'j': {
803 int n; /* Number of days since 1st day of year */
804 0 DateTime y = x;
805 0 y.validJD = 0;
806 0 y.M = 1;
807 0 y.D = 1;
808 0 computeJD(&y);
809 0 n = x.rJD - y.rJD;
810 0 ▶ if( zFmt[i]=='W' ){
Branch: 1 branch
BranchTrueFalseTotal
if( zFmt[i]=='W' ){ 0 0 0
811 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
812 0 wd = ((int)(x.rJD+0.5)) % 7;
813 0 sprintf(&z[j],"%02d",(n+7-wd)/7);
814 0 j += 2;
815 }else{
816 0 sprintf(&z[j],"%03d",n+1);
817 0 j += 3;
818 }
819 0 break;
820 }
821 0 case 'J': sprintf(&z[j],"%.16g",x.rJD); j+=strlen(&z[j]); break;
822 0 case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break;
823 0 case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break;
824 0 case 's': {
825 0 sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5));
826 0 j += strlen(&z[j]);
827 0 break;
828 }
829 0 case 'S': sprintf(&z[j],"%02d",(int)(x.s+0.5)); j+=2; break;
830 0 case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break;
831 0 case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break;
832 0 case '%': z[j++] = '%'; break;
833 }
834 }
835 }
836 0 z[j] = 0;
837 0 sqlite_set_result_string(context, z, -1);
838 0 ▶ if( z!=zBuf ){
Branch: 1 branch
BranchTrueFalseTotal
if( z!=zBuf ){ 0 0 0
839 0 sqliteFree(z);
840 }
841 }
842  
843  
844 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
845  
846 /*
847 ** This function registered all of the above C functions as SQL
848 ** functions. This should be the only routine in this file with
849 ** external linkage.
850 */
851 25 void sqliteRegisterDateTimeFunctions(sqlite *db){
852 #ifndef SQLITE_OMIT_DATETIME_FUNCS
853 static struct {
854 char *zName;
855 int nArg;
856 int dataType;
857 void (*xFunc)(sqlite_func*,int,const char**);
858 } aFuncs[] = {
859 { "julianday", -1, SQLITE_NUMERIC, juliandayFunc },
860 { "date", -1, SQLITE_TEXT, dateFunc },
861 { "time", -1, SQLITE_TEXT, timeFunc },
862 { "datetime", -1, SQLITE_TEXT, datetimeFunc },
863 { "strftime", -1, SQLITE_TEXT, strftimeFunc },
864 };
865 int i;
866  
867 150 ▶ for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 125 25 150
868 125 sqlite_create_function(db, aFuncs[i].zName,
869 aFuncs[i].nArg, aFuncs[i].xFunc, 0);
870 125 ▶ if( aFuncs[i].xFunc ){
Branch: 1 branch
BranchTrueFalseTotal
if( aFuncs[i].xFunc ){ 125 0 125
871 125 sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
872 }
873 }
874 #endif
875 25 }
« trigger.c ↑ Summary pragma.c »