File: diff.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 (282 lines) | stat: -rw-r--r-- 7,917 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * 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 <stdlib.h>
#include <grass/datetime.h>
#include "math.h"

/*************************************************************/
/*
   This performs the formula:   result = a - b;

   both a and b must be absolute.
   result will be relative
   If a is "earlier" than b, then result should be set negative.

   b must be no more "precise" than a.
   (a copy of b is "extended" to the precision of a)

   datetime_copy (tb, b)
   datetime_reset_from_to (tb, b.from, a.to, a.fracsec))


   If result.to == SECOND, then result.fracsec is a.fracsec

   result will have the following from/to based on a.to:

   result
   a.to      from    to
   YEAR      YEAR    YEAR
   MONTH     YEAR    MONTH
   DAY       DAY     DAY
   HOUR      DAY     HOUR
   MINUTE    DAY     MINUTE
   SECOND    DAY     SECOND

   If either 'a' or 'b' has a timezone, both must have a timezone.
   The difference will account for the differences in the time zones.
 */

static int _datetime_ymd_to_ddays(const DateTime *, double *);
static int _datetime_compare(const DateTime *, const DateTime *);

/*!
 * \brief
 *
 *
 * This performs the formula:   result = a - b;
 * <ul>
 <li> both a and b must be absolute.
 * </li>
 <li> result will be relative
 * </li>
 <li> If a is "earlier" than b, then result will be set negative.
 * </li>
 <li> b must be no more "precise" than a.
 * (a copy of b is "extended" to the precision of a)
 * </li>
 <li> If result.to == SECOND, then result.fracsec is a.fracsec
 * </li>
 <li> result will have the following from/to based
 * on a.to: result a.to      from    to YEAR      YEAR    YEAR MONTH     YEAR
 * MONTH DAY       DAY     DAY HOUR      DAY     HOUR MINUTE    DAY
 * MINUTE SECOND    DAY     SECOND [LAYOUT ??? - see HTML]
 * </li>
 <li> If either 'a' or 'b' has a timezone, both must have a timezone. The
 * difference will account for the differences in the time zones.
 </li></ul>

 *
 *  \param a
 *  \param b
 *  \param result
 *  \return int
 */

int datetime_difference(const DateTime *a, const DateTime *b, DateTime *result)
{
    DateTime tb, ta, *early, *late;
    int compare, tzmin;

    /* if not both absolute, return error */

    datetime_copy(&tb, b);
    datetime_change_from_to(&tb, DATETIME_YEAR, a->to, a->fracsec);

    datetime_copy(&ta, a);
    if (datetime_get_timezone(&ta, &tzmin) == 0 ||
        datetime_get_timezone(&tb, &tzmin) == 0) {
        if (datetime_get_timezone(&ta, &tzmin) == 0 &&
            datetime_get_timezone(&tb, &tzmin) == 0) {
            datetime_change_to_utc(&ta);
            datetime_change_to_utc(&tb);
        }
        else
            return datetime_error(-1,
                                  "only one opperand contains valid timezone");
    }

    /* initialize result */
    datetime_set_type(result, DATETIME_RELATIVE,
                      ta.to < DATETIME_DAY ? DATETIME_YEAR : DATETIME_DAY,
                      ta.to, ta.fracsec);
    compare = _datetime_compare(&ta, &tb);
    if (compare > 0) {
        early = &tb;
        late = &ta;
        result->positive = 1;
    }
    else if (compare < 0) {
        early = &ta;
        late = &tb;
        result->positive = 0;
    }
    else { /* equal */
        return (0);
    }

    /* now the work */
    if (datetime_in_interval_year_month(ta.to)) {
        int dm;

        if (ta.positive == tb.positive) {
            /* change if we use doubles! */
            result->year = abs(late->year - early->year);
        }
        else {
            result->year = late->year + early->year - 2;
        }
        dm = late->month - early->month;
        if (dm >= 0)
            result->month = dm;
        else {
            result->year -= 1;
            result->month = dm + 12;
        }
    }
    else {
        DateTime erel, lrel;
        double latedays, earlydays;

        datetime_set_increment_type(a, &erel);
        _datetime_ymd_to_ddays(early, &earlydays);
        /* copy day -> down */
        erel.day = earlydays;
        erel.hour = early->hour;
        erel.minute = early->minute;
        erel.second = early->second;

        datetime_set_increment_type(a, &lrel);
        _datetime_ymd_to_ddays(late, &latedays);
        /* copy day -> down */
        lrel.day = latedays;
        lrel.hour = late->hour;
        lrel.minute = late->minute;
        lrel.second = late->second;

        datetime_invert_sign(&erel);
        datetime_increment(&erel, &lrel);

        /* copy erel back to result */
        result->day = erel.day;
        result->hour = erel.hour;
        result->minute = erel.minute;
        result->second = erel.second;

        /* need carry? */
    }

    return (0);
}

/*************************************************************/
/* returns 1 if a is later than b,
   -1 if a is earlier than a,
   0 otherwise
 */
/* only looks at from-to fields defined by a */

static int _datetime_compare(const DateTime *a, const DateTime *b)
{
    int i;

    if (a->positive && !b->positive)
        return (1);
    else if (b->positive && !a->positive)
        return (-1);

    /* same signs */
    for (i = a->from; i <= a->to; i++) {
        switch (i) {

        case DATETIME_SECOND:
            if (a->second > b->second)
                return (1);
            else if (a->second < b->second)
                return (-1);
            break;

        case DATETIME_MINUTE:
            if (a->minute > b->minute)
                return (1);
            else if (a->minute < b->minute)
                return (-1);
            break;

        case DATETIME_HOUR:
            if (a->hour > b->hour)
                return (1);
            else if (a->hour < b->hour)
                return (-1);
            break;

        case DATETIME_DAY:
            if (a->day > b->day)
                return (1);
            else if (a->day < b->day)
                return (-1);
            break;

        case DATETIME_MONTH:
            if (a->month > b->month)
                return (1);
            else if (a->month < b->month)
                return (-1);
            break;

        case DATETIME_YEAR: /* only place sign matters */
            if (a->positive) {
                if (a->year > b->year)
                    return (1);
                else if (a->year < b->year)
                    return (-1);
            }
            else {
                if (a->year < b->year)
                    return (1);
                else if (a->year > b->year)
                    return (-1);
            }
            break;
        }
    }
    return (0);
}

/*************************************************************/

static int _datetime_ymd_to_ddays(const DateTime *dtymd, double *days)
{ /* note extra precision! */
    int yr, mo;

    *days = 0.0;

    if (dtymd->positive) {
        *days = dtymd->day - 1;                     /* start w/ days - 1 */
        for (mo = dtymd->month - 1; mo > 0; mo--) { /* add earlier months */
            *days += datetime_days_in_month(dtymd->year, mo, dtymd->positive);
        }
        for (yr = dtymd->year - 1; yr > 0; yr--) { /* add earlier years */
            *days += datetime_days_in_year(yr, dtymd->positive);
        }
    }
    else {
        for (yr = dtymd->year - 1; yr > 0; yr--) { /* add later years */
            *days += datetime_days_in_year(yr, dtymd->positive);
        }
        for (mo = 12; mo >= dtymd->month;
             mo--) { /*add current & later months */
            *days += datetime_days_in_month(dtymd->year, mo, dtymd->positive);
        }
        *days -= dtymd->day; /* subtract current days */
    }

    return 0;
}

/*************************************************************/

/*************************************************************/