-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
datetime.c
330 lines (308 loc) · 7.64 KB
/
datetime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*!
\file lib/db/dbmi_base/datetime.c
\brief DBMI Library (base) - datetime conversions
(C) 1999-2009, 2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Joel Jones (CERL/UIUC), Radim Blazek
\author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
*/
#include <stdio.h>
#include <string.h>
#include <grass/dbmi.h>
static char ds = '-';
static char ts = ':';
/*!
\brief Convert datetime value into string
\param value pointer to dbValue
\param sqltype SQL data type
\param[out] string pointer to dbString
\return DB_OK on success
*/
int db_convert_value_datetime_into_string(dbValue *value, int sqltype,
dbString *string)
{
int to, from;
int year, month, day, hour, minute;
double seconds;
char *xs;
char buf[64];
if (db_test_value_datetime_current(value))
return db_set_string(string, "CURRENT");
*buf = 0;
year = db_get_value_year(value);
month = db_get_value_month(value);
day = db_get_value_day(value);
hour = db_get_value_hour(value);
minute = db_get_value_minute(value);
seconds = db_get_value_seconds(value);
if (seconds < 10.0)
xs = "0";
else
xs = "";
db_interval_range(sqltype, &from, &to);
switch (from) {
case DB_YEAR:
switch (to) {
case DB_YEAR:
sprintf(buf, "%d", year);
break;
case DB_MONTH:
sprintf(buf, "%d%c%02d", year, ds, month);
break;
case DB_DAY:
sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
break;
case DB_HOUR:
sprintf(buf, "%d%c%02d%c%02d %02d", year, ds, month, ds, day, hour);
break;
case DB_MINUTE:
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d",
year, ds, month, ds, day, hour, ts, minute);
break;
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
year, ds, month, ds, day, hour, ts, minute, ts, xs,
seconds);
break;
}
break;
case DB_MONTH:
switch (to) {
case DB_MONTH:
sprintf(buf, "%d", month);
break;
case DB_DAY:
sprintf(buf, "%02d%c%02d", month, ds, day);
break;
case DB_HOUR:
sprintf(buf, "%02d%c%02d %02d", month, ds, day, hour);
break;
case DB_MINUTE:
sprintf(buf, "%02d%c%02d %02d%c%02d", month, ds, day, hour, ts, minute);
break;
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%02d%c%02d %02d%c%02d%c%s%.10g",
month, ds, day, hour, ts, minute, ts, xs, seconds);
break;
}
break;
case DB_DAY:
switch (to) {
case DB_DAY:
sprintf(buf, "%02d", day);
break;
case DB_HOUR:
sprintf(buf, "%02d %02d", day, hour);
break;
case DB_MINUTE:
sprintf(buf, "%02d %02d%c%02d", day, hour, ts, minute);
break;
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%02d %02d%c%02d%c%s%.10g",
day, hour, ts, minute, ts, xs, seconds);
break;
}
break;
case DB_HOUR:
switch (to) {
case DB_HOUR:
sprintf(buf, "%02d", hour);
break;
case DB_MINUTE:
sprintf(buf, "%02d%c%02d", hour, ts, minute);
break;
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%02d%c%02d%c%s%.10g", hour, ts, minute, ts, xs,
seconds);
break;
}
break;
case DB_MINUTE:
switch (to) {
case DB_MINUTE:
sprintf(buf, "%02d", minute);
break;
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%02d%c%s%.10g", minute, ts, xs, seconds);
break;
}
break;
case DB_SECOND:
case DB_FRACTION:
switch (to) {
case DB_SECOND:
case DB_FRACTION:
sprintf(buf, "%g", seconds);
break;
}
break;
default:
switch (sqltype) {
case DB_SQL_TYPE_DATE:
sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
break;
case DB_SQL_TYPE_TIME:
sprintf(buf, "%02d%c%02d%c%s%.10g",
hour, ts, minute, ts, xs, seconds);
break;
case DB_SQL_TYPE_TIMESTAMP:
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
year, ds, month, ds, day, hour, ts, minute, ts, xs,
seconds);
break;
}
}
return db_set_string(string, buf);
}
/*!
\brief Convert datetime string to value
The format of <em>buf</em> must be as follows
- buf == "CURRENT" in a case-insignificant fashion value is marked as current
- sqltype == DB_SQL_TYPE_DATE
"year*month*day"
- sqltype == DB_SQL_TYPE_TIME
"hour*minute*second"
- sqltype == DB_SQL_TYPE_TIMESTAMP
"year*month*day hour*minute*second"
- otherwise the to and from markings in sqltype are used,
where "*" represents any non-whitespace character
\param buf input string buffer
\param sqltype SQL data type
\param[out] value pointer to dbValue to be set
\return DB_OK
*/
int db_convert_Cstring_to_value_datetime(const char *buf, int sqltype,
dbValue *value)
{
int from, to;
int year, month, day, hour, minute;
double seconds;
year = month = day = 0;
hour = minute = 0;
seconds = 0;
if (db_nocase_compare(buf, "CURRENT")) {
db_set_value_datetime_current(value);
return DB_OK;
}
db_interval_range(sqltype, &from, &to);
switch (from) {
case DB_YEAR:
switch (to) {
case DB_YEAR:
sscanf(buf, "%d", &year);
break;
case DB_MONTH:
sscanf(buf, "%d%*c%d", &year, &month);
break;
case DB_DAY:
sscanf(buf, "%d%*c%d%*c%d", &year, &month, &day);
break;
case DB_HOUR:
sscanf(buf, "%d%*c%d%*c%d %d", &year, &month, &day, &hour);
break;
case DB_MINUTE:
sscanf(buf, "%d%*c%d%*c%d %d%*c%d",
&year, &month, &day, &hour, &minute);
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%d%*c%d%*c%d %d%*c%d%*c%lf",
&year, &month, &day, &hour, &minute, &seconds);
break;
}
break;
case DB_MONTH:
switch (to) {
case DB_MONTH:
sscanf(buf, "%d", &month);
break;
case DB_DAY:
sscanf(buf, "%d%*c%d", &month, &day);
break;
case DB_HOUR:
sscanf(buf, "%d%*c%d %d", &month, &day, &hour);
break;
case DB_MINUTE:
sscanf(buf, "%d%*c%d %d%*c%d", &month, &day, &hour, &minute);
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%d%*c%d %d%*c%d%*c%lf",
&month, &day, &hour, &minute, &seconds);
break;
}
break;
case DB_DAY:
switch (to) {
case DB_DAY:
sscanf(buf, "%d", &day);
break;
case DB_HOUR:
sscanf(buf, "%d %d", &day, &hour);
break;
case DB_MINUTE:
sscanf(buf, "%d %d%*c%d", &day, &hour, &minute);
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%d %d%*c%d%*c%lf", &day, &hour, &minute, &seconds);
break;
}
break;
case DB_HOUR:
switch (to) {
case DB_HOUR:
sscanf(buf, "%d", &hour);
break;
case DB_MINUTE:
sscanf(buf, "%d%*c%d", &hour, &minute);
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%d%*c%d%*c%lf", &hour, &minute, &seconds);
break;
}
break;
case DB_MINUTE:
switch (to) {
case DB_MINUTE:
sscanf(buf, "%d", &minute);
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%d%*c%lf", &minute, &seconds);
break;
}
break;
case DB_SECOND:
case DB_FRACTION:
sscanf(buf, "%lf", &seconds);
break;
default:
switch (sqltype) {
case DB_SQL_TYPE_DATE:
sscanf(buf, "%d%*c%d%*c%d", &year, &month, &day);
break;
case DB_SQL_TYPE_TIME:
sscanf(buf, "%d%*c%d%*c%lf", &hour, &minute, &seconds);
break;
case DB_SQL_TYPE_TIMESTAMP:
sscanf(buf, "%d%*c%d%*c%d %d%*c%d%*c%lf",
&year, &month, &day, &hour, &minute, &seconds);
break;
}
}
db_set_value_year(value, year);
db_set_value_month(value, month);
db_set_value_day(value, day);
db_set_value_hour(value, hour);
db_set_value_minute(value, minute);
db_set_value_seconds(value, seconds);
return DB_OK;
}