drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 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 |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 16 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 17 | ** All other code has file scope. |
| 18 | ** |
drh | 181fc99 | 2004-08-17 10:42:54 +0000 | [diff] [blame^] | 19 | ** $Id: date.c,v 1.34 2004/08/17 10:42:55 drh Exp $ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 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 | */ |
dougcurrie | ae53418 | 2003-12-24 01:41:19 +0000 | [diff] [blame] | 50 | #include "os.h" |
| 51 | #include "sqliteInt.h" |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 52 | #include <ctype.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <assert.h> |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 55 | #include <time.h> |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 56 | |
drh | 4bc0585 | 2004-02-10 13:19:35 +0000 | [diff] [blame] | 57 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
| 58 | |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 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 | /* |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 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. |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 88 | */ |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 89 | 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 | int cnt = 0; |
| 98 | va_start(ap, zDate); |
| 99 | do{ |
| 100 | N = va_arg(ap, int); |
| 101 | min = va_arg(ap, int); |
| 102 | max = va_arg(ap, int); |
| 103 | nextC = va_arg(ap, int); |
| 104 | pVal = va_arg(ap, int*); |
| 105 | val = 0; |
| 106 | while( N-- ){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 107 | if( !isdigit(*(u8*)zDate) ){ |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 108 | return cnt; |
| 109 | } |
| 110 | val = val*10 + *zDate - '0'; |
| 111 | zDate++; |
| 112 | } |
| 113 | if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ |
| 114 | return cnt; |
| 115 | } |
| 116 | *pVal = val; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 117 | zDate++; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 118 | cnt++; |
| 119 | }while( nextC ); |
| 120 | return cnt; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | ** Read text from z[] and convert into a floating point number. Return |
| 125 | ** the number of digits converted. |
| 126 | */ |
| 127 | static int getValue(const char *z, double *pR){ |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 128 | const char *zEnd; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 129 | *pR = sqlite3AtoF(z, &zEnd); |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 130 | return zEnd - z; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 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 | static int parseTimezone(const char *zDate, DateTime *p){ |
| 146 | int sgn = 0; |
| 147 | int nHr, nMn; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 148 | while( isspace(*(u8*)zDate) ){ zDate++; } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 149 | p->tz = 0; |
| 150 | if( *zDate=='-' ){ |
| 151 | sgn = -1; |
| 152 | }else if( *zDate=='+' ){ |
| 153 | sgn = +1; |
| 154 | }else{ |
| 155 | return *zDate!=0; |
| 156 | } |
| 157 | zDate++; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 158 | if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ |
| 159 | return 1; |
| 160 | } |
| 161 | zDate += 5; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 162 | p->tz = sgn*(nMn + nHr*60); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 163 | while( isspace(*(u8*)zDate) ){ zDate++; } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 164 | 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 | static int parseHhMmSs(const char *zDate, DateTime *p){ |
| 175 | int h, m, s; |
| 176 | double ms = 0.0; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 177 | if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ |
| 178 | return 1; |
| 179 | } |
| 180 | zDate += 5; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 181 | if( *zDate==':' ){ |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 182 | zDate++; |
| 183 | if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ |
| 184 | return 1; |
| 185 | } |
| 186 | zDate += 2; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 187 | if( *zDate=='.' && isdigit((u8)zDate[1]) ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 188 | double rScale = 1.0; |
| 189 | zDate++; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 190 | while( isdigit(*(u8*)zDate) ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 191 | ms = ms*10.0 + *zDate - '0'; |
| 192 | rScale *= 10.0; |
| 193 | zDate++; |
| 194 | } |
| 195 | ms /= rScale; |
| 196 | } |
| 197 | }else{ |
| 198 | s = 0; |
| 199 | } |
| 200 | p->validJD = 0; |
| 201 | p->validHMS = 1; |
| 202 | p->h = h; |
| 203 | p->m = m; |
| 204 | p->s = s + ms; |
| 205 | if( parseTimezone(zDate, p) ) return 1; |
| 206 | p->validTZ = p->tz!=0; |
| 207 | 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 | static void computeJD(DateTime *p){ |
| 217 | int Y, M, D, A, B, X1, X2; |
| 218 | |
| 219 | if( p->validJD ) return; |
| 220 | if( p->validYMD ){ |
| 221 | Y = p->Y; |
| 222 | M = p->M; |
| 223 | D = p->D; |
| 224 | }else{ |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 225 | Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 226 | M = 1; |
| 227 | D = 1; |
| 228 | } |
| 229 | if( M<=2 ){ |
| 230 | Y--; |
| 231 | M += 12; |
| 232 | } |
| 233 | A = Y/100; |
| 234 | B = 2 - A + (A/4); |
| 235 | X1 = 365.25*(Y+4716); |
| 236 | X2 = 30.6001*(M+1); |
| 237 | p->rJD = X1 + X2 + D + B - 1524.5; |
| 238 | p->validJD = 1; |
| 239 | p->validYMD = 0; |
| 240 | if( p->validHMS ){ |
| 241 | p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0; |
| 242 | if( p->validTZ ){ |
| 243 | p->rJD += p->tz*60/86400.0; |
| 244 | p->validHMS = 0; |
| 245 | 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 | static int parseYyyyMmDd(const char *zDate, DateTime *p){ |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 263 | int Y, M, D, neg; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 264 | |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 265 | if( zDate[0]=='-' ){ |
| 266 | zDate++; |
| 267 | neg = 1; |
| 268 | }else{ |
| 269 | neg = 0; |
| 270 | } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 271 | if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ |
| 272 | return 1; |
| 273 | } |
| 274 | zDate += 10; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 275 | while( isspace(*(u8*)zDate) ){ zDate++; } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 276 | if( parseHhMmSs(zDate, p)==0 ){ |
| 277 | /* We got the time */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 278 | }else if( *zDate==0 ){ |
| 279 | p->validHMS = 0; |
| 280 | }else{ |
| 281 | return 1; |
| 282 | } |
| 283 | p->validJD = 0; |
| 284 | p->validYMD = 1; |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 285 | p->Y = neg ? -Y : Y; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 286 | p->M = M; |
| 287 | p->D = D; |
| 288 | if( p->validTZ ){ |
| 289 | computeJD(p); |
| 290 | } |
| 291 | 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 | static int parseDateOrTime(const char *zDate, DateTime *p){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 311 | memset(p, 0, sizeof(*p)); |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 312 | if( parseYyyyMmDd(zDate,p)==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 313 | return 0; |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 314 | }else if( parseHhMmSs(zDate, p)==0 ){ |
| 315 | return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 316 | }else if( sqlite3StrICmp(zDate,"now")==0){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 317 | double r; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 318 | if( sqlite3OsCurrentTime(&r)==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 319 | p->rJD = r; |
| 320 | p->validJD = 1; |
| 321 | return 0; |
| 322 | } |
| 323 | return 1; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 324 | }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 325 | p->rJD = sqlite3AtoF(zDate, 0); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 326 | p->validJD = 1; |
| 327 | return 0; |
| 328 | } |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | ** Compute the Year, Month, and Day from the julian day number. |
| 334 | */ |
| 335 | static void computeYMD(DateTime *p){ |
| 336 | int Z, A, B, C, D, E, X1; |
| 337 | if( p->validYMD ) return; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 338 | if( !p->validJD ){ |
| 339 | p->Y = 2000; |
| 340 | p->M = 1; |
| 341 | p->D = 1; |
| 342 | }else{ |
| 343 | Z = p->rJD + 0.5; |
| 344 | A = (Z - 1867216.25)/36524.25; |
| 345 | A = Z + 1 + A - (A/4); |
| 346 | B = A + 1524; |
| 347 | C = (B - 122.1)/365.25; |
| 348 | D = 365.25*C; |
| 349 | E = (B-D)/30.6001; |
| 350 | X1 = 30.6001*E; |
| 351 | p->D = B - D - X1; |
| 352 | p->M = E<14 ? E-1 : E-13; |
| 353 | p->Y = p->M>2 ? C - 4716 : C - 4715; |
| 354 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 355 | p->validYMD = 1; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | ** Compute the Hour, Minute, and Seconds from the julian day number. |
| 360 | */ |
| 361 | static void computeHMS(DateTime *p){ |
| 362 | int Z, s; |
| 363 | if( p->validHMS ) return; |
| 364 | Z = p->rJD + 0.5; |
| 365 | s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5; |
| 366 | p->s = 0.001*s; |
| 367 | s = p->s; |
| 368 | p->s -= s; |
| 369 | p->h = s/3600; |
| 370 | s -= p->h*3600; |
| 371 | p->m = s/60; |
| 372 | p->s += s - p->m*60; |
| 373 | p->validHMS = 1; |
| 374 | } |
| 375 | |
| 376 | /* |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 377 | ** Compute both YMD and HMS |
| 378 | */ |
| 379 | static void computeYMD_HMS(DateTime *p){ |
| 380 | computeYMD(p); |
| 381 | computeHMS(p); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | ** Clear the YMD and HMS and the TZ |
| 386 | */ |
| 387 | static void clearYMD_HMS_TZ(DateTime *p){ |
| 388 | p->validYMD = 0; |
| 389 | p->validHMS = 0; |
| 390 | p->validTZ = 0; |
| 391 | } |
| 392 | |
| 393 | /* |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 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 | static double localtimeOffset(DateTime *p){ |
| 398 | DateTime x, y; |
| 399 | time_t t; |
| 400 | struct tm *pTm; |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 401 | x = *p; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 402 | computeYMD_HMS(&x); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 403 | if( x.Y<1971 || x.Y>=2038 ){ |
| 404 | x.Y = 2000; |
| 405 | x.M = 1; |
| 406 | x.D = 1; |
| 407 | x.h = 0; |
| 408 | x.m = 0; |
| 409 | x.s = 0.0; |
| 410 | } else { |
| 411 | int s = x.s + 0.5; |
| 412 | x.s = s; |
| 413 | } |
| 414 | x.tz = 0; |
| 415 | x.validJD = 0; |
| 416 | computeJD(&x); |
| 417 | t = (x.rJD-2440587.5)*86400.0 + 0.5; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 418 | sqlite3OsEnterMutex(); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 419 | pTm = localtime(&t); |
| 420 | y.Y = pTm->tm_year + 1900; |
| 421 | y.M = pTm->tm_mon + 1; |
| 422 | y.D = pTm->tm_mday; |
| 423 | y.h = pTm->tm_hour; |
| 424 | y.m = pTm->tm_min; |
| 425 | y.s = pTm->tm_sec; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 426 | sqlite3OsLeaveMutex(); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 427 | y.validYMD = 1; |
| 428 | y.validHMS = 1; |
| 429 | y.validJD = 0; |
| 430 | y.validTZ = 0; |
| 431 | computeJD(&y); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 432 | return y.rJD - x.rJD; |
| 433 | } |
| 434 | |
| 435 | /* |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 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 |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 451 | ** localtime |
| 452 | ** utc |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 453 | ** |
| 454 | ** Return 0 on success and 1 if there is any kind of error. |
| 455 | */ |
| 456 | static int parseModifier(const char *zMod, DateTime *p){ |
| 457 | int rc = 1; |
| 458 | int n; |
| 459 | double r; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 460 | char *z, zBuf[30]; |
| 461 | z = zBuf; |
| 462 | for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 463 | z[n] = tolower(zMod[n]); |
| 464 | } |
| 465 | z[n] = 0; |
| 466 | switch( z[0] ){ |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 467 | 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 | if( strcmp(z, "localtime")==0 ){ |
| 474 | computeJD(p); |
| 475 | p->rJD += localtimeOffset(p); |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 476 | clearYMD_HMS_TZ(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 477 | rc = 0; |
| 478 | } |
| 479 | break; |
| 480 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 481 | 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 | if( strcmp(z, "unixepoch")==0 && p->validJD ){ |
| 489 | p->rJD = p->rJD/86400.0 + 2440587.5; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 490 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 491 | rc = 0; |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 492 | }else if( strcmp(z, "utc")==0 ){ |
| 493 | double c1; |
| 494 | computeJD(p); |
| 495 | c1 = localtimeOffset(p); |
| 496 | p->rJD -= c1; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 497 | clearYMD_HMS_TZ(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 498 | p->rJD += c1 - localtimeOffset(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 499 | rc = 0; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 500 | } |
| 501 | break; |
| 502 | } |
| 503 | case 'w': { |
| 504 | /* |
| 505 | ** weekday N |
| 506 | ** |
drh | 181fc99 | 2004-08-17 10:42:54 +0000 | [diff] [blame^] | 507 | ** Move the date to the same time on the next occurrence of |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 508 | ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 509 | ** date is already on the appropriate weekday, this is a no-op. |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 510 | */ |
| 511 | if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0 |
| 512 | && (n=r)==r && n>=0 && r<7 ){ |
| 513 | int Z; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 514 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 515 | p->validTZ = 0; |
| 516 | p->validJD = 0; |
| 517 | computeJD(p); |
| 518 | Z = p->rJD + 1.5; |
| 519 | Z %= 7; |
| 520 | if( Z>n ) Z -= 7; |
| 521 | p->rJD += n - Z; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 522 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 523 | rc = 0; |
| 524 | } |
| 525 | break; |
| 526 | } |
| 527 | 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 | if( strncmp(z, "start of ", 9)!=0 ) break; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 535 | z += 9; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 536 | computeYMD(p); |
| 537 | p->validHMS = 1; |
| 538 | p->h = p->m = 0; |
| 539 | p->s = 0.0; |
| 540 | p->validTZ = 0; |
| 541 | p->validJD = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 542 | if( strcmp(z,"month")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 543 | p->D = 1; |
| 544 | rc = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 545 | }else if( strcmp(z,"year")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 546 | computeYMD(p); |
| 547 | p->M = 1; |
| 548 | p->D = 1; |
| 549 | rc = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 550 | }else if( strcmp(z,"day")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 551 | rc = 0; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | 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 | n = getValue(z, &r); |
| 568 | if( n<=0 ) break; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 569 | if( z[n]==':' ){ |
| 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 | const char *z2 = z; |
| 576 | DateTime tx; |
| 577 | int day; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 578 | if( !isdigit(*(u8*)z2) ) z2++; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 579 | memset(&tx, 0, sizeof(tx)); |
| 580 | if( parseHhMmSs(z2, &tx) ) break; |
| 581 | computeJD(&tx); |
drh | 0d131ab | 2004-02-29 01:08:17 +0000 | [diff] [blame] | 582 | tx.rJD -= 0.5; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 583 | day = (int)tx.rJD; |
drh | b6829e9 | 2004-02-29 00:50:33 +0000 | [diff] [blame] | 584 | tx.rJD -= day; |
drh | b6829e9 | 2004-02-29 00:50:33 +0000 | [diff] [blame] | 585 | if( z[0]=='-' ) tx.rJD = -tx.rJD; |
drh | 0d131ab | 2004-02-29 01:08:17 +0000 | [diff] [blame] | 586 | computeJD(p); |
| 587 | clearYMD_HMS_TZ(p); |
| 588 | p->rJD += tx.rJD; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 589 | rc = 0; |
| 590 | break; |
| 591 | } |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 592 | z += n; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 593 | while( isspace(*(u8*)z) ) z++; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 594 | n = strlen(z); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 595 | if( n>10 || n<3 ) break; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 596 | if( z[n-1]=='s' ){ z[n-1] = 0; n--; } |
| 597 | computeJD(p); |
| 598 | rc = 0; |
| 599 | if( n==3 && strcmp(z,"day")==0 ){ |
| 600 | p->rJD += r; |
| 601 | }else if( n==4 && strcmp(z,"hour")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 602 | p->rJD += r/24.0; |
| 603 | }else if( n==6 && strcmp(z,"minute")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 604 | p->rJD += r/(24.0*60.0); |
| 605 | }else if( n==6 && strcmp(z,"second")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 606 | p->rJD += r/(24.0*60.0*60.0); |
| 607 | }else if( n==5 && strcmp(z,"month")==0 ){ |
| 608 | int x, y; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 609 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 610 | p->M += r; |
| 611 | x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; |
| 612 | p->Y += x; |
| 613 | p->M -= x*12; |
| 614 | p->validJD = 0; |
| 615 | computeJD(p); |
| 616 | y = r; |
| 617 | if( y!=r ){ |
| 618 | p->rJD += (r - y)*30.0; |
| 619 | } |
| 620 | }else if( n==4 && strcmp(z,"year")==0 ){ |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 621 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 622 | p->Y += r; |
| 623 | p->validJD = 0; |
| 624 | computeJD(p); |
| 625 | }else{ |
| 626 | rc = 1; |
| 627 | } |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 628 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | default: { |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | 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 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 644 | static int isDate(int argc, sqlite3_value **argv, DateTime *p){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 645 | int i; |
| 646 | if( argc==0 ) return 1; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 647 | if( SQLITE_NULL==sqlite3_value_type(argv[0]) || |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 648 | parseDateOrTime(sqlite3_value_text(argv[0]), p) ) return 1; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 649 | for(i=1; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 650 | if( SQLITE_NULL==sqlite3_value_type(argv[i]) || |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 651 | parseModifier(sqlite3_value_text(argv[i]), p) ) return 1; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /* |
| 658 | ** The following routines implement the various date and time functions |
| 659 | ** of SQLite. |
| 660 | */ |
| 661 | |
| 662 | /* |
| 663 | ** julianday( TIMESTRING, MOD, MOD, ...) |
| 664 | ** |
| 665 | ** Return the julian day number of the date specified in the arguments |
| 666 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 667 | static void juliandayFunc( |
| 668 | sqlite3_context *context, |
| 669 | int argc, |
| 670 | sqlite3_value **argv |
| 671 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 672 | DateTime x; |
| 673 | if( isDate(argc, argv, &x)==0 ){ |
| 674 | computeJD(&x); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 675 | sqlite3_result_double(context, x.rJD); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | ** datetime( TIMESTRING, MOD, MOD, ...) |
| 681 | ** |
| 682 | ** Return YYYY-MM-DD HH:MM:SS |
| 683 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 684 | static void datetimeFunc( |
| 685 | sqlite3_context *context, |
| 686 | int argc, |
| 687 | sqlite3_value **argv |
| 688 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 689 | DateTime x; |
| 690 | if( isDate(argc, argv, &x)==0 ){ |
| 691 | char zBuf[100]; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 692 | computeYMD_HMS(&x); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 693 | sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m, |
| 694 | (int)(x.s)); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 695 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | ** time( TIMESTRING, MOD, MOD, ...) |
| 701 | ** |
| 702 | ** Return HH:MM:SS |
| 703 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 704 | static void timeFunc( |
| 705 | sqlite3_context *context, |
| 706 | int argc, |
| 707 | sqlite3_value **argv |
| 708 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 709 | DateTime x; |
| 710 | if( isDate(argc, argv, &x)==0 ){ |
| 711 | char zBuf[100]; |
| 712 | computeHMS(&x); |
| 713 | sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 714 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | ** date( TIMESTRING, MOD, MOD, ...) |
| 720 | ** |
| 721 | ** Return YYYY-MM-DD |
| 722 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 723 | static void dateFunc( |
| 724 | sqlite3_context *context, |
| 725 | int argc, |
| 726 | sqlite3_value **argv |
| 727 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 728 | DateTime x; |
| 729 | if( isDate(argc, argv, &x)==0 ){ |
| 730 | char zBuf[100]; |
| 731 | computeYMD(&x); |
| 732 | sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 733 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) |
| 739 | ** |
| 740 | ** Return a string described by FORMAT. Conversions as follows: |
| 741 | ** |
| 742 | ** %d day of month |
| 743 | ** %f ** fractional seconds SS.SSS |
| 744 | ** %H hour 00-24 |
| 745 | ** %j day of year 000-366 |
| 746 | ** %J ** Julian day number |
| 747 | ** %m month 01-12 |
| 748 | ** %M minute 00-59 |
| 749 | ** %s seconds since 1970-01-01 |
| 750 | ** %S seconds 00-59 |
| 751 | ** %w day of week 0-6 sunday==0 |
| 752 | ** %W week of year 00-53 |
| 753 | ** %Y year 0000-9999 |
| 754 | ** %% % |
| 755 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 756 | static void strftimeFunc( |
| 757 | sqlite3_context *context, |
| 758 | int argc, |
| 759 | sqlite3_value **argv |
| 760 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 761 | DateTime x; |
| 762 | int n, i, j; |
| 763 | char *z; |
drh | 4f26d6c | 2004-05-26 23:25:30 +0000 | [diff] [blame] | 764 | const char *zFmt = sqlite3_value_text(argv[0]); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 765 | char zBuf[100]; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 766 | if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 767 | for(i=0, n=1; zFmt[i]; i++, n++){ |
| 768 | if( zFmt[i]=='%' ){ |
| 769 | switch( zFmt[i+1] ){ |
| 770 | case 'd': |
| 771 | case 'H': |
| 772 | case 'm': |
| 773 | case 'M': |
| 774 | case 'S': |
| 775 | case 'W': |
| 776 | n++; |
| 777 | /* fall thru */ |
| 778 | case 'w': |
| 779 | case '%': |
| 780 | break; |
| 781 | case 'f': |
| 782 | n += 8; |
| 783 | break; |
| 784 | case 'j': |
| 785 | n += 3; |
| 786 | break; |
| 787 | case 'Y': |
| 788 | n += 8; |
| 789 | break; |
| 790 | case 's': |
| 791 | case 'J': |
| 792 | n += 50; |
| 793 | break; |
| 794 | default: |
| 795 | return; /* ERROR. return a NULL */ |
| 796 | } |
| 797 | i++; |
| 798 | } |
| 799 | } |
| 800 | if( n<sizeof(zBuf) ){ |
| 801 | z = zBuf; |
| 802 | }else{ |
| 803 | z = sqliteMalloc( n ); |
| 804 | if( z==0 ) return; |
| 805 | } |
| 806 | computeJD(&x); |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 807 | computeYMD_HMS(&x); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 808 | for(i=j=0; zFmt[i]; i++){ |
| 809 | if( zFmt[i]!='%' ){ |
| 810 | z[j++] = zFmt[i]; |
| 811 | }else{ |
| 812 | i++; |
| 813 | switch( zFmt[i] ){ |
| 814 | case 'd': sprintf(&z[j],"%02d",x.D); j+=2; break; |
| 815 | case 'f': { |
| 816 | int s = x.s; |
| 817 | int ms = (x.s - s)*1000.0; |
| 818 | sprintf(&z[j],"%02d.%03d",s,ms); |
| 819 | j += strlen(&z[j]); |
| 820 | break; |
| 821 | } |
| 822 | case 'H': sprintf(&z[j],"%02d",x.h); j+=2; break; |
| 823 | case 'W': /* Fall thru */ |
| 824 | case 'j': { |
drh | 1020d49 | 2004-07-18 22:22:43 +0000 | [diff] [blame] | 825 | int n; /* Number of days since 1st day of year */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 826 | DateTime y = x; |
| 827 | y.validJD = 0; |
| 828 | y.M = 1; |
| 829 | y.D = 1; |
| 830 | computeJD(&y); |
drh | 1020d49 | 2004-07-18 22:22:43 +0000 | [diff] [blame] | 831 | n = x.rJD - y.rJD; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 832 | if( zFmt[i]=='W' ){ |
drh | 1020d49 | 2004-07-18 22:22:43 +0000 | [diff] [blame] | 833 | int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ |
| 834 | wd = ((int)(x.rJD+0.5)) % 7; |
| 835 | sprintf(&z[j],"%02d",(n+7-wd)/7); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 836 | j += 2; |
| 837 | }else{ |
drh | 1020d49 | 2004-07-18 22:22:43 +0000 | [diff] [blame] | 838 | sprintf(&z[j],"%03d",n+1); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 839 | j += 3; |
| 840 | } |
| 841 | break; |
| 842 | } |
| 843 | case 'J': sprintf(&z[j],"%.16g",x.rJD); j+=strlen(&z[j]); break; |
| 844 | case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break; |
| 845 | case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break; |
| 846 | case 's': { |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 847 | sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5)); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 848 | j += strlen(&z[j]); |
| 849 | break; |
| 850 | } |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 851 | case 'S': sprintf(&z[j],"%02d",(int)(x.s+0.5)); j+=2; break; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 852 | case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break; |
| 853 | case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break; |
| 854 | case '%': z[j++] = '%'; break; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | z[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 859 | sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 860 | if( z!=zBuf ){ |
| 861 | sqliteFree(z); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | |
| 866 | #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ |
| 867 | |
| 868 | /* |
| 869 | ** This function registered all of the above C functions as SQL |
| 870 | ** functions. This should be the only routine in this file with |
| 871 | ** external linkage. |
| 872 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 873 | void sqlite3RegisterDateTimeFunctions(sqlite *db){ |
drh | fd1f394 | 2004-07-20 00:39:14 +0000 | [diff] [blame] | 874 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 875 | static struct { |
| 876 | char *zName; |
| 877 | int nArg; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 878 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 879 | } aFuncs[] = { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 880 | { "julianday", -1, juliandayFunc }, |
| 881 | { "date", -1, dateFunc }, |
| 882 | { "time", -1, timeFunc }, |
| 883 | { "datetime", -1, datetimeFunc }, |
| 884 | { "strftime", -1, strftimeFunc }, |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 885 | }; |
| 886 | int i; |
| 887 | |
| 888 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 889 | sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 890 | SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 891 | } |
drh | fd1f394 | 2004-07-20 00:39:14 +0000 | [diff] [blame] | 892 | #endif |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 893 | } |