~ubuntu-branches/ubuntu/utopic/pacemaker/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/common/iso8601_fields.c

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-07-16 16:40:24 UTC
  • mfrom: (1.1.11) (2.2.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130716164024-lvwrf4xivk1wdr3c
Tags: 1.1.9+git20130321-1ubuntu1
* Resync from debian expiremental.
* debian/control:
  - Use lower version for Build-Depends on libcorosync-dev
    and libqb-dev.
  - Build-Depends on libcfg-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * Copyright (C) 2005 Andrew Beekhof <andrew@beekhof.net>
3
 
 * 
4
 
 * This library is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Lesser General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2.1 of the License, or (at your option) any later version.
8
 
 * 
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Lesser General Public License for more details.
13
 
 * 
14
 
 * You should have received a copy of the GNU Lesser General Public
15
 
 * License along with this library; if not, write to the Free Software
16
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 */
18
 
 
19
 
/*
20
 
 * http://en.wikipedia.org/wiki/ISO_8601 as at 2005-08-01
21
 
 *
22
 
 */
23
 
 
24
 
#include <crm_internal.h>
25
 
#include <crm/crm.h>
26
 
#include <time.h>
27
 
#include <ctype.h>
28
 
#include <crm/common/iso8601.h>
29
 
 
30
 
#define do_add_field(atime, field, extra, limit, overflow)              \
31
 
        {                                                               \
32
 
                crm_trace("Adding %d to %d (limit=%d)",         \
33
 
                            extra, atime->field, limit);                \
34
 
                atime->field += extra;                                  \
35
 
                if(limit > 0) {                                         \
36
 
                        while(limit < atime->field) {                   \
37
 
                                crm_trace("Overflowing: %d", atime->field); \
38
 
                                atime->field -= limit;                  \
39
 
                                overflow(atime, 1);                     \
40
 
                        }                                               \
41
 
                }                                                       \
42
 
                atime->field = atime->field;                            \
43
 
                crm_trace("Result: %d", atime->field);          \
44
 
        }
45
 
 
46
 
#define do_add_days_field(atime, field, extra, overflow)                \
47
 
        {                                                               \
48
 
                int __limit = days_per_month(atime->months, atime->years);      \
49
 
                crm_trace("Adding %d to %d (limit=%d)",         \
50
 
                            extra, atime->field, __limit);              \
51
 
                atime->field += extra;                                  \
52
 
                if(__limit > 0) {                                               \
53
 
                        while(__limit < atime->field) {                 \
54
 
                                crm_trace("Overflowing: %d", atime->field); \
55
 
                                overflow(atime, 1);                     \
56
 
                                __limit = days_per_month(atime->months, atime->years);  \
57
 
                                atime->field -= __limit;                        \
58
 
                        }                                               \
59
 
                }                                                       \
60
 
                atime->field = atime->field;                            \
61
 
                crm_trace("Result: %d", atime->field);          \
62
 
        }
63
 
 
64
 
#define do_add_time_field(atime, field, extra, limit, overflow)         \
65
 
        {                                                               \
66
 
                crm_trace("Adding %d to %d (limit=%d)",         \
67
 
                            extra, atime->field, limit);                \
68
 
                atime->field += extra;                                  \
69
 
                if(limit > 0) {                                         \
70
 
                        while(limit <= atime->field) {                  \
71
 
                                crm_trace("Overflowing: %d", atime->field); \
72
 
                                atime->field -= limit;                  \
73
 
                                overflow(atime, 1);                     \
74
 
                        }                                               \
75
 
                }                                                       \
76
 
                atime->field = atime->field;                            \
77
 
                crm_trace("Result: %d", atime->field);          \
78
 
        }
79
 
 
80
 
#define do_sub_field(atime, field, extra, limit, overflow)              \
81
 
        {                                                               \
82
 
                crm_trace("Subtracting %d from %d (limit=%d)",  \
83
 
                            extra, atime->field, limit);                \
84
 
                atime->field -= extra;                                  \
85
 
                while(atime->field < 1) {                               \
86
 
                        crm_trace("Underflowing: %d", atime->field);    \
87
 
                        atime->field += limit;                          \
88
 
                        overflow(atime, 1);                             \
89
 
                }                                                       \
90
 
                crm_trace("Result: %d", atime->field);          \
91
 
        }
92
 
 
93
 
#define do_sub_days_field(atime, field, extra, overflow)                \
94
 
        {                                                               \
95
 
                int __limit = days_per_month(atime->months, atime->years);      \
96
 
                crm_trace("Subtracting %d from %d (__limit=%d)",        \
97
 
                            extra, atime->field, __limit);              \
98
 
                atime->field -= extra;                                  \
99
 
                while(atime->field < 1) {                               \
100
 
                        crm_trace("Underflowing: %d", atime->field);    \
101
 
                        overflow(atime, 1);                             \
102
 
                        __limit = days_per_month(atime->months, atime->years);  \
103
 
                        atime->field += __limit;                                \
104
 
                }                                                       \
105
 
                crm_trace("Result: %d", atime->field);          \
106
 
        }
107
 
#define do_sub_time_field(atime, field, extra, limit, overflow)         \
108
 
        {                                                               \
109
 
                crm_trace("Subtracting %d from %d (limit=%d)",  \
110
 
                            extra, atime->field, limit);                \
111
 
                atime->field -= extra;                                  \
112
 
                while(atime->field < 0) {                               \
113
 
                        crm_trace("Underflowing: %d", atime->field);    \
114
 
                        atime->field += limit;                          \
115
 
                        overflow(atime, 1);                             \
116
 
                }                                                       \
117
 
                crm_trace("Result: %d", atime->field);          \
118
 
        }
119
 
 
120
 
void
121
 
add_seconds(ha_time_t * a_time, int extra)
122
 
{
123
 
    if (extra < 0) {
124
 
        sub_seconds(a_time, -extra);
125
 
    } else {
126
 
        do_add_time_field(a_time, seconds, extra, 60, add_minutes);
127
 
    }
128
 
}
129
 
 
130
 
void
131
 
add_minutes(ha_time_t * a_time, int extra)
132
 
{
133
 
    if (extra < 0) {
134
 
        sub_minutes(a_time, -extra);
135
 
    } else {
136
 
        do_add_time_field(a_time, minutes, extra, 60, add_hours);
137
 
    }
138
 
}
139
 
 
140
 
void
141
 
add_hours(ha_time_t * a_time, int extra)
142
 
{
143
 
    if (extra < 0) {
144
 
        sub_hours(a_time, -extra);
145
 
    } else {
146
 
        do_add_time_field(a_time, hours, extra, 24, add_days);
147
 
    }
148
 
}
149
 
 
150
 
void
151
 
add_days(ha_time_t * a_time, int extra)
152
 
{
153
 
    if (a_time->has->days == FALSE) {
154
 
        crm_trace("has->days == FALSE");
155
 
        return;
156
 
    }
157
 
    if (extra < 0) {
158
 
        sub_days(a_time, -extra);
159
 
    } else {
160
 
        do_add_days_field(a_time, days, extra, add_months);
161
 
    }
162
 
 
163
 
    convert_from_gregorian(a_time);
164
 
}
165
 
 
166
 
void
167
 
add_weekdays(ha_time_t * a_time, int extra)
168
 
{
169
 
    if (a_time->has->weekdays == FALSE) {
170
 
        crm_trace("has->weekdays == FALSE");
171
 
        return;
172
 
    }
173
 
    if (extra < 0) {
174
 
        sub_weekdays(a_time, -extra);
175
 
    } else {
176
 
        do_add_field(a_time, weekdays, extra, 7, add_weeks);
177
 
    }
178
 
 
179
 
    convert_from_weekdays(a_time);
180
 
}
181
 
 
182
 
void
183
 
add_yeardays(ha_time_t * a_time, int extra)
184
 
{
185
 
    if (a_time->has->yeardays == FALSE) {
186
 
        crm_trace("has->yeardays == FALSE");
187
 
        return;
188
 
    }
189
 
    if (extra < 0) {
190
 
        sub_yeardays(a_time, -extra);
191
 
    } else {
192
 
        /* coverity[result_independent_of_operands] Not interesting */
193
 
        do_add_field(a_time, yeardays, extra,
194
 
                     (is_leap_year(a_time->years) ? 366 : 365), add_ordinalyears);
195
 
    }
196
 
 
197
 
    convert_from_ordinal(a_time);
198
 
}
199
 
 
200
 
void
201
 
add_weeks(ha_time_t * a_time, int extra)
202
 
{
203
 
    if (a_time->has->weeks == FALSE) {
204
 
        crm_trace("has->weeks == FALSE");
205
 
        return;
206
 
    }
207
 
    if (extra < 0) {
208
 
        sub_weeks(a_time, -extra);
209
 
    } else {
210
 
        do_add_field(a_time, weeks, extra, weeks_in_year(a_time->years), add_weekyears);
211
 
    }
212
 
 
213
 
    convert_from_weekdays(a_time);
214
 
}
215
 
 
216
 
void
217
 
add_months(ha_time_t * a_time, int extra)
218
 
{
219
 
    int max = 0;
220
 
 
221
 
    if (a_time->has->months == FALSE) {
222
 
        crm_trace("has->months == FALSE");
223
 
        return;
224
 
    }
225
 
    if (extra < 0) {
226
 
        sub_months(a_time, -extra);
227
 
    } else {
228
 
        do_add_field(a_time, months, extra, 12, add_years);
229
 
    }
230
 
 
231
 
    max = days_per_month(a_time->months, a_time->years);
232
 
    if (a_time->days > max) {
233
 
        a_time->days = max;
234
 
    }
235
 
    convert_from_gregorian(a_time);
236
 
}
237
 
 
238
 
void
239
 
add_years(ha_time_t * a_time, int extra)
240
 
{
241
 
    if (a_time->has->years == FALSE) {
242
 
        crm_trace("has->years == FALSE");
243
 
        return;
244
 
    }
245
 
    a_time->years += extra;
246
 
    convert_from_gregorian(a_time);
247
 
}
248
 
 
249
 
void
250
 
add_ordinalyears(ha_time_t * a_time, int extra)
251
 
{
252
 
    if (a_time->has->years == FALSE) {
253
 
        crm_trace("has->years == FALSE");
254
 
        return;
255
 
    }
256
 
    a_time->years += extra;
257
 
    convert_from_ordinal(a_time);
258
 
}
259
 
 
260
 
void
261
 
add_weekyears(ha_time_t * a_time, int extra)
262
 
{
263
 
    if (a_time->has->weekyears == FALSE) {
264
 
        crm_trace("has->weekyears == FALSE");
265
 
        return;
266
 
    }
267
 
    a_time->weekyears += extra;
268
 
    convert_from_weekdays(a_time);
269
 
}
270
 
 
271
 
void
272
 
sub_seconds(ha_time_t * a_time, int extra)
273
 
{
274
 
    if (extra < 0) {
275
 
        add_seconds(a_time, -extra);
276
 
    } else {
277
 
        do_sub_time_field(a_time, seconds, extra, 60, sub_minutes);
278
 
    }
279
 
}
280
 
 
281
 
void
282
 
sub_minutes(ha_time_t * a_time, int extra)
283
 
{
284
 
    if (extra < 0) {
285
 
        add_minutes(a_time, -extra);
286
 
    } else {
287
 
        do_sub_time_field(a_time, minutes, extra, 60, sub_hours);
288
 
    }
289
 
}
290
 
 
291
 
void
292
 
sub_hours(ha_time_t * a_time, int extra)
293
 
{
294
 
    if (extra < 0) {
295
 
        add_hours(a_time, -extra);
296
 
    } else {
297
 
        do_sub_time_field(a_time, hours, extra, 24, sub_days);
298
 
    }
299
 
}
300
 
 
301
 
void
302
 
sub_days(ha_time_t * a_time, int extra)
303
 
{
304
 
    if (a_time->has->days == FALSE) {
305
 
        crm_trace("has->days == FALSE");
306
 
        return;
307
 
    }
308
 
 
309
 
    crm_trace("Subtracting %d days from %.4d-%.2d-%.2d",
310
 
              extra, a_time->years, a_time->months, a_time->days);
311
 
 
312
 
    if (extra < 0) {
313
 
        add_days(a_time, -extra);
314
 
    } else {
315
 
        do_sub_days_field(a_time, days, extra, sub_months);
316
 
    }
317
 
 
318
 
    convert_from_gregorian(a_time);
319
 
}
320
 
 
321
 
void
322
 
sub_weekdays(ha_time_t * a_time, int extra)
323
 
{
324
 
    if (a_time->has->weekdays == FALSE) {
325
 
        crm_trace("has->weekdays == FALSE");
326
 
        return;
327
 
    }
328
 
 
329
 
    crm_trace("Subtracting %d days from %.4d-%.2d-%.2d",
330
 
              extra, a_time->years, a_time->months, a_time->days);
331
 
 
332
 
    if (extra < 0) {
333
 
        add_weekdays(a_time, -extra);
334
 
    } else {
335
 
        do_sub_field(a_time, weekdays, extra, 7, sub_weeks);
336
 
    }
337
 
 
338
 
    convert_from_weekdays(a_time);
339
 
}
340
 
 
341
 
void
342
 
sub_yeardays(ha_time_t * a_time, int extra)
343
 
{
344
 
    if (a_time->has->yeardays == FALSE) {
345
 
        crm_trace("has->yeardays == FALSE");
346
 
        return;
347
 
    }
348
 
 
349
 
    crm_trace("Subtracting %d days from %.4d-%.3d", extra, a_time->years, a_time->yeardays);
350
 
 
351
 
    if (extra < 0) {
352
 
        add_yeardays(a_time, -extra);
353
 
    } else {
354
 
        do_sub_field(a_time, yeardays, extra,
355
 
                     is_leap_year(a_time->years) ? 366 : 365, sub_ordinalyears);
356
 
    }
357
 
 
358
 
    convert_from_ordinal(a_time);
359
 
}
360
 
 
361
 
void
362
 
sub_weeks(ha_time_t * a_time, int extra)
363
 
{
364
 
    if (a_time->has->weeks == FALSE) {
365
 
        crm_trace("has->weeks == FALSE");
366
 
        return;
367
 
    }
368
 
    if (extra < 0) {
369
 
        add_weeks(a_time, -extra);
370
 
    } else {
371
 
        do_sub_field(a_time, weeks, extra, weeks_in_year(a_time->years), sub_weekyears);
372
 
    }
373
 
 
374
 
    convert_from_weekdays(a_time);
375
 
}
376
 
 
377
 
void
378
 
sub_months(ha_time_t * a_time, int extra)
379
 
{
380
 
    if (a_time->has->months == FALSE) {
381
 
        crm_trace("has->months == FALSE");
382
 
        return;
383
 
    }
384
 
    if (extra < 0) {
385
 
        add_months(a_time, -extra);
386
 
    } else {
387
 
        do_sub_field(a_time, months, extra, 12, sub_years);
388
 
    }
389
 
    convert_from_gregorian(a_time);
390
 
}
391
 
 
392
 
void
393
 
sub_years(ha_time_t * a_time, int extra)
394
 
{
395
 
    if (a_time->has->years == FALSE) {
396
 
        crm_trace("has->years == FALSE");
397
 
        return;
398
 
    }
399
 
    a_time->years -= extra;
400
 
    convert_from_gregorian(a_time);
401
 
}
402
 
 
403
 
void
404
 
sub_weekyears(ha_time_t * a_time, int extra)
405
 
{
406
 
    if (a_time->has->weekyears == FALSE) {
407
 
        crm_trace("has->weekyears == FALSE");
408
 
        return;
409
 
    }
410
 
    a_time->weekyears -= extra;
411
 
 
412
 
    convert_from_weekdays(a_time);
413
 
}
414
 
 
415
 
void
416
 
sub_ordinalyears(ha_time_t * a_time, int extra)
417
 
{
418
 
    if (a_time->has->years == FALSE) {
419
 
        crm_trace("has->years == FALSE");
420
 
        return;
421
 
    }
422
 
    a_time->years -= extra;
423
 
 
424
 
    convert_from_ordinal(a_time);
425
 
}