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