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 | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame^] | 19 | ** $Id: date.c,v 1.55 2006/09/08 12:27:37 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 "sqliteInt.h" |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 51 | #include "os.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 | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 108 | goto end_getDigits; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 109 | } |
| 110 | val = val*10 + *zDate - '0'; |
| 111 | zDate++; |
| 112 | } |
| 113 | if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){ |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 114 | goto end_getDigits; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 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 ); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 120 | end_getDigits: |
drh | 15b9a15 | 2006-01-31 20:49:13 +0000 | [diff] [blame] | 121 | va_end(ap); |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 122 | return cnt; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | ** Read text from z[] and convert into a floating point number. Return |
| 127 | ** the number of digits converted. |
| 128 | */ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 129 | #define getValue sqlite3AtoF |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | ** Parse a timezone extension on the end of a date-time. |
| 133 | ** The extension is of the form: |
| 134 | ** |
| 135 | ** (+/-)HH:MM |
| 136 | ** |
| 137 | ** If the parse is successful, write the number of minutes |
| 138 | ** of change in *pnMin and return 0. If a parser error occurs, |
| 139 | ** return 0. |
| 140 | ** |
| 141 | ** A missing specifier is not considered an error. |
| 142 | */ |
| 143 | static int parseTimezone(const char *zDate, DateTime *p){ |
| 144 | int sgn = 0; |
| 145 | int nHr, nMn; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 146 | while( isspace(*(u8*)zDate) ){ zDate++; } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 147 | p->tz = 0; |
| 148 | if( *zDate=='-' ){ |
| 149 | sgn = -1; |
| 150 | }else if( *zDate=='+' ){ |
| 151 | sgn = +1; |
| 152 | }else{ |
| 153 | return *zDate!=0; |
| 154 | } |
| 155 | zDate++; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 156 | if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ |
| 157 | return 1; |
| 158 | } |
| 159 | zDate += 5; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 160 | p->tz = sgn*(nMn + nHr*60); |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 161 | while( isspace(*(u8*)zDate) ){ zDate++; } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 162 | return *zDate!=0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF. |
| 167 | ** The HH, MM, and SS must each be exactly 2 digits. The |
| 168 | ** fractional seconds FFFF can be one or more digits. |
| 169 | ** |
| 170 | ** Return 1 if there is a parsing error and 0 on success. |
| 171 | */ |
| 172 | static int parseHhMmSs(const char *zDate, DateTime *p){ |
| 173 | int h, m, s; |
| 174 | double ms = 0.0; |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 175 | if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){ |
| 176 | return 1; |
| 177 | } |
| 178 | zDate += 5; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 179 | if( *zDate==':' ){ |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 180 | zDate++; |
| 181 | if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){ |
| 182 | return 1; |
| 183 | } |
| 184 | zDate += 2; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 185 | if( *zDate=='.' && isdigit((u8)zDate[1]) ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 186 | double rScale = 1.0; |
| 187 | zDate++; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 188 | while( isdigit(*(u8*)zDate) ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 189 | ms = ms*10.0 + *zDate - '0'; |
| 190 | rScale *= 10.0; |
| 191 | zDate++; |
| 192 | } |
| 193 | ms /= rScale; |
| 194 | } |
| 195 | }else{ |
| 196 | s = 0; |
| 197 | } |
| 198 | p->validJD = 0; |
| 199 | p->validHMS = 1; |
| 200 | p->h = h; |
| 201 | p->m = m; |
| 202 | p->s = s + ms; |
| 203 | if( parseTimezone(zDate, p) ) return 1; |
| 204 | p->validTZ = p->tz!=0; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume |
| 210 | ** that the YYYY-MM-DD is according to the Gregorian calendar. |
| 211 | ** |
| 212 | ** Reference: Meeus page 61 |
| 213 | */ |
| 214 | static void computeJD(DateTime *p){ |
| 215 | int Y, M, D, A, B, X1, X2; |
| 216 | |
| 217 | if( p->validJD ) return; |
| 218 | if( p->validYMD ){ |
| 219 | Y = p->Y; |
| 220 | M = p->M; |
| 221 | D = p->D; |
| 222 | }else{ |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 223 | Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 224 | M = 1; |
| 225 | D = 1; |
| 226 | } |
| 227 | if( M<=2 ){ |
| 228 | Y--; |
| 229 | M += 12; |
| 230 | } |
| 231 | A = Y/100; |
| 232 | B = 2 - A + (A/4); |
| 233 | X1 = 365.25*(Y+4716); |
| 234 | X2 = 30.6001*(M+1); |
| 235 | p->rJD = X1 + X2 + D + B - 1524.5; |
| 236 | p->validJD = 1; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 237 | if( p->validHMS ){ |
| 238 | p->rJD += (p->h*3600.0 + p->m*60.0 + p->s)/86400.0; |
| 239 | if( p->validTZ ){ |
drh | 5739103 | 2006-01-09 00:18:02 +0000 | [diff] [blame] | 240 | p->rJD -= p->tz*60/86400.0; |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame^] | 241 | p->validYMD = 0; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 242 | p->validHMS = 0; |
| 243 | p->validTZ = 0; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ** Parse dates of the form |
| 250 | ** |
| 251 | ** YYYY-MM-DD HH:MM:SS.FFF |
| 252 | ** YYYY-MM-DD HH:MM:SS |
| 253 | ** YYYY-MM-DD HH:MM |
| 254 | ** YYYY-MM-DD |
| 255 | ** |
| 256 | ** Write the result into the DateTime structure and return 0 |
| 257 | ** on success and 1 if the input string is not a well-formed |
| 258 | ** date. |
| 259 | */ |
| 260 | static int parseYyyyMmDd(const char *zDate, DateTime *p){ |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 261 | int Y, M, D, neg; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 262 | |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 263 | if( zDate[0]=='-' ){ |
| 264 | zDate++; |
| 265 | neg = 1; |
| 266 | }else{ |
| 267 | neg = 0; |
| 268 | } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 269 | if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){ |
| 270 | return 1; |
| 271 | } |
| 272 | zDate += 10; |
drh | 4cb29b4 | 2005-03-21 00:43:44 +0000 | [diff] [blame] | 273 | while( isspace(*(u8*)zDate) || 'T'==*(u8*)zDate ){ zDate++; } |
drh | eb9a9e8 | 2004-02-22 17:49:32 +0000 | [diff] [blame] | 274 | if( parseHhMmSs(zDate, p)==0 ){ |
| 275 | /* We got the time */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 276 | }else if( *zDate==0 ){ |
| 277 | p->validHMS = 0; |
| 278 | }else{ |
| 279 | return 1; |
| 280 | } |
| 281 | p->validJD = 0; |
| 282 | p->validYMD = 1; |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 283 | p->Y = neg ? -Y : Y; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 284 | p->M = M; |
| 285 | p->D = D; |
| 286 | if( p->validTZ ){ |
| 287 | computeJD(p); |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | ** Attempt to parse the given string into a Julian Day Number. Return |
| 294 | ** the number of errors. |
| 295 | ** |
| 296 | ** The following are acceptable forms for the input string: |
| 297 | ** |
| 298 | ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM |
| 299 | ** DDDD.DD |
| 300 | ** now |
| 301 | ** |
| 302 | ** In the first form, the +/-HH:MM is always optional. The fractional |
| 303 | ** seconds extension (the ".FFF") is optional. The seconds portion |
| 304 | ** (":SS.FFF") is option. The year and date can be omitted as long |
| 305 | ** as there is a time string. The time string can be omitted as long |
| 306 | ** as there is a year and date. |
| 307 | */ |
| 308 | static int parseDateOrTime(const char *zDate, DateTime *p){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 309 | memset(p, 0, sizeof(*p)); |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 310 | if( parseYyyyMmDd(zDate,p)==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 311 | return 0; |
drh | 8eb2cce | 2004-02-21 03:28:18 +0000 | [diff] [blame] | 312 | }else if( parseHhMmSs(zDate, p)==0 ){ |
| 313 | return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 314 | }else if( sqlite3StrICmp(zDate,"now")==0){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 315 | double r; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 316 | sqlite3OsCurrentTime(&r); |
drh | 018d1a4 | 2005-01-15 01:52:31 +0000 | [diff] [blame] | 317 | p->rJD = r; |
| 318 | p->validJD = 1; |
| 319 | return 0; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 320 | }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 321 | getValue(zDate, &p->rJD); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 322 | p->validJD = 1; |
| 323 | return 0; |
| 324 | } |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | ** Compute the Year, Month, and Day from the julian day number. |
| 330 | */ |
| 331 | static void computeYMD(DateTime *p){ |
| 332 | int Z, A, B, C, D, E, X1; |
| 333 | if( p->validYMD ) return; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 334 | if( !p->validJD ){ |
| 335 | p->Y = 2000; |
| 336 | p->M = 1; |
| 337 | p->D = 1; |
| 338 | }else{ |
| 339 | Z = p->rJD + 0.5; |
| 340 | A = (Z - 1867216.25)/36524.25; |
| 341 | A = Z + 1 + A - (A/4); |
| 342 | B = A + 1524; |
| 343 | C = (B - 122.1)/365.25; |
| 344 | D = 365.25*C; |
| 345 | E = (B-D)/30.6001; |
| 346 | X1 = 30.6001*E; |
| 347 | p->D = B - D - X1; |
| 348 | p->M = E<14 ? E-1 : E-13; |
| 349 | p->Y = p->M>2 ? C - 4716 : C - 4715; |
| 350 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 351 | p->validYMD = 1; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Compute the Hour, Minute, and Seconds from the julian day number. |
| 356 | */ |
| 357 | static void computeHMS(DateTime *p){ |
| 358 | int Z, s; |
| 359 | if( p->validHMS ) return; |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame^] | 360 | computeJD(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 361 | Z = p->rJD + 0.5; |
| 362 | s = (p->rJD + 0.5 - Z)*86400000.0 + 0.5; |
| 363 | p->s = 0.001*s; |
| 364 | s = p->s; |
| 365 | p->s -= s; |
| 366 | p->h = s/3600; |
| 367 | s -= p->h*3600; |
| 368 | p->m = s/60; |
| 369 | p->s += s - p->m*60; |
| 370 | p->validHMS = 1; |
| 371 | } |
| 372 | |
| 373 | /* |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 374 | ** Compute both YMD and HMS |
| 375 | */ |
| 376 | static void computeYMD_HMS(DateTime *p){ |
| 377 | computeYMD(p); |
| 378 | computeHMS(p); |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | ** Clear the YMD and HMS and the TZ |
| 383 | */ |
| 384 | static void clearYMD_HMS_TZ(DateTime *p){ |
| 385 | p->validYMD = 0; |
| 386 | p->validHMS = 0; |
| 387 | p->validTZ = 0; |
| 388 | } |
| 389 | |
| 390 | /* |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 391 | ** Compute the difference (in days) between localtime and UTC (a.k.a. GMT) |
| 392 | ** for the time value p where p is in UTC. |
| 393 | */ |
| 394 | static double localtimeOffset(DateTime *p){ |
| 395 | DateTime x, y; |
| 396 | time_t t; |
| 397 | struct tm *pTm; |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 398 | x = *p; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 399 | computeYMD_HMS(&x); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 400 | if( x.Y<1971 || x.Y>=2038 ){ |
| 401 | x.Y = 2000; |
| 402 | x.M = 1; |
| 403 | x.D = 1; |
| 404 | x.h = 0; |
| 405 | x.m = 0; |
| 406 | x.s = 0.0; |
| 407 | } else { |
| 408 | int s = x.s + 0.5; |
| 409 | x.s = s; |
| 410 | } |
| 411 | x.tz = 0; |
| 412 | x.validJD = 0; |
| 413 | computeJD(&x); |
| 414 | t = (x.rJD-2440587.5)*86400.0 + 0.5; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 415 | sqlite3OsEnterMutex(); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 416 | pTm = localtime(&t); |
| 417 | y.Y = pTm->tm_year + 1900; |
| 418 | y.M = pTm->tm_mon + 1; |
| 419 | y.D = pTm->tm_mday; |
| 420 | y.h = pTm->tm_hour; |
| 421 | y.m = pTm->tm_min; |
| 422 | y.s = pTm->tm_sec; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 423 | sqlite3OsLeaveMutex(); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 424 | y.validYMD = 1; |
| 425 | y.validHMS = 1; |
| 426 | y.validJD = 0; |
| 427 | y.validTZ = 0; |
| 428 | computeJD(&y); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 429 | return y.rJD - x.rJD; |
| 430 | } |
| 431 | |
| 432 | /* |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 433 | ** Process a modifier to a date-time stamp. The modifiers are |
| 434 | ** as follows: |
| 435 | ** |
| 436 | ** NNN days |
| 437 | ** NNN hours |
| 438 | ** NNN minutes |
| 439 | ** NNN.NNNN seconds |
| 440 | ** NNN months |
| 441 | ** NNN years |
| 442 | ** start of month |
| 443 | ** start of year |
| 444 | ** start of week |
| 445 | ** start of day |
| 446 | ** weekday N |
| 447 | ** unixepoch |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 448 | ** localtime |
| 449 | ** utc |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 450 | ** |
| 451 | ** Return 0 on success and 1 if there is any kind of error. |
| 452 | */ |
| 453 | static int parseModifier(const char *zMod, DateTime *p){ |
| 454 | int rc = 1; |
| 455 | int n; |
| 456 | double r; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 457 | char *z, zBuf[30]; |
| 458 | z = zBuf; |
| 459 | for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 460 | z[n] = tolower(zMod[n]); |
| 461 | } |
| 462 | z[n] = 0; |
| 463 | switch( z[0] ){ |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 464 | case 'l': { |
| 465 | /* localtime |
| 466 | ** |
| 467 | ** Assuming the current time value is UTC (a.k.a. GMT), shift it to |
| 468 | ** show local time. |
| 469 | */ |
| 470 | if( strcmp(z, "localtime")==0 ){ |
| 471 | computeJD(p); |
| 472 | p->rJD += localtimeOffset(p); |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 473 | clearYMD_HMS_TZ(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 474 | rc = 0; |
| 475 | } |
| 476 | break; |
| 477 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 478 | case 'u': { |
| 479 | /* |
| 480 | ** unixepoch |
| 481 | ** |
| 482 | ** Treat the current value of p->rJD as the number of |
| 483 | ** seconds since 1970. Convert to a real julian day number. |
| 484 | */ |
| 485 | if( strcmp(z, "unixepoch")==0 && p->validJD ){ |
| 486 | p->rJD = p->rJD/86400.0 + 2440587.5; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 487 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 488 | rc = 0; |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 489 | }else if( strcmp(z, "utc")==0 ){ |
| 490 | double c1; |
| 491 | computeJD(p); |
| 492 | c1 = localtimeOffset(p); |
| 493 | p->rJD -= c1; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 494 | clearYMD_HMS_TZ(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 495 | p->rJD += c1 - localtimeOffset(p); |
drh | 7091cb0 | 2003-12-23 16:22:18 +0000 | [diff] [blame] | 496 | rc = 0; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 497 | } |
| 498 | break; |
| 499 | } |
| 500 | case 'w': { |
| 501 | /* |
| 502 | ** weekday N |
| 503 | ** |
drh | 181fc99 | 2004-08-17 10:42:54 +0000 | [diff] [blame] | 504 | ** Move the date to the same time on the next occurrence of |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 505 | ** weekday N where 0==Sunday, 1==Monday, and so forth. If the |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 506 | ** date is already on the appropriate weekday, this is a no-op. |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 507 | */ |
| 508 | if( strncmp(z, "weekday ", 8)==0 && getValue(&z[8],&r)>0 |
| 509 | && (n=r)==r && n>=0 && r<7 ){ |
| 510 | int Z; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 511 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 512 | p->validTZ = 0; |
| 513 | p->validJD = 0; |
| 514 | computeJD(p); |
| 515 | Z = p->rJD + 1.5; |
| 516 | Z %= 7; |
| 517 | if( Z>n ) Z -= 7; |
| 518 | p->rJD += n - Z; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 519 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 520 | rc = 0; |
| 521 | } |
| 522 | break; |
| 523 | } |
| 524 | case 's': { |
| 525 | /* |
| 526 | ** start of TTTTT |
| 527 | ** |
| 528 | ** Move the date backwards to the beginning of the current day, |
| 529 | ** or month or year. |
| 530 | */ |
| 531 | if( strncmp(z, "start of ", 9)!=0 ) break; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 532 | z += 9; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 533 | computeYMD(p); |
| 534 | p->validHMS = 1; |
| 535 | p->h = p->m = 0; |
| 536 | p->s = 0.0; |
| 537 | p->validTZ = 0; |
| 538 | p->validJD = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 539 | if( strcmp(z,"month")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 540 | p->D = 1; |
| 541 | rc = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 542 | }else if( strcmp(z,"year")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 543 | computeYMD(p); |
| 544 | p->M = 1; |
| 545 | p->D = 1; |
| 546 | rc = 0; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 547 | }else if( strcmp(z,"day")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 548 | rc = 0; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | case '+': |
| 553 | case '-': |
| 554 | case '0': |
| 555 | case '1': |
| 556 | case '2': |
| 557 | case '3': |
| 558 | case '4': |
| 559 | case '5': |
| 560 | case '6': |
| 561 | case '7': |
| 562 | case '8': |
| 563 | case '9': { |
| 564 | n = getValue(z, &r); |
| 565 | if( n<=0 ) break; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 566 | if( z[n]==':' ){ |
| 567 | /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the |
| 568 | ** specified number of hours, minutes, seconds, and fractional seconds |
| 569 | ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be |
| 570 | ** omitted. |
| 571 | */ |
| 572 | const char *z2 = z; |
| 573 | DateTime tx; |
| 574 | int day; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 575 | if( !isdigit(*(u8*)z2) ) z2++; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 576 | memset(&tx, 0, sizeof(tx)); |
| 577 | if( parseHhMmSs(z2, &tx) ) break; |
| 578 | computeJD(&tx); |
drh | 0d131ab | 2004-02-29 01:08:17 +0000 | [diff] [blame] | 579 | tx.rJD -= 0.5; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 580 | day = (int)tx.rJD; |
drh | b6829e9 | 2004-02-29 00:50:33 +0000 | [diff] [blame] | 581 | tx.rJD -= day; |
drh | b6829e9 | 2004-02-29 00:50:33 +0000 | [diff] [blame] | 582 | if( z[0]=='-' ) tx.rJD = -tx.rJD; |
drh | 0d131ab | 2004-02-29 01:08:17 +0000 | [diff] [blame] | 583 | computeJD(p); |
| 584 | clearYMD_HMS_TZ(p); |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame^] | 585 | p->rJD += tx.rJD; |
drh | 33a9ad2 | 2004-02-29 00:40:32 +0000 | [diff] [blame] | 586 | rc = 0; |
| 587 | break; |
| 588 | } |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 589 | z += n; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 590 | while( isspace(*(u8*)z) ) z++; |
drh | 4d5b836 | 2004-01-17 01:16:21 +0000 | [diff] [blame] | 591 | n = strlen(z); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 592 | if( n>10 || n<3 ) break; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 593 | if( z[n-1]=='s' ){ z[n-1] = 0; n--; } |
| 594 | computeJD(p); |
| 595 | rc = 0; |
| 596 | if( n==3 && strcmp(z,"day")==0 ){ |
| 597 | p->rJD += r; |
| 598 | }else if( n==4 && strcmp(z,"hour")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 599 | p->rJD += r/24.0; |
| 600 | }else if( n==6 && strcmp(z,"minute")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 601 | p->rJD += r/(24.0*60.0); |
| 602 | }else if( n==6 && strcmp(z,"second")==0 ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 603 | p->rJD += r/(24.0*60.0*60.0); |
| 604 | }else if( n==5 && strcmp(z,"month")==0 ){ |
| 605 | int x, y; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 606 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 607 | p->M += r; |
| 608 | x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; |
| 609 | p->Y += x; |
| 610 | p->M -= x*12; |
| 611 | p->validJD = 0; |
| 612 | computeJD(p); |
| 613 | y = r; |
| 614 | if( y!=r ){ |
| 615 | p->rJD += (r - y)*30.0; |
| 616 | } |
| 617 | }else if( n==4 && strcmp(z,"year")==0 ){ |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 618 | computeYMD_HMS(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 619 | p->Y += r; |
| 620 | p->validJD = 0; |
| 621 | computeJD(p); |
| 622 | }else{ |
| 623 | rc = 1; |
| 624 | } |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 625 | clearYMD_HMS_TZ(p); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | default: { |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | return rc; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | ** Process time function arguments. argv[0] is a date-time stamp. |
| 637 | ** argv[1] and following are modifiers. Parse them all and write |
| 638 | ** the resulting time into the DateTime structure p. Return 0 |
| 639 | ** on success and 1 if there are any errors. |
| 640 | */ |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 641 | static int isDate(int argc, sqlite3_value **argv, DateTime *p){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 642 | int i; |
| 643 | if( argc==0 ) return 1; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 644 | if( SQLITE_NULL==sqlite3_value_type(argv[0]) || |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 645 | parseDateOrTime((char*)sqlite3_value_text(argv[0]), p) ) return 1; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 646 | for(i=1; i<argc; i++){ |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 647 | if( SQLITE_NULL==sqlite3_value_type(argv[i]) || |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 648 | parseModifier((char*)sqlite3_value_text(argv[i]), p) ) return 1; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /* |
| 655 | ** The following routines implement the various date and time functions |
| 656 | ** of SQLite. |
| 657 | */ |
| 658 | |
| 659 | /* |
| 660 | ** julianday( TIMESTRING, MOD, MOD, ...) |
| 661 | ** |
| 662 | ** Return the julian day number of the date specified in the arguments |
| 663 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 664 | static void juliandayFunc( |
| 665 | sqlite3_context *context, |
| 666 | int argc, |
| 667 | sqlite3_value **argv |
| 668 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 669 | DateTime x; |
| 670 | if( isDate(argc, argv, &x)==0 ){ |
| 671 | computeJD(&x); |
danielk1977 | 7e18c25 | 2004-05-25 11:47:24 +0000 | [diff] [blame] | 672 | sqlite3_result_double(context, x.rJD); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | ** datetime( TIMESTRING, MOD, MOD, ...) |
| 678 | ** |
| 679 | ** Return YYYY-MM-DD HH:MM:SS |
| 680 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 681 | static void datetimeFunc( |
| 682 | sqlite3_context *context, |
| 683 | int argc, |
| 684 | sqlite3_value **argv |
| 685 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 686 | DateTime x; |
| 687 | if( isDate(argc, argv, &x)==0 ){ |
| 688 | char zBuf[100]; |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 689 | computeYMD_HMS(&x); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 690 | sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m, |
| 691 | (int)(x.s)); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 692 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | ** time( TIMESTRING, MOD, MOD, ...) |
| 698 | ** |
| 699 | ** Return HH:MM:SS |
| 700 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 701 | static void timeFunc( |
| 702 | sqlite3_context *context, |
| 703 | int argc, |
| 704 | sqlite3_value **argv |
| 705 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 706 | DateTime x; |
| 707 | if( isDate(argc, argv, &x)==0 ){ |
| 708 | char zBuf[100]; |
| 709 | computeHMS(&x); |
| 710 | sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 711 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | ** date( TIMESTRING, MOD, MOD, ...) |
| 717 | ** |
| 718 | ** Return YYYY-MM-DD |
| 719 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 720 | static void dateFunc( |
| 721 | sqlite3_context *context, |
| 722 | int argc, |
| 723 | sqlite3_value **argv |
| 724 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 725 | DateTime x; |
| 726 | if( isDate(argc, argv, &x)==0 ){ |
| 727 | char zBuf[100]; |
| 728 | computeYMD(&x); |
| 729 | sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 730 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) |
| 736 | ** |
| 737 | ** Return a string described by FORMAT. Conversions as follows: |
| 738 | ** |
| 739 | ** %d day of month |
| 740 | ** %f ** fractional seconds SS.SSS |
| 741 | ** %H hour 00-24 |
| 742 | ** %j day of year 000-366 |
| 743 | ** %J ** Julian day number |
| 744 | ** %m month 01-12 |
| 745 | ** %M minute 00-59 |
| 746 | ** %s seconds since 1970-01-01 |
| 747 | ** %S seconds 00-59 |
| 748 | ** %w day of week 0-6 sunday==0 |
| 749 | ** %W week of year 00-53 |
| 750 | ** %Y year 0000-9999 |
| 751 | ** %% % |
| 752 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 753 | static void strftimeFunc( |
| 754 | sqlite3_context *context, |
| 755 | int argc, |
| 756 | sqlite3_value **argv |
| 757 | ){ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 758 | DateTime x; |
| 759 | int n, i, j; |
| 760 | char *z; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 761 | const char *zFmt = (const char*)sqlite3_value_text(argv[0]); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 762 | char zBuf[100]; |
danielk1977 | 51ad0ec | 2004-05-24 12:39:02 +0000 | [diff] [blame] | 763 | if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 764 | for(i=0, n=1; zFmt[i]; i++, n++){ |
| 765 | if( zFmt[i]=='%' ){ |
| 766 | switch( zFmt[i+1] ){ |
| 767 | case 'd': |
| 768 | case 'H': |
| 769 | case 'm': |
| 770 | case 'M': |
| 771 | case 'S': |
| 772 | case 'W': |
| 773 | n++; |
| 774 | /* fall thru */ |
| 775 | case 'w': |
| 776 | case '%': |
| 777 | break; |
| 778 | case 'f': |
| 779 | n += 8; |
| 780 | break; |
| 781 | case 'j': |
| 782 | n += 3; |
| 783 | break; |
| 784 | case 'Y': |
| 785 | n += 8; |
| 786 | break; |
| 787 | case 's': |
| 788 | case 'J': |
| 789 | n += 50; |
| 790 | break; |
| 791 | default: |
| 792 | return; /* ERROR. return a NULL */ |
| 793 | } |
| 794 | i++; |
| 795 | } |
| 796 | } |
| 797 | if( n<sizeof(zBuf) ){ |
| 798 | z = zBuf; |
| 799 | }else{ |
| 800 | z = sqliteMalloc( n ); |
| 801 | if( z==0 ) return; |
| 802 | } |
| 803 | computeJD(&x); |
drh | ba21256 | 2004-01-08 02:17:31 +0000 | [diff] [blame] | 804 | computeYMD_HMS(&x); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 805 | for(i=j=0; zFmt[i]; i++){ |
| 806 | if( zFmt[i]!='%' ){ |
| 807 | z[j++] = zFmt[i]; |
| 808 | }else{ |
| 809 | i++; |
| 810 | switch( zFmt[i] ){ |
| 811 | case 'd': sprintf(&z[j],"%02d",x.D); j+=2; break; |
| 812 | case 'f': { |
| 813 | int s = x.s; |
| 814 | int ms = (x.s - s)*1000.0; |
| 815 | sprintf(&z[j],"%02d.%03d",s,ms); |
| 816 | j += strlen(&z[j]); |
| 817 | break; |
| 818 | } |
| 819 | case 'H': sprintf(&z[j],"%02d",x.h); j+=2; break; |
| 820 | case 'W': /* Fall thru */ |
| 821 | case 'j': { |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 822 | int nDay; /* Number of days since 1st day of year */ |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 823 | DateTime y = x; |
| 824 | y.validJD = 0; |
| 825 | y.M = 1; |
| 826 | y.D = 1; |
| 827 | computeJD(&y); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 828 | nDay = x.rJD - y.rJD; |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 829 | if( zFmt[i]=='W' ){ |
drh | 1020d49 | 2004-07-18 22:22:43 +0000 | [diff] [blame] | 830 | int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ |
| 831 | wd = ((int)(x.rJD+0.5)) % 7; |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 832 | sprintf(&z[j],"%02d",(nDay+7-wd)/7); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 833 | j += 2; |
| 834 | }else{ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 835 | sprintf(&z[j],"%03d",nDay+1); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 836 | j += 3; |
| 837 | } |
| 838 | break; |
| 839 | } |
| 840 | case 'J': sprintf(&z[j],"%.16g",x.rJD); j+=strlen(&z[j]); break; |
| 841 | case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break; |
| 842 | case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break; |
| 843 | case 's': { |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 844 | sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5)); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 845 | j += strlen(&z[j]); |
| 846 | break; |
| 847 | } |
drh | c5dd9fa | 2004-01-07 03:29:16 +0000 | [diff] [blame] | 848 | 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] | 849 | case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break; |
| 850 | case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break; |
| 851 | case '%': z[j++] = '%'; break; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | z[j] = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 856 | sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 857 | if( z!=zBuf ){ |
| 858 | sqliteFree(z); |
| 859 | } |
| 860 | } |
| 861 | |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 862 | /* |
| 863 | ** current_time() |
| 864 | ** |
| 865 | ** This function returns the same value as time('now'). |
| 866 | */ |
| 867 | static void ctimeFunc( |
| 868 | sqlite3_context *context, |
| 869 | int argc, |
| 870 | sqlite3_value **argv |
| 871 | ){ |
| 872 | sqlite3_value *pVal = sqlite3ValueNew(); |
| 873 | if( pVal ){ |
| 874 | sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); |
| 875 | timeFunc(context, 1, &pVal); |
| 876 | sqlite3ValueFree(pVal); |
| 877 | } |
| 878 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 879 | |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 880 | /* |
| 881 | ** current_date() |
| 882 | ** |
| 883 | ** This function returns the same value as date('now'). |
| 884 | */ |
| 885 | static void cdateFunc( |
| 886 | sqlite3_context *context, |
| 887 | int argc, |
| 888 | sqlite3_value **argv |
| 889 | ){ |
| 890 | sqlite3_value *pVal = sqlite3ValueNew(); |
| 891 | if( pVal ){ |
| 892 | sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); |
| 893 | dateFunc(context, 1, &pVal); |
| 894 | sqlite3ValueFree(pVal); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | ** current_timestamp() |
| 900 | ** |
| 901 | ** This function returns the same value as datetime('now'). |
| 902 | */ |
| 903 | static void ctimestampFunc( |
| 904 | sqlite3_context *context, |
| 905 | int argc, |
| 906 | sqlite3_value **argv |
| 907 | ){ |
| 908 | sqlite3_value *pVal = sqlite3ValueNew(); |
| 909 | if( pVal ){ |
| 910 | sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); |
| 911 | datetimeFunc(context, 1, &pVal); |
| 912 | sqlite3ValueFree(pVal); |
| 913 | } |
| 914 | } |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 915 | #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ |
| 916 | |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 917 | #ifdef SQLITE_OMIT_DATETIME_FUNCS |
| 918 | /* |
| 919 | ** If the library is compiled to omit the full-scale date and time |
| 920 | ** handling (to get a smaller binary), the following minimal version |
| 921 | ** of the functions current_time(), current_date() and current_timestamp() |
| 922 | ** are included instead. This is to support column declarations that |
| 923 | ** include "DEFAULT CURRENT_TIME" etc. |
| 924 | ** |
danielk1977 | 2df9fab | 2004-11-11 01:50:30 +0000 | [diff] [blame] | 925 | ** This function uses the C-library functions time(), gmtime() |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 926 | ** and strftime(). The format string to pass to strftime() is supplied |
| 927 | ** as the user-data for the function. |
| 928 | */ |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 929 | static void currentTimeFunc( |
| 930 | sqlite3_context *context, |
| 931 | int argc, |
| 932 | sqlite3_value **argv |
| 933 | ){ |
| 934 | time_t t; |
| 935 | char *zFormat = (char *)sqlite3_user_data(context); |
| 936 | char zBuf[20]; |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 937 | |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 938 | time(&t); |
| 939 | #ifdef SQLITE_TEST |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 940 | { |
| 941 | extern int sqlite3_current_time; /* See os_XXX.c */ |
| 942 | if( sqlite3_current_time ){ |
| 943 | t = sqlite3_current_time; |
| 944 | } |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 945 | } |
| 946 | #endif |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 947 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 948 | sqlite3OsEnterMutex(); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 949 | strftime(zBuf, 20, zFormat, gmtime(&t)); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 950 | sqlite3OsLeaveMutex(); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 951 | |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 952 | sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 953 | } |
| 954 | #endif |
| 955 | |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 956 | /* |
| 957 | ** This function registered all of the above C functions as SQL |
| 958 | ** functions. This should be the only routine in this file with |
| 959 | ** external linkage. |
| 960 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 961 | void sqlite3RegisterDateTimeFunctions(sqlite3 *db){ |
drh | fd1f394 | 2004-07-20 00:39:14 +0000 | [diff] [blame] | 962 | #ifndef SQLITE_OMIT_DATETIME_FUNCS |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 963 | static const struct { |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 964 | char *zName; |
| 965 | int nArg; |
danielk1977 | 0ae8b83 | 2004-05-25 12:05:56 +0000 | [diff] [blame] | 966 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 967 | } aFuncs[] = { |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 968 | { "julianday", -1, juliandayFunc }, |
| 969 | { "date", -1, dateFunc }, |
| 970 | { "time", -1, timeFunc }, |
| 971 | { "datetime", -1, datetimeFunc }, |
| 972 | { "strftime", -1, strftimeFunc }, |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 973 | { "current_time", 0, ctimeFunc }, |
| 974 | { "current_timestamp", 0, ctimestampFunc }, |
| 975 | { "current_date", 0, cdateFunc }, |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 976 | }; |
| 977 | int i; |
| 978 | |
| 979 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 980 | sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 981 | SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0); |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 982 | } |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 983 | #else |
| 984 | static const struct { |
| 985 | char *zName; |
| 986 | char *zFormat; |
| 987 | } aFuncs[] = { |
| 988 | { "current_time", "%H:%M:%S" }, |
| 989 | { "current_date", "%Y-%m-%d" }, |
| 990 | { "current_timestamp", "%Y-%m-%d %H:%M:%S" } |
| 991 | }; |
| 992 | int i; |
| 993 | |
| 994 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 995 | sqlite3CreateFunc(db, aFuncs[i].zName, 0, SQLITE_UTF8, |
danielk1977 | 752e679 | 2004-11-09 16:13:33 +0000 | [diff] [blame] | 996 | aFuncs[i].zFormat, currentTimeFunc, 0, 0); |
| 997 | } |
drh | fd1f394 | 2004-07-20 00:39:14 +0000 | [diff] [blame] | 998 | #endif |
drh | 7014aff | 2003-11-01 01:53:53 +0000 | [diff] [blame] | 999 | } |