annotate sqlite/date.c @ 1622:55011fbbd8cb trunk

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