File: change.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (238 lines) | stat: -rw-r--r-- 6,703 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (C) 1995.  Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
 *
 * This program is free software under the GPL (>=v2)
 * Read the file GPL.TXT coming with GRASS for details.
 */
#include <grass/datetime.h>

static void make_incr(DateTime *, int, int, DateTime *);

/*!
 * \brief
 *
 * Changes the from/to of the type for dt.
 * The 'from/to' must be legal
 * values for the mode of dt; (if they are not legal, then the original values
 * are preserved, dt is not changed).
 * Returns:
 * 0 OK
 * -1 invalid 'dt'
 * -2 invalid 'from/to' <br>
 <ul>
 <li> round =
 * negative implies floor() [decrease magnitude]
 * 0 implies normal rounding, [incr/decr magnitude]
 * positive implies ceil() [increase magnitude]
 </li>
 <li> If dt.from < 'from' (losing "lower" elements), convert the "lost"
 * values to the equivalent value for the new 'from' Lost elements are then set
 * to zero. (This case can only occur for dt.mode relative):
 * months += lost years * 12 ; years = 0
 * hours += lost days * 24 ; days = 0
 * minutes += lost hours * 60 ; hours = 0
 * seconds += lost minutes * 60.0 ; minutes = 0
 </li>
 <li> If dt.from > 'from' (adding "lower" elements), the new elements are set
 * to zero.
 </li>
 <li> If dt.to < 'to' (adding "higher" elements), the new elements are set to
 * zero.
 </li>
 <li> If dt.to > 'to' (losing "higher" elements), the the new 'to' is
 * adjusted according to the value for 'round' After rounding the "lost"
 * elements are set to zero.
 </li></ul>
 *
 *  \param dt
 *  \param from
 *  \param to
 *  \param round
 *  \return int
 */

int datetime_change_from_to(DateTime *dt, int from, int to, int round)
{
    DateTime dummy, incr;
    int pos;
    int carry;
    int ndays;
    int dtfrom;

    /* is 'dt' valid? */
    if (!datetime_is_valid_type(dt))
        return -1;

    /* is new from/to valid for dt->mode? */
    if (datetime_set_type(&dummy, dt->mode, from, to, 0) != 0)
        return -2;

    /* copy dt->from to local variable, then change it
       in the structure so that increment works correctly for RELATIVE.
       Otherwise, since increment "reduces" answers, performing carries,
       we would carry to invalid units */

    dtfrom = dt->from;

    /* now set the from */
    dt->from = from;

    /* convert the "lost" lower elements to equiv value for the new 'from'
     * NOTE: this only affects DATETIME_RELATIVE
     *       since absolute will have from==dt->from==YEAR
     */
    for (pos = dtfrom; pos < from; pos++) {
        switch (pos) {
        case DATETIME_YEAR:
            dt->month += dt->year * 12;
            dt->year = 0;
            break;
        case DATETIME_DAY:
            dt->hour += dt->day * 24;
            dt->day = 0;
            break;
        case DATETIME_HOUR:
            dt->minute += dt->hour * 60;
            dt->hour = 0;
            break;
        case DATETIME_MINUTE:
            dt->second += dt->minute * 60.0;
            dt->minute = 0;
            break;
        }
    }

    /* if losing precision, round
     *    round > 0 force up if any lost values not zero
     *    round ==0 increment by all lost values
     */
    if (to < dt->to) {
        if (round > 0) {
            int x;

            x = datetime_is_absolute(dt) ? 1 : 0;

            for (carry = 0, pos = dt->to; carry == 0 && pos > to; pos--) {
                switch (pos) {
                case DATETIME_MONTH:
                    if (dt->month != x)
                        carry = 1;
                    break;
                case DATETIME_DAY:
                    if (dt->day != x)
                        carry = 1;
                    break;
                case DATETIME_HOUR:
                    if (dt->hour != 0)
                        carry = 1;
                    break;
                case DATETIME_MINUTE:
                    if (dt->minute != 0)
                        carry = 1;
                    break;
                case DATETIME_SECOND:
                    if (dt->second != 0)
                        carry = 1;
                    break;
                }
            }

            if (carry) {
                make_incr(&incr, to, to, dt);

                incr.year = 1;
                incr.month = 1;
                incr.day = 1;
                incr.hour = 1;
                incr.minute = 1;
                incr.second = 1.0;

                datetime_increment(dt, &incr);
            }
        }

        if (round == 0) {
            /*NEW*/ if (datetime_is_absolute(dt))
                /*NEW*/ ndays = datetime_days_in_year(dt->year, dt->positive);
            /*NEW*/
            else
                /*NEW*/ ndays = 0;

            for (pos = dt->to; pos > to; pos--) {
                make_incr(&incr, pos, pos, dt);

                incr.year = dt->year;
                incr.month = dt->month;
                /*NEW*/ incr.day = dt->day + ndays / 2;
                incr.hour = dt->hour;
                incr.minute = dt->minute;
                incr.second = dt->second;

                datetime_increment(dt, &incr);
                /*NEW*/ if (ndays > 0 && pos == DATETIME_DAY)
                    /*NEW*/ break;
            }
        }
    }

    /* set the new elements to zero */
    for (pos = from; pos < dtfrom; pos++)
        switch (pos) {
        case DATETIME_YEAR:
            dt->year = 0;
            break;
        case DATETIME_MONTH:
            dt->month = 0;
            break;
        case DATETIME_DAY:
            dt->day = 0;
            break;
        case DATETIME_HOUR:
            dt->hour = 0;
            break;
        case DATETIME_MINUTE:
            dt->minute = 0;
            break;
        case DATETIME_SECOND:
            dt->second = 0;
            break;
        }

    for (pos = to; pos > dt->to; pos--)
        switch (pos) {
        case DATETIME_YEAR:
            dt->year = 0;
            break;
        case DATETIME_MONTH:
            dt->month = 0;
            break;
        case DATETIME_DAY:
            dt->day = 0;
            break;
        case DATETIME_HOUR:
            dt->hour = 0;
            break;
        case DATETIME_MINUTE:
            dt->minute = 0;
            break;
        case DATETIME_SECOND:
            dt->second = 0;
            break;
        }

    /* make sure that fracsec is zero if original didn't have seconds */
    if (dt->to < DATETIME_SECOND)
        dt->fracsec = 0;

    /* now set the to */
    dt->to = to;

    return 0;
}

static void make_incr(DateTime *incr, int from, int to, DateTime *dt)
{
    datetime_set_type(incr, DATETIME_RELATIVE, from, to, 0);
    if (datetime_is_relative(dt) && datetime_is_negative(dt))
        datetime_set_negative(incr);
}