GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
datetime.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <grass/dbmi.h>
4 
5 static char ds = '-';
6 static char ts = ':';
7 
14 int db_convert_value_datetime_into_string(dbValue * value, int sqltype,
15  dbString * string)
16 {
17  int to, from;
18  int year, month, day, hour, minute;
19  double seconds;
20  char *xs;
21  char buf[64];
22 
24  return db_set_string(string, "CURRENT");
25 
26  *buf = 0;
27 
28  year = db_get_value_year(value);
29  month = db_get_value_month(value);
30  day = db_get_value_day(value);
31  hour = db_get_value_hour(value);
32  minute = db_get_value_minute(value);
33  seconds = db_get_value_seconds(value);
34  if (seconds < 10.0)
35  xs = "0";
36  else
37  xs = "";
38 
39  db_interval_range(sqltype, &from, &to);
40  switch (from) {
41  case DB_YEAR:
42  switch (to) {
43  case DB_YEAR:
44  sprintf(buf, "%d", year);
45  break;
46  case DB_MONTH:
47  sprintf(buf, "%d%c%02d", year, ds, month);
48  break;
49  case DB_DAY:
50  sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
51  break;
52  case DB_HOUR:
53  sprintf(buf, "%d%c%02d%c%02d %02d", year, ds, month, ds, day, hour);
54  break;
55  case DB_MINUTE:
56  sprintf(buf, "%d%c%02d%c%02d %02d%c%02d",
57  year, ds, month, ds, day, hour, ts, minute);
58  break;
59  case DB_SECOND:
60  case DB_FRACTION:
61  sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
62  year, ds, month, ds, day, hour, ts, minute, ts, xs,
63  seconds);
64  break;
65  }
66  break;
67  case DB_MONTH:
68  switch (to) {
69  case DB_MONTH:
70  sprintf(buf, "%d", month);
71  break;
72  case DB_DAY:
73  sprintf(buf, "%02d%c%02d", month, ds, day);
74  break;
75  case DB_HOUR:
76  sprintf(buf, "%02d%c%02d %02d", month, ds, day, hour);
77  break;
78  case DB_MINUTE:
79  sprintf(buf, "%02d%c%02d %02d%c%02d", month, ds, day, hour, ts, minute);
80  break;
81  case DB_SECOND:
82  case DB_FRACTION:
83  sprintf(buf, "%02d%c%02d %02d%c%02d%c%s%.10g",
84  month, ds, day, hour, ts, minute, ts, xs, seconds);
85  break;
86  }
87  break;
88  case DB_DAY:
89  switch (to) {
90  case DB_DAY:
91  sprintf(buf, "%02d", day);
92  break;
93  case DB_HOUR:
94  sprintf(buf, "%02d %02d", day, hour);
95  break;
96  case DB_MINUTE:
97  sprintf(buf, "%02d %02d%c%02d", day, hour, ts, minute);
98  break;
99  case DB_SECOND:
100  case DB_FRACTION:
101  sprintf(buf, "%02d %02d%c%02d%c%s%.10g",
102  day, hour, ts, minute, ts, xs, seconds);
103  break;
104  }
105  break;
106  case DB_HOUR:
107  switch (to) {
108  case DB_HOUR:
109  sprintf(buf, "%02d", hour);
110  break;
111  case DB_MINUTE:
112  sprintf(buf, "%02d%c%02d", hour, ts, minute);
113  break;
114  case DB_SECOND:
115  case DB_FRACTION:
116  sprintf(buf, "%02d%c%02d%c%s%.10g", hour, ts, minute, ts, xs,
117  seconds);
118  break;
119  }
120  break;
121  case DB_MINUTE:
122  switch (to) {
123  case DB_MINUTE:
124  sprintf(buf, "%02d", minute);
125  break;
126  case DB_SECOND:
127  case DB_FRACTION:
128  sprintf(buf, "%02d%c%s%.10g", minute, ts, xs, seconds);
129  break;
130  }
131  break;
132  case DB_SECOND:
133  case DB_FRACTION:
134  switch (to) {
135  case DB_SECOND:
136  case DB_FRACTION:
137  sprintf(buf, "%g", seconds);
138  break;
139  }
140  break;
141  default:
142  switch (sqltype) {
143  case DB_SQL_TYPE_DATE:
144  sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
145  break;
146  case DB_SQL_TYPE_TIME:
147  sprintf(buf, "%02d%c%02d%c%s%.10g",
148  hour, ts, minute, ts, xs, seconds);
149  break;
150  case DB_SQL_TYPE_TIMESTAMP:
151  sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
152  year, ds, month, ds, day, hour, ts, minute, ts, xs,
153  seconds);
154  break;
155  }
156  }
157  return db_set_string(string, buf);
158 }
159 
166 /* NAME: db_convert_Cstring_to_value_datetime
167  * INPUT: buf, a C string formated as indicated by sqltype, value, a dbValue
168  * to put the converted value into
169  * OUTPUT: the converted datetime value in value
170  * PROCESSING: the format of buf must be as follows
171  * buf == "CURRENT" in a case-insignificant fashion
172  * value is marked as current
173  * sqltype == DB_SQL_TYPE_DATE
174  * "year*month*day"
175  * sqltype == DB_SQL_TYPE_TIME
176  * "hour*minute*second"
177  * sqltype == DB_SQL_TYPE_TIMESTAMP
178  * "year*month*day hour*minute*second"
179  * otherwise the to and from markings in sqltype are used.
180  * where "*" represents any non-whitespace character
181  */
182 int db_convert_Cstring_to_value_datetime(const char *buf, int sqltype,
183  dbValue * value)
184 {
185  int from, to;
186  int year, month, day, hour, minute;
187  double seconds;
188 
189  year = month = day = 0;
190  hour = minute = 0;
191  seconds = 0;
192 
193  if (db_nocase_compare(buf, "CURRENT")) {
195  return DB_OK;
196  }
197 
198  db_interval_range(sqltype, &from, &to);
199  switch (from) {
200  case DB_YEAR:
201  switch (to) {
202  case DB_YEAR:
203  sscanf(buf, "%d", &year);
204  break;
205  case DB_MONTH:
206  sscanf(buf, "%d%*c%d", &year, &month);
207  break;
208  case DB_DAY:
209  sscanf(buf, "%d%*c%d%*c%d", &year, &month, &day);
210  break;
211  case DB_HOUR:
212  sscanf(buf, "%d%*c%d%*c%d %d", &year, &month, &day, &hour);
213  break;
214  case DB_MINUTE:
215  sscanf(buf, "%d%*c%d%*c%d %d%*c%d",
216  &year, &month, &day, &hour, &minute);
217  break;
218  case DB_SECOND:
219  case DB_FRACTION:
220  sscanf(buf, "%d%*c%d%*c%d %d%*c%d%*c%lf",
221  &year, &month, &day, &hour, &minute, &seconds);
222  break;
223  }
224  break;
225  case DB_MONTH:
226  switch (to) {
227  case DB_MONTH:
228  sscanf(buf, "%d", &month);
229  break;
230  case DB_DAY:
231  sscanf(buf, "%d%*c%d", &month, &day);
232  break;
233  case DB_HOUR:
234  sscanf(buf, "%d%*c%d %d", &month, &day, &hour);
235  break;
236  case DB_MINUTE:
237  sscanf(buf, "%d%*c%d %d%*c%d", &month, &day, &hour, &minute);
238  break;
239  case DB_SECOND:
240  case DB_FRACTION:
241  sscanf(buf, "%d%*c%d %d%*c%d%*c%lf",
242  &month, &day, &hour, &minute, &seconds);
243  break;
244  }
245  break;
246  case DB_DAY:
247  switch (to) {
248  case DB_DAY:
249  sscanf(buf, "%d", &day);
250  break;
251  case DB_HOUR:
252  sscanf(buf, "%d %d", &day, &hour);
253  break;
254  case DB_MINUTE:
255  sscanf(buf, "%d %d%*c%d", &day, &hour, &minute);
256  break;
257  case DB_SECOND:
258  case DB_FRACTION:
259  sscanf(buf, "%d %d%*c%d%*c%lf", &day, &hour, &minute, &seconds);
260  break;
261  }
262  break;
263  case DB_HOUR:
264  switch (to) {
265  case DB_HOUR:
266  sscanf(buf, "%d", &hour);
267  break;
268  case DB_MINUTE:
269  sscanf(buf, "%d%*c%d", &hour, &minute);
270  break;
271  case DB_SECOND:
272  case DB_FRACTION:
273  sscanf(buf, "%d%*c%d%*c%lf", &hour, &minute, &seconds);
274  break;
275  }
276  break;
277  case DB_MINUTE:
278  switch (to) {
279  case DB_MINUTE:
280  sscanf(buf, "%d", &minute);
281  break;
282  case DB_SECOND:
283  case DB_FRACTION:
284  sscanf(buf, "%d%*c%lf", &minute, &seconds);
285  break;
286  }
287  break;
288  case DB_SECOND:
289  case DB_FRACTION:
290  sscanf(buf, "%lf", &seconds);
291  break;
292  default:
293  switch (sqltype) {
294  case DB_SQL_TYPE_DATE:
295  sscanf(buf, "%d%*c%d%*c%d", &year, &month, &day);
296  break;
297  case DB_SQL_TYPE_TIME:
298  sscanf(buf, "%d%*c%d%*c%lf", &hour, &minute, &seconds);
299  break;
300  case DB_SQL_TYPE_TIMESTAMP:
301  sscanf(buf, "%d%*c%d%*c%d %d%*c%d%*c%lf",
302  &year, &month, &day, &hour, &minute, &seconds);
303  break;
304  }
305  }
306 
307  db_set_value_year(value, year);
308  db_set_value_month(value, month);
309  db_set_value_day(value, day);
310  db_set_value_hour(value, hour);
311  db_set_value_minute(value, minute);
312  db_set_value_seconds(value, seconds);
313 
314  return DB_OK;
315 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void db_set_value_year(dbValue *value, int year)
Definition: value.c:205
int db_get_value_day(dbValue *value)
Definition: value.c:103
int db_get_value_month(dbValue *value)
Definition: value.c:92
void db_set_value_month(dbValue *value, int month)
Definition: value.c:217
int db_test_value_datetime_current(dbValue *value)
Definition: value.c:277
int db_get_value_minute(dbValue *value)
Definition: value.c:125
void db_set_value_seconds(dbValue *value, double seconds)
Definition: value.c:265
int db_convert_value_datetime_into_string(dbValue *value, int sqltype, dbString *string)
Definition: datetime.c:14
int db_nocase_compare(const char *a, const char *b)
Definition: case.c:57
void db_set_value_day(dbValue *value, int day)
Definition: value.c:229
double db_get_value_seconds(dbValue *value)
Definition: value.c:136
void db_set_value_minute(dbValue *value, int minute)
Definition: value.c:253
char * value
Definition: env.c:30
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int db_get_value_hour(dbValue *value)
Definition: value.c:114
int db_get_value_year(dbValue *value)
Definition: value.c:81
void db_interval_range(int sqltype, int *from, int *to)
Definition: interval.c:9
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
void db_set_value_hour(dbValue *value, int hour)
Definition: value.c:241
void db_set_value_datetime_current(dbValue *value)
Definition: value.c:288
int db_convert_Cstring_to_value_datetime(const char *buf, int sqltype, dbValue *value)
Definition: datetime.c:182