GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
10 /*!
11  * \brief
12  *
13  *
14  * <ul>
15  <li> This routine must be called can be made with other datetime functions.
16  * </li>
17  <li> initialize all the elements in dt.
18  * </li>
19  <li> Set all values to zero except:
20  * tz (set to illegal value - 99*24)
21  * positive (set to 1 for positive)
22  * </li>
23  <li> Set the type info in dt: mode, from, to, fracsec
24  * </li>
25  <li> validate the mode/from/to/fracsec (according to the rules for the mode)
26  * </li>
27  <li> return the return value from <tt>datetime_check_type</tt>(dt)
28  </li></ul>
29  *
30  * \param mode
31  * \param from
32  * \param to
33  * \param fracsec
34  * \return int
35  */
36 
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 
69 /*!
70  * \brief
71  *
72  * Returns:
73  * 1 if <b>datetime_check_type()</b> returns 0
74  * 0 if not.
75  *
76  * \param dt
77  * \return int
78  */
79 
81 {
82  /* Returns 0 if DateTime structure is not valid. */
83  return datetime_check_type(dt) == 0;
84 }
85 
86 
87 /*!
88  * \brief
89  *
90  * checks the mode/from/to/fracsec in dt.
91  * Returns:
92  * <ul>
93  <li> 0: OK
94  </li>
95  <li> -1: mode is invalid - not one of {ABSOLUTE,RELATIVE}
96  </li>
97  <li> -2: from is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
98  </li>
99  <li> -3: to is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
100  </li>
101  <li> -4: from/to are reversed (from>to is illegal)
102  </li>
103  <li> -5: invalid from/to combination for RELATIVE mode:
104  * from in {YEAR,MONTH} but to is not, or
105  * from in {DAY,HOUR,MINUTE,SECOND} but to is not
106  </li>
107  <li> -6: from is invalid for ABSOLUTE mode (from != YEAR is illegal)
108  </li>
109  <li> -7: fracsec is negative (only if to==SECOND)
110  </li></ul>
111  *
112  * \param dt
113  * \return int
114  */
115 
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 
130  return datetime_error(-2, "invalid datetime 'from'");
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) {
138  return datetime_error(-5, "invalid relative datetime 'from-to'");
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 {
154 }
155 
157 {
159 }
160 
161 
162 /*!
163  * \brief
164  *
165  * Returns:
166  * 1 if dt.mode is absolute
167  * 0 if not (even if dt.mode is not defined)
168  *
169  * \param dt
170  * \return int
171  */
172 
174 {
175  return (dt->mode == DATETIME_ABSOLUTE);
176 }
177 
178 
179 /*!
180  * \brief
181  *
182  * Returns:
183  * 1 if dt.mode is relative
184  * 0 if not (even if dt.mode is not defined)
185  *
186  * \param dt
187  * \return int
188  */
189 
191 {
192  return (dt->mode == DATETIME_RELATIVE);
193 }
int hour
Definition: datetime.h:23
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:37
#define DATETIME_SECOND
Definition: datetime.h:15
int day
Definition: datetime.h:22
double second
Definition: datetime.h:24
#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 ...
int from
Definition: datetime.h:20
int mode
Definition: datetime.h:19
#define DATETIME_RELATIVE
Definition: datetime.h:5
#define x
int datetime_in_interval_day_second(int x)
int year
Definition: datetime.h:22
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:
#define DATETIME_YEAR
Definition: datetime.h:10
int positive
Definition: datetime.h:25
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_unset_timezone(DateTime *dt)
unsets timezone in &#39;dt&#39; returns 0
Definition: tz1.c:92
int fracsec
Definition: datetime.h:21
int datetime_is_absolute(const DateTime *dt)
Returns: 1 if dt.mode is absolute 0 if not (even if dt.mode is not defined)
#define DATETIME_DAY
Definition: datetime.h:12
int month
Definition: datetime.h:22
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 to
Definition: datetime.h:20
#define DATETIME_ABSOLUTE
Definition: datetime.h:4
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)
int minute
Definition: datetime.h:23