GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
datetime/type.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 
37 int datetime_set_type(DateTime * dt, int mode, int from, int to, int fracsec)
38 {
39  dt->mode = mode;
40  dt->from = from;
41  dt->to = to;
42  dt->fracsec = fracsec;
43 
44  dt->year = 0;
45  dt->month = 0;
46  dt->day = 0;
47  dt->hour = 0;
48  dt->minute = 0;
49  dt->second = 0.0;
51 
52  dt->positive = 1;
53 
54  return datetime_check_type(dt);
55 }
56 
57 int
58 datetime_get_type(const DateTime * dt, int *mode, int *from, int *to,
59  int *fracsec)
60 {
61  *mode = dt->mode;
62  *to = dt->to;
63  *from = dt->from;
64  *fracsec = dt->fracsec;
65  return datetime_check_type(dt);
66 }
67 
68 
80 int datetime_is_valid_type(const DateTime * dt)
81 {
82  /* Returns 0 if DateTime structure is not valid. */
83  return datetime_check_type(dt) == 0;
84 }
85 
86 
116 int datetime_check_type(const DateTime * dt)
117 {
118  /* Returns 0 for a valid DateTime structure.
119  Sets the error code and error message if the structure is not
120  valid. Returns error code. */
121  switch (dt->mode) {
122  case DATETIME_ABSOLUTE:
123  case DATETIME_RELATIVE:
124  break;
125  default:
126  return datetime_error(-1, "invalid datetime 'mode'");
127  }
128 
129  if (!datetime_is_between(dt->from, DATETIME_YEAR, DATETIME_SECOND))
130  return datetime_error(-2, "invalid datetime 'from'");
131  if (!datetime_is_between(dt->to, DATETIME_YEAR, DATETIME_SECOND))
132  return datetime_error(-3, "invalid datetime 'to'");
133  if (dt->from > dt->to)
134  return datetime_error(-4, "invalid datetime 'from-to'");
135  if (dt->mode == DATETIME_RELATIVE) {
136  if (datetime_in_interval_year_month(dt->from)
138  return datetime_error(-5, "invalid relative datetime 'from-to'");
139  if (datetime_in_interval_day_second(dt->from)
141  return datetime_error(-5, "invalid relative datetime 'from-to'");
142  }
143  if (dt->mode == DATETIME_ABSOLUTE && dt->from != DATETIME_YEAR)
144  return datetime_error(-6, "invalid absolute datetime 'from'");
145  if (dt->to == DATETIME_SECOND && dt->fracsec < 0)
146  return datetime_error(-7, "invalid datetime 'fracsec'");
147 
148  return 0;
149 }
150 
152 {
153  return datetime_is_between(x, DATETIME_YEAR, DATETIME_MONTH);
154 }
155 
157 {
158  return datetime_is_between(x, DATETIME_DAY, DATETIME_SECOND);
159 }
160 
161 
173 int datetime_is_absolute(const DateTime * dt)
174 {
175  return (dt->mode == DATETIME_ABSOLUTE);
176 }
177 
178 
190 int datetime_is_relative(const DateTime * dt)
191 {
192  return (dt->mode == DATETIME_RELATIVE);
193 }
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:37
int datetime_in_interval_day_second(int x)
int datetime_is_between(int x, int a, int b)
Definition: between.c:8
int datetime_check_type(const DateTime *dt)
checks the mode/from/to/fracsec in dt. Returns:
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_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_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
Definition: datetime/type.c:80
int datetime_unset_timezone(DateTime *dt)
unsets timezone in &#39;dt&#39; returns 0
Definition: tz1.c:92
int datetime_get_type(const DateTime *dt, int *mode, int *from, int *to, int *fracsec)
Definition: datetime/type.c:58
int datetime_in_interval_year_month(int x)
tuple mode
Definition: tools.py:1481