GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-8828094037
values.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
3  *
4  * This program is free software under the GPL (>=v2)
5  * Read the file GPL.TXT coming with GRASS for details.
6  */
7 #include <grass/datetime.h>
8 
9 static int have(int x, const DateTime *dt)
10 {
11  return datetime_is_between(x, dt->from, dt->to);
12 }
13 
14 /*!
15  * \brief
16  *
17  * Returns:
18  * 0 is legal year for dt
19  * -1 illegal year for this dt
20  * -2 dt has no year component
21  *
22  * \param dt
23  * \param year
24  * \return int
25  */
26 
27 int datetime_check_year(const DateTime *dt, int year)
28 {
29  if (!have(DATETIME_YEAR, dt))
30  return datetime_error(-2, "datetime has no year");
31  if (year < 0)
32  return datetime_error(-1, "invalid datetime year");
33  if (datetime_is_absolute(dt) && year <= 0)
34  return datetime_error(-1, "invalid datetime year");
35 
36  return 0;
37 }
38 
39 /*!
40  * \brief
41  *
42  * Returns:
43  * 0 is legal month for dt
44  * -1 illegal month for this dt
45  * -2 dt has no month component
46  *
47  * \param dt
48  * \param month
49  * \return int
50  */
51 
52 int datetime_check_month(const DateTime *dt, int month)
53 {
54  if (!have(DATETIME_MONTH, dt))
55  return datetime_error(-2, "datetime has no month");
56  if (month < 0)
57  return datetime_error(-1, "invalid datetime month");
58  if (datetime_is_absolute(dt) && (month < 1 || month > 12))
59  return datetime_error(-1, "invalid datetime month");
60  /*
61  if (dt->from != DATETIME_MONTH && month > 11)
62  return datetime_error(-1,"invalid datetime month");
63  BILL CHANGED TO: */
64 
65  if (datetime_is_relative(dt) && dt->from != DATETIME_MONTH && month > 11)
66  return datetime_error(-1, "invalid datetime month");
67 
68  return 0;
69 }
70 
71 /*!
72  * \brief
73  *
74  * Returns:
75  * 0 is legal day for dt
76  * -1 illegal day for this dt
77  * -2 dt has no day component<br>
78  * Note: if dt.mode is ABSOLUTE, then dt.year and
79  * dt.month must also be legal, since the 'day' must be a legal value for the
80  * dt.year/dt.month
81  *
82  * \param dt
83  * \param day
84  * \return int
85  */
86 
87 int datetime_check_day(const DateTime *dt, int day)
88 {
89  int month, year, ad;
90  int stat;
91 
92  if (!have(DATETIME_DAY, dt))
93  return datetime_error(-2, "datetime has no day");
94  if (day < 0)
95  return datetime_error(-1, "invalid datetime day");
96  if (datetime_is_absolute(dt)) {
97  stat = datetime_get_month(dt, &month);
98  if (stat != 0)
99  return stat;
100  stat = datetime_get_year(dt, &year);
101  if (stat != 0)
102  return stat;
103  ad = datetime_is_positive(dt);
104  if (day < 1 || day > datetime_days_in_month(year, month, ad))
105  return datetime_error(-1, "invalid datetime day");
106  }
107 
108  return 0;
109 }
110 
111 /*!
112  * \brief
113  *
114  * returns:
115  * 0 on success
116  * -1 if 'dt' has an invalid hour
117  * -2 if 'dt' has no hour
118  *
119  * \param dt
120  * \param hour
121  * \return int
122  */
123 
124 int datetime_check_hour(const DateTime *dt, int hour)
125 {
126  if (!have(DATETIME_HOUR, dt))
127  return datetime_error(-2, "datetime has no hour");
128  if (hour < 0)
129  return datetime_error(-1, "invalid datetime hour");
130  if (dt->from != DATETIME_HOUR && hour > 23)
131  return datetime_error(-1, "invalid datetime hour");
132 
133  return 0;
134 }
135 
136 /*!
137  * \brief
138  *
139  * returns:
140  * 0 on success
141  * -1 if 'dt' has an invalid minute
142  * -2 if 'dt' has no minute
143  *
144  * \param dt
145  * \param minute
146  * \return int
147  */
148 
149 int datetime_check_minute(const DateTime *dt, int minute)
150 {
151  if (!have(DATETIME_MINUTE, dt))
152  return datetime_error(-2, "datetime has no minute");
153  if (minute < 0)
154  return datetime_error(-1, "invalid datetime minute");
155  if (dt->from != DATETIME_MINUTE && minute > 59)
156  return datetime_error(-1, "invalid datetime minute");
157 
158  return 0;
159 }
160 
161 /*!
162  * \brief
163  *
164  * returns:
165  * 0 on success
166  * -1 if 'dt' has an invalid second
167  * -2 if 'dt' has no second
168  *
169  * \param dt
170  * \param second
171  * \return int
172  */
173 
174 int datetime_check_second(const DateTime *dt, double second)
175 {
176  if (!have(DATETIME_SECOND, dt))
177  return datetime_error(-2, "datetime has no second");
178  if (second < 0)
179  return datetime_error(-1, "invalid datetime second");
180  if (dt->from != DATETIME_SECOND && second >= 60.0)
181  return datetime_error(-1, "invalid datetime second");
182 
183  return 0;
184 }
185 
186 /*!
187  * \brief
188  *
189  * returns:
190  * 0 on success
191  * -1 if 'dt' has an invalid fracsec
192  * -2 if 'dt' has no fracsec
193  *
194  * \param dt
195  * \param fracsec
196  * \return int
197  */
198 
199 int datetime_check_fracsec(const DateTime *dt, int fracsec)
200 {
201  if (!have(DATETIME_SECOND, dt))
202  return datetime_error(-2, "datetime has no fracsec");
203  if (fracsec < 0)
204  return datetime_error(-1, "invalid datetime fracsec");
205  return 0;
206 }
207 
208 /*!
209  * \brief
210  *
211  * returns 0 on success or negative value on error
212  *
213  * \param dt
214  * \param year
215  * \return int
216  */
217 
218 int datetime_get_year(const DateTime *dt, int *year)
219 {
220  int stat;
221 
222  stat = datetime_check_year(dt, dt->year);
223  if (stat == 0)
224  *year = dt->year;
225 
226  return stat;
227 }
228 
229 /*!
230  * \brief
231  *
232  * if dt.mode = ABSOLUTE, this also sets dt.day = 0
233  *
234  * returns 0 on success or negative value on error
235  *
236  * \param dt
237  * \param year
238  * \return int
239  */
240 
241 int datetime_set_year(DateTime *dt, int year)
242 {
243  int stat;
244 
245  stat = datetime_check_year(dt, year);
246  if (stat == 0) {
247  dt->year = year;
248  if (datetime_is_absolute(dt))
249  dt->day = 0;
250  }
251 
252  return stat;
253 }
254 
255 /*!
256  * \brief
257  *
258  * returns 0 on success or negative value on error
259  *
260  * \param dt
261  * \param month
262  * \return int
263  */
264 
265 int datetime_get_month(const DateTime *dt, int *month)
266 {
267  int stat;
268 
269  stat = datetime_check_month(dt, dt->month);
270  if (stat == 0)
271  *month = dt->month;
272 
273  return stat;
274 }
275 
276 /*!
277  * \brief
278  *
279  * if dt.mode = ABSOLUTE, this also sets dt.day = 0
280  *
281  * returns 0 on success or negative value on error
282  *
283  * \param dt
284  * \param month
285  * \return int
286  */
287 
288 int datetime_set_month(DateTime *dt, int month)
289 {
290  int stat;
291 
292  stat = datetime_check_month(dt, month);
293  if (stat == 0) {
294  dt->month = month;
295  if (datetime_is_absolute(dt))
296  dt->day = 0;
297  }
298 
299  return stat;
300 }
301 
302 /*!
303  * \brief
304  *
305  * returns 0 on success or negative value on error
306  *
307  * \param dt
308  * \param day
309  * \return int
310  */
311 
312 int datetime_get_day(const DateTime *dt, int *day)
313 {
314  int stat;
315 
316  stat = datetime_check_day(dt, dt->day);
317  if (stat == 0)
318  *day = dt->day;
319 
320  return stat;
321 }
322 
323 /*!
324  * \brief
325  *
326  * if dt.mode = ABSOLUTE, then the dt.year, dt.month:
327  \code
328  if (day > <b>datetime_days_in_month</b> (dt.year, dt.month))
329  {error}
330  \endcode
331  * This implies that year/month must be set for ABSOLUTE datetimes.
332  *
333  * Returns 0 on success or negative value on error
334  *
335  * \param dt
336  * \param day
337  * \return int
338  */
339 
340 int datetime_set_day(DateTime *dt, int day)
341 {
342  int stat;
343 
344  stat = datetime_check_day(dt, day);
345  if (stat == 0)
346  dt->day = day;
347 
348  return stat;
349 }
350 
351 /*!
352  * \brief
353  *
354  * returns 0 on success or negative value on error
355  *
356  * \param dt
357  * \param hour
358  * \return int
359  */
360 
361 int datetime_get_hour(const DateTime *dt, int *hour)
362 {
363  int stat;
364 
365  stat = datetime_check_hour(dt, dt->hour);
366  if (stat == 0)
367  *hour = dt->hour;
368 
369  return stat;
370 }
371 
372 /*!
373  * \brief
374  *
375  * returns 0 on success or negative value on error
376  *
377  * \param dt
378  * \param hour
379  * \return int
380  */
381 
382 int datetime_set_hour(DateTime *dt, int hour)
383 {
384  int stat;
385 
386  stat = datetime_check_hour(dt, hour);
387  if (stat == 0)
388  dt->hour = hour;
389 
390  return stat;
391 }
392 
393 /*!
394  * \brief
395  *
396  * returns 0 on success or negative value on error
397  *
398  * \param dt
399  * \param minute
400  * \return int
401  */
402 
403 int datetime_get_minute(const DateTime *dt, int *minute)
404 {
405  int stat;
406 
407  stat = datetime_check_minute(dt, dt->minute);
408  if (stat == 0)
409  *minute = dt->minute;
410 
411  return stat;
412 }
413 
414 /*!
415  * \brief
416  *
417  * returns 0 on success or negative value on error
418  *
419  * \param dt
420  * \param minute
421  * \return int
422  */
423 
424 int datetime_set_minute(DateTime *dt, int minute)
425 {
426  int stat;
427 
428  stat = datetime_check_minute(dt, minute);
429  if (stat == 0)
430  dt->minute = minute;
431 
432  return stat;
433 }
434 
435 /*!
436  * \brief
437  *
438  * returns 0 on success or negative value on error
439  *
440  * \param dt
441  * \param second
442  * \return int
443  */
444 
445 int datetime_get_second(const DateTime *dt, double *second)
446 {
447  int stat;
448 
449  stat = datetime_check_second(dt, dt->second);
450  if (stat == 0)
451  *second = dt->second;
452 
453  return stat;
454 }
455 
456 /*!
457  * \brief
458  *
459  * returns 0 on success or negative value on error
460  *
461  * \param dt
462  * \param second
463  * \return int
464  */
465 
466 int datetime_set_second(DateTime *dt, double second)
467 {
468  int stat;
469 
470  stat = datetime_check_second(dt, second);
471  if (stat == 0)
472  dt->second = second;
473 
474  return stat;
475 }
476 
477 /*!
478  * \brief
479  *
480  * returns 0 on success or negative value on error
481  *
482  * \param dt
483  * \param fracsec
484  * \return int
485  */
486 
487 int datetime_get_fracsec(const DateTime *dt, int *fracsec)
488 {
489  int stat;
490 
491  stat = datetime_check_fracsec(dt, dt->fracsec);
492  if (stat == 0)
493  *fracsec = dt->fracsec;
494 
495  return stat;
496 }
497 
498 /*!
499  * \brief
500  *
501  * returns 0 on success or negative value on error
502  *
503  * \param dt
504  * \param fracsec
505  * \return int
506  */
507 
508 int datetime_set_fracsec(DateTime *dt, int fracsec)
509 {
510  int stat;
511 
512  stat = datetime_check_fracsec(dt, fracsec);
513  if (stat == 0)
514  dt->fracsec = fracsec;
515 
516  return stat;
517 }
#define DATETIME_MONTH
Definition: datetime.h:11
#define DATETIME_DAY
Definition: datetime.h:12
#define DATETIME_HOUR
Definition: datetime.h:13
#define DATETIME_SECOND
Definition: datetime.h:15
#define DATETIME_MINUTE
Definition: datetime.h:14
#define DATETIME_YEAR
Definition: datetime.h:10
int datetime_error(int code, char *msg)
record 'code' and 'msg' as error code/msg (in static variables) code==0 will clear the error (ie set ...
int datetime_is_positive(const DateTime *dt)
Returns: 1 if the Datetime is positive 0 otherwise.
Definition: sign.c:20
int datetime_days_in_month(int year, int month, int ad)
returns number of days in 'month' of a particular 'year'
Definition: datetime/misc.c:61
int datetime_is_between(int x, int a, int b)
Definition: between.c:8
int datetime_is_absolute(const DateTime *dt)
Returns: 1 if dt.mode is absolute 0 if not (even if dt.mode is not defined)
int datetime_is_relative(const DateTime *dt)
Returns: 1 if dt.mode is relative 0 if not (even if dt.mode is not defined)
int month
Definition: datetime.h:21
int year
Definition: datetime.h:21
int to
Definition: datetime.h:19
double second
Definition: datetime.h:23
int fracsec
Definition: datetime.h:20
int from
Definition: datetime.h:19
int hour
Definition: datetime.h:22
int minute
Definition: datetime.h:22
int day
Definition: datetime.h:21
int datetime_set_fracsec(DateTime *dt, int fracsec)
returns 0 on success or negative value on error
Definition: values.c:508
int datetime_get_second(const DateTime *dt, double *second)
returns 0 on success or negative value on error
Definition: values.c:445
int datetime_check_year(const DateTime *dt, int year)
Returns: 0 is legal year for dt -1 illegal year for this dt -2 dt has no year component.
Definition: values.c:27
int datetime_get_fracsec(const DateTime *dt, int *fracsec)
returns 0 on success or negative value on error
Definition: values.c:487
int datetime_set_day(DateTime *dt, int day)
if dt.mode = ABSOLUTE, then the dt.year, dt.month:
Definition: values.c:340
int datetime_check_day(const DateTime *dt, int day)
Returns: 0 is legal day for dt -1 illegal day for this dt -2 dt has no day component Note: if dt....
Definition: values.c:87
int datetime_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:288
int datetime_check_fracsec(const DateTime *dt, int fracsec)
returns: 0 on success -1 if 'dt' has an invalid fracsec -2 if 'dt' has no fracsec
Definition: values.c:199
int datetime_check_month(const DateTime *dt, int month)
Returns: 0 is legal month for dt -1 illegal month for this dt -2 dt has no month component.
Definition: values.c:52
int datetime_get_hour(const DateTime *dt, int *hour)
returns 0 on success or negative value on error
Definition: values.c:361
int datetime_get_year(const DateTime *dt, int *year)
returns 0 on success or negative value on error
Definition: values.c:218
int datetime_check_minute(const DateTime *dt, int minute)
returns: 0 on success -1 if 'dt' has an invalid minute -2 if 'dt' has no minute
Definition: values.c:149
int datetime_set_hour(DateTime *dt, int hour)
returns 0 on success or negative value on error
Definition: values.c:382
int datetime_set_year(DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:241
int datetime_get_minute(const DateTime *dt, int *minute)
returns 0 on success or negative value on error
Definition: values.c:403
int datetime_set_second(DateTime *dt, double second)
returns 0 on success or negative value on error
Definition: values.c:466
int datetime_get_day(const DateTime *dt, int *day)
returns 0 on success or negative value on error
Definition: values.c:312
int datetime_check_hour(const DateTime *dt, int hour)
returns: 0 on success -1 if 'dt' has an invalid hour -2 if 'dt' has no hour
Definition: values.c:124
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
Definition: values.c:424
int datetime_check_second(const DateTime *dt, double second)
returns: 0 on success -1 if 'dt' has an invalid second -2 if 'dt' has no second
Definition: values.c:174
int datetime_get_month(const DateTime *dt, int *month)
returns 0 on success or negative value on error
Definition: values.c:265
#define x