GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
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;
49
datetime_unset_timezone
(dt);
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
int
datetime_is_valid_type
(
const
DateTime *dt)
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
*/
110
int
datetime_check_type
(
const
DateTime *dt)
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
123
if
(!
datetime_is_between
(dt->from, DATETIME_YEAR, DATETIME_SECOND))
124
return
datetime_error
(-2,
"invalid datetime 'from'"
);
125
if
(!
datetime_is_between
(dt->to, DATETIME_YEAR, DATETIME_SECOND))
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) {
130
if
(
datetime_in_interval_year_month
(dt->from) &&
131
!
datetime_in_interval_year_month
(dt->to))
132
return
datetime_error
(-5,
"invalid relative datetime 'from-to'"
);
133
if
(
datetime_in_interval_day_second
(dt->from) &&
134
!
datetime_in_interval_day_second
(dt->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
145
int
datetime_in_interval_year_month
(
int
x
)
146
{
147
return
datetime_is_between
(
x
, DATETIME_YEAR, DATETIME_MONTH);
148
}
149
150
int
datetime_in_interval_day_second
(
int
x
)
151
{
152
return
datetime_is_between
(
x
, DATETIME_DAY, DATETIME_SECOND);
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
*/
165
int
datetime_is_absolute
(
const
DateTime *dt)
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
*/
180
int
datetime_is_relative
(
const
DateTime *dt)
181
{
182
return
(dt->mode == DATETIME_RELATIVE);
183
}
datetime_is_between
int datetime_is_between(int x, int a, int b)
Definition
between.c:7
datetime_error
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 ...
Definition
datetime/error.c:27
datetime_in_interval_day_second
int datetime_in_interval_day_second(int x)
Definition
type.c:150
datetime_get_type
int datetime_get_type(const DateTime *dt, int *mode, int *from, int *to, int *fracsec)
Definition
type.c:56
datetime_is_valid_type
int datetime_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
Definition
type.c:76
datetime_is_absolute
int datetime_is_absolute(const DateTime *dt)
Returns: 1 if dt.mode is absolute 0 if not (even if dt.mode is not defined).
Definition
type.c:165
datetime_set_type
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
Definition
type.c:36
datetime_is_relative
int datetime_is_relative(const DateTime *dt)
Returns: 1 if dt.mode is relative 0 if not (even if dt.mode is not defined).
Definition
type.c:180
datetime_check_type
int datetime_check_type(const DateTime *dt)
checks the mode/from/to/fracsec in dt. Returns:
Definition
type.c:110
datetime_in_interval_year_month
int datetime_in_interval_year_month(int x)
Definition
type.c:145
datetime_unset_timezone
int datetime_unset_timezone(DateTime *dt)
unsets timezone in 'dt' returns 0
Definition
tz1.c:84
x
#define x
datetime
type.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0