GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 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
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_is_positive(const DateTime *dt)
Returns: 1 if the Datetime is positive 0 otherwise.
Definition: sign.c:21
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
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
int datetime_is_between(int x, int a, int b)
Definition: between.c:8
int datetime_set_fracsec(DateTime *dt, int fracsec)
returns 0 on success or negative value on error
Definition: values.c:530
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
int datetime_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
Definition: values.c:300
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 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 stat
Definition: g3dcolor.c:369
int datetime_get_fracsec(const DateTime *dt, int *fracsec)
returns 0 on success or negative value on error
Definition: values.c:508
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 ...
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_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: misc.c:64
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 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