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