GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-69b20cfed1
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  * \brief
11  *
12  *
13  * <ul>
14  <li> This routine must be called can be made with other datetime functions.
15  * </li>
16  <li> initialize all the elements in dt.
17  * </li>
18  <li> Set all values to zero except:
19  * tz (set to illegal value - 99*24)
20  * positive (set to 1 for positive)
21  * </li>
22  <li> Set the type info in dt: mode, from, to, fracsec
23  * </li>
24  <li> validate the mode/from/to/fracsec (according to the rules for the mode)
25  * </li>
26  <li> return the return value from <tt>datetime_check_type</tt>(dt)
27  </li></ul>
28  *
29  * \param mode
30  * \param from
31  * \param to
32  * \param fracsec
33  * \return int
34  */
35 
36 int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
37 {
38  dt->mode = mode;
39  dt->from = from;
40  dt->to = to;
41  dt->fracsec = fracsec;
42 
43  dt->year = 0;
44  dt->month = 0;
45  dt->day = 0;
46  dt->hour = 0;
47  dt->minute = 0;
48  dt->second = 0.0;
50 
51  dt->positive = 1;
52 
53  return datetime_check_type(dt);
54 }
55 
56 int datetime_get_type(const DateTime *dt, int *mode, int *from, int *to,
57  int *fracsec)
58 {
59  *mode = dt->mode;
60  *to = dt->to;
61  *from = dt->from;
62  *fracsec = dt->fracsec;
63  return datetime_check_type(dt);
64 }
65 
66 /*!
67  * \brief
68  *
69  * Returns:
70  * 1 if <b>datetime_check_type()</b> returns 0
71  * 0 if not.
72  *
73  * \param dt
74  * \return int
75  */
76 
78 {
79  /* Returns 0 if DateTime structure is not valid. */
80  return datetime_check_type(dt) == 0;
81 }
82 
83 /*!
84  * \brief
85  *
86  * checks the mode/from/to/fracsec in dt.
87  * Returns:
88  * <ul>
89  <li> 0: OK
90  </li>
91  <li> -1: mode is invalid - not one of {ABSOLUTE,RELATIVE}
92  </li>
93  <li> -2: from is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
94  </li>
95  <li> -3: to is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
96  </li>
97  <li> -4: from/to are reversed (from>to is illegal)
98  </li>
99  <li> -5: invalid from/to combination for RELATIVE mode:
100  * from in {YEAR,MONTH} but to is not, or
101  * from in {DAY,HOUR,MINUTE,SECOND} but to is not
102  </li>
103  <li> -6: from is invalid for ABSOLUTE mode (from != YEAR is illegal)
104  </li>
105  <li> -7: fracsec is negative (only if to==SECOND)
106  </li></ul>
107  *
108  * \param dt
109  * \return int
110  */
111 
113 {
114  /* Returns 0 for a valid DateTime structure.
115  Sets the error code and error message if the structure is not
116  valid. Returns error code. */
117  switch (dt->mode) {
118  case DATETIME_ABSOLUTE:
119  case DATETIME_RELATIVE:
120  break;
121  default:
122  return datetime_error(-1, "invalid datetime 'mode'");
123  }
124 
126  return datetime_error(-2, "invalid datetime 'from'");
128  return datetime_error(-3, "invalid datetime 'to'");
129  if (dt->from > dt->to)
130  return datetime_error(-4, "invalid datetime 'from-to'");
131  if (dt->mode == DATETIME_RELATIVE) {
134  return datetime_error(-5, "invalid relative datetime 'from-to'");
137  return datetime_error(-5, "invalid relative datetime 'from-to'");
138  }
139  if (dt->mode == DATETIME_ABSOLUTE && dt->from != DATETIME_YEAR)
140  return datetime_error(-6, "invalid absolute datetime 'from'");
141  if (dt->to == DATETIME_SECOND && dt->fracsec < 0)
142  return datetime_error(-7, "invalid datetime 'fracsec'");
143 
144  return 0;
145 }
146 
148 {
150 }
151 
153 {
155 }
156 
157 /*!
158  * \brief
159  *
160  * Returns:
161  * 1 if dt.mode is absolute
162  * 0 if not (even if dt.mode is not defined)
163  *
164  * \param dt
165  * \return int
166  */
167 
169 {
170  return (dt->mode == DATETIME_ABSOLUTE);
171 }
172 
173 /*!
174  * \brief
175  *
176  * Returns:
177  * 1 if dt.mode is relative
178  * 0 if not (even if dt.mode is not defined)
179  *
180  * \param dt
181  * \return int
182  */
183 
185 {
186  return (dt->mode == DATETIME_RELATIVE);
187 }
int datetime_in_interval_day_second(int x)
int datetime_get_type(const DateTime *dt, int *mode, int *from, int *to, int *fracsec)
Definition: datetime/type.c:56
int datetime_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
Definition: datetime/type.c:77
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_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition: datetime/type.c:36
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_check_type(const DateTime *dt)
checks the mode/from/to/fracsec in dt. Returns:
int datetime_in_interval_year_month(int x)
#define DATETIME_ABSOLUTE
Definition: datetime.h:4
#define DATETIME_MONTH
Definition: datetime.h:11
#define DATETIME_DAY
Definition: datetime.h:12
#define DATETIME_SECOND
Definition: datetime.h:15
#define DATETIME_RELATIVE
Definition: datetime.h:5
#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_unset_timezone(DateTime *dt)
unsets timezone in 'dt' returns 0
Definition: tz1.c:88
int datetime_is_between(int x, int a, int b)
Definition: between.c:8
int positive
Definition: datetime.h:24
int month
Definition: datetime.h:21
int year
Definition: datetime.h:21
int mode
Definition: datetime.h:18
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
#define x