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