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