GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 dt
30  * \param mode
31  * \param from
32  * \param to
33  * \param fracsec
34  * \return int
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  */
77 {
78  /* Returns 0 if DateTime structure is not valid. */
79  return datetime_check_type(dt) == 0;
80 }
81 
82 /*!
83  * \brief
84  *
85  * checks the mode/from/to/fracsec in dt.
86  * Returns:
87  * <ul>
88  <li> 0: OK
89  </li>
90  <li> -1: mode is invalid - not one of {ABSOLUTE,RELATIVE}
91  </li>
92  <li> -2: from is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
93  </li>
94  <li> -3: to is invalid - not one of {YEAR,MONTH,DAY,HOUR,MINUTE,SECOND}
95  </li>
96  <li> -4: from/to are reversed (from>to is illegal)
97  </li>
98  <li> -5: invalid from/to combination for RELATIVE mode:
99  * from in {YEAR,MONTH} but to is not, or
100  * from in {DAY,HOUR,MINUTE,SECOND} but to is not
101  </li>
102  <li> -6: from is invalid for ABSOLUTE mode (from != YEAR is illegal)
103  </li>
104  <li> -7: fracsec is negative (only if to==SECOND)
105  </li></ul>
106  *
107  * \param dt
108  * \return int
109  */
111 {
112  /* Returns 0 for a valid DateTime structure.
113  Sets the error code and error message if the structure is not
114  valid. Returns error code. */
115  switch (dt->mode) {
116  case DATETIME_ABSOLUTE:
117  case DATETIME_RELATIVE:
118  break;
119  default:
120  return datetime_error(-1, "invalid datetime 'mode'");
121  }
122 
124  return datetime_error(-2, "invalid datetime 'from'");
126  return datetime_error(-3, "invalid datetime 'to'");
127  if (dt->from > dt->to)
128  return datetime_error(-4, "invalid datetime 'from-to'");
129  if (dt->mode == DATETIME_RELATIVE) {
132  return datetime_error(-5, "invalid relative datetime 'from-to'");
135  return datetime_error(-5, "invalid relative datetime 'from-to'");
136  }
137  if (dt->mode == DATETIME_ABSOLUTE && dt->from != DATETIME_YEAR)
138  return datetime_error(-6, "invalid absolute datetime 'from'");
139  if (dt->to == DATETIME_SECOND && dt->fracsec < 0)
140  return datetime_error(-7, "invalid datetime 'fracsec'");
141 
142  return 0;
143 }
144 
146 {
148 }
149 
151 {
153 }
154 
155 /*!
156  * \brief
157  *
158  * Returns:
159  * 1 if dt.mode is absolute
160  * 0 if not (even if dt.mode is not defined)
161  *
162  * \param dt
163  * \return int
164  */
166 {
167  return (dt->mode == DATETIME_ABSOLUTE);
168 }
169 
170 /*!
171  * \brief
172  *
173  * Returns:
174  * 1 if dt.mode is relative
175  * 0 if not (even if dt.mode is not defined)
176  *
177  * \param dt
178  * \return int
179  */
181 {
182  return (dt->mode == DATETIME_RELATIVE);
183 }
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:76
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:84
int datetime_is_between(int x, int a, int b)
Definition: between.c:7
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