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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/*
* 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 int _datetime_add_field(DateTime *, DateTime *, int);
static int _datetime_subtract_field(DateTime *, DateTime *, int);
/*****************************************************************/
#if 0 /* unused */
static double _debug_decimal(DateTime * dt)
{
double dtdec = 0.0;
if (dt->mode == DATETIME_RELATIVE) {
if (datetime_in_interval_year_month(dt->from)) {
dtdec = dt->year + dt->month / 12.;
}
else {
dtdec = dt->day / 365.25 +
dt->hour / 8766. + dt->minute / 525960.
+ dt->second / 31557600.;
}
}
if (dt->positive)
return (dtdec);
return (-dtdec);
}
#endif /* unused */
/*****************************************************************/
/*!
* \brief
*
* This function changes the 'src' date/time data based on the 'incr'
* The type (mode/from/to) of the 'src' can be anything.
* The mode of the 'incr' must be RELATIVE, and the type (mode/from/to) for
* 'incr' must be a valid increment for 'src'. See
<b>datetime_is_valid_increment()</b>,
* <b>datetime_check_increment()</b>
* Returns:
* 0: OK
* -1: 'incr' is invalid increment for 'src'
* For src.mode ABSOLUTE,
* <ul>
<li> positive 'incr' moves into the future,
</li>
<li> negative 'incr' moves into the past.
</li>
<li> BC implies the year is negative, but all else is positive. Also, year==0
* is illegal: adding 1 year to 1[bc] gives 1[ad]
</li></ul>
* The 'fracsec' in 'src' is preserved.
* The 'from/to' of the 'src' is preserved.
* A timezone in 'src' is allowed - it's presence is ignored.
* NOTE: There is no datetime_decrement() To decrement, set the 'incr' negative.
*
* \param src
* \param incr
* \return int
*/
int datetime_increment(DateTime *src, DateTime *incr)
{
int i, relfrom;
DateTime cpdt, *dt;
if (!datetime_is_valid_increment(src, incr))
return datetime_error_code();
/* special case - incrementing a relative might try to increment
or borrow from a "lower" field than src has,
so we use a copy to change from */
if (src->mode == DATETIME_RELATIVE) {
datetime_copy(&cpdt, src);
relfrom = datetime_in_interval_day_second(src->from) ? DATETIME_DAY
: DATETIME_YEAR;
datetime_change_from_to(&cpdt, relfrom, src->to, -1); /* min. from */
dt = &cpdt;
}
else
dt = src;
/* need to call carry first? (just to make sure?) */
/*
fprintf (stdout,"DEBUG: INCR %.12lf %.12lf = %.12lf\n",
_debug_decimal(dt), _debug_decimal(incr),
_debug_decimal(dt)+_debug_decimal(incr));
*/
/* no sign change, just add */
if ((dt->positive && incr->positive) ||
(dt->mode == DATETIME_RELATIVE && !dt->positive && !incr->positive)) {
for (i = incr->to; i >= incr->from; i--) {
_datetime_add_field(dt, incr, i);
}
}
else if (!incr->positive || dt->mode == DATETIME_RELATIVE) {
for (i = incr->to; i >= incr->from; i--) {
_datetime_subtract_field(dt, incr, i);
}
}
/* now only two special cases of bc ABSOLUTE left */
else if (!incr->positive) { /* incr is negative, dt is positive */
for (i = incr->to; i > DATETIME_YEAR; i--) {
_datetime_subtract_field(dt, incr, i);
}
_datetime_add_field(dt, incr, DATETIME_YEAR);
}
else { /* incr is positive, dt is negative */
for (i = incr->to; i > DATETIME_YEAR; i--) {
_datetime_add_field(dt, incr, i);
}
_datetime_subtract_field(dt, incr, DATETIME_YEAR);
}
/*
fprintf (stdout,"DEBUG: INCR RESULT = %.12lf\n", _debug_decimal(dt));
*/
if (src->mode == DATETIME_RELATIVE) {
datetime_change_from_to(dt, src->from, src->to, -1);
/* copy dt back into src to return */
datetime_copy(src, dt);
}
return 0;
}
/*****************************************************************/
/*
When calling, the field must be
in the range of src, but this is not enforced here.
The only thing used from the "incr" DateTime is the value of
the field being subtracted and the "from" & "to"
by the time we get here, if src is RELATIVE, src->from should
already be minimized to allow borrowing from "lower" fields
*/
static int _datetime_subtract_field(DateTime *src, DateTime *incr, int field)
{
if (src->mode == DATETIME_RELATIVE) {
DateTime srcinc, tinc;
int borrow = 0;
datetime_copy(&tinc, src);
datetime_copy(&srcinc, incr);
switch (field) {
case DATETIME_SECOND:
/* no "-1" here - remember seconds is floating point */
/* might result in over borrowing, so have to check */
if (src->second < incr->second) {
if ((int)(incr->second - src->second) ==
(incr->second - src->second)) { /* diff is integer */
borrow = 1 + (incr->second - src->second - 1) / 60;
}
else
borrow = 1 + (incr->second - src->second) / 60;
src->second += borrow * 60;
}
src->second -= incr->second;
if (borrow) {
srcinc.minute = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_MINUTE);
}
break;
case DATETIME_MINUTE:
if (src->minute < incr->minute) {
borrow = 1 + (incr->minute - src->minute - 1) / 60;
src->minute += borrow * 60;
}
src->minute -= incr->minute;
if (borrow) {
srcinc.hour = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_HOUR);
}
break;
case DATETIME_HOUR:
if (src->hour < incr->hour) {
borrow = 1 + (incr->hour - src->hour - 1) / 24;
src->hour += borrow * 24;
}
src->hour -= incr->hour;
if (borrow) {
srcinc.day = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_DAY);
}
break;
case DATETIME_DAY:
if (src->day < incr->day) { /* SIGN CHANGE */
src->day = incr->day - src->day;
datetime_invert_sign(src);
tinc.day = 0;
src->hour = 0;
src->minute = 0;
src->second = 0.0;
datetime_increment(src, &tinc); /* no sign change */
}
else
src->day -= incr->day;
break;
case DATETIME_MONTH:
if (src->month < incr->month) {
borrow = 1 + (incr->month - src->month - 1) / 12;
src->month += borrow * 12;
}
src->month -= incr->month;
if (borrow) {
srcinc.year = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_YEAR);
}
break;
case DATETIME_YEAR:
if (src->year < incr->year) { /* SIGN CHANGE */
src->year = incr->year - src->year;
datetime_invert_sign(src);
tinc.year = 0;
src->month = 0;
datetime_increment(src, &tinc); /* no sign change */
}
else
src->year -= incr->year;
break;
}
}
else if (src->mode == DATETIME_ABSOLUTE) {
DateTime srcinc, tinc, cpsrc;
int i, newdays, borrow = 0;
datetime_copy(&srcinc, incr); /* makes srcinc valid incr */
switch (field) {
case DATETIME_SECOND:
if (src->second < incr->second) {
borrow = 1 + (incr->second - src->second - 1) / 60;
src->second += borrow * 60;
}
src->second -= incr->second;
if (borrow) {
srcinc.minute = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_MINUTE);
}
break;
case DATETIME_MINUTE:
if (src->minute < incr->minute) {
borrow = 1 + (incr->minute - src->minute - 1) / 60;
src->minute += borrow * 60;
}
src->minute -= incr->minute;
if (borrow) {
srcinc.hour = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_HOUR);
}
break;
case DATETIME_HOUR:
if (src->hour < incr->hour) {
borrow = 1 + (incr->hour - src->hour - 1) / 24;
src->hour += borrow * 24;
}
src->hour -= incr->hour;
if (borrow) {
srcinc.day = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_DAY);
}
break;
case DATETIME_DAY:
if (src->day <= incr->day) {
datetime_copy(&cpsrc, src);
datetime_change_from_to(&cpsrc, DATETIME_YEAR, DATETIME_MONTH,
-1);
datetime_set_increment_type(&cpsrc, &tinc);
tinc.month = 1;
newdays = src->day;
while (newdays <= incr->day) {
_datetime_subtract_field(&cpsrc, &tinc, DATETIME_MONTH);
newdays += datetime_days_in_month(cpsrc.year, cpsrc.month,
cpsrc.positive);
borrow++;
}
src->day = newdays;
}
src->day -= incr->day;
if (borrow) {
/*
src->year = cpsrc.year;
src->month = cpsrc.month;
src->positive = cpsrc.positive;
*/
/* check here & below - srcinc may be a day-second interval -
* mess anything up? */
srcinc.month = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_MONTH);
}
break;
case DATETIME_MONTH:
if (src->month <= incr->month) {
borrow = 1 + (incr->month - src->month) / 12;
src->month += borrow * 12;
}
src->month -= incr->month;
if (borrow) {
srcinc.year = borrow;
_datetime_subtract_field(src, &srcinc, DATETIME_YEAR);
}
break;
case DATETIME_YEAR:
if (src->year <= incr->year) { /* SIGN CHANGE */
datetime_set_increment_type(src, &tinc);
tinc.positive = src->positive;
if (datetime_in_interval_year_month(tinc.to)) {
tinc.month = src->month - 1; /* convert to REL */
src->year = incr->year - src->year + 1;
/* +1 to skip 0 */
datetime_invert_sign(src);
tinc.year = 0;
src->month = 1;
datetime_increment(src, &tinc); /* no sign change */
}
else { /* have to convert to days */
tinc.day = src->day - 1; /* convert to REL */
for (i = src->month - 1; i > 0; i--) {
tinc.day +=
datetime_days_in_month(src->year, i, src->positive);
}
tinc.hour = src->hour;
tinc.minute = src->minute;
tinc.second = src->second;
src->year = incr->year - src->year + 1;
/* +1 to skip 0 */
datetime_invert_sign(src);
src->month = 1;
src->day = 1;
src->hour = src->minute = 0;
src->second = 0;
datetime_increment(src, &tinc); /* no sign change */
}
}
else
src->year -= incr->year;
break;
}
}
return 0;
}
/*****************************************************************/
/* When absolute is zero, all fields carry toward the future */
/* When absolute is one, sign of datetime is ignored */
static int _datetime_carry(DateTime *dt, int absolute)
{
int i, carry;
/* normalize day-sec (same for ABSOLUTE & RELATIVE) */
for (i = dt->to; i > dt->from && i > DATETIME_DAY; i--) {
switch (i) {
case DATETIME_SECOND:
if (dt->second >= 60.) {
carry = dt->second / 60.;
dt->minute += carry;
dt->second -= carry * 60;
}
break;
case DATETIME_MINUTE:
if (dt->minute >= 60) {
carry = dt->minute / 60;
dt->hour += carry;
dt->minute -= carry * 60;
}
break;
case DATETIME_HOUR:
if (dt->hour >= 24) {
carry = dt->hour / 24;
dt->day += carry;
dt->hour -= carry * 24;
}
break;
}
}
/* give year a SIGN, temporarily */
if (!absolute && !dt->positive && dt->mode == DATETIME_ABSOLUTE) {
dt->year = -dt->year;
}
if (dt->from == DATETIME_YEAR && dt->to >= DATETIME_MONTH) {
/* normalize yr-mo */
if (dt->mode == DATETIME_ABSOLUTE) {
if (dt->month > 12) { /* month will never be zero */
carry = (dt->month - 1) / 12; /* no carry until 13 */
dt->year += carry;
if (dt->year == 0)
dt->year = 1;
dt->month -= carry * 12;
/*
if(dt->month == 0) dt->month = 1;
shouldn't happen */
}
}
else {
if (dt->month >= 12) {
carry = dt->month / 12;
dt->year += carry;
dt->month -= carry * 12;
}
}
}
/* normalize yr-day */
if (dt->mode == DATETIME_ABSOLUTE && dt->to > DATETIME_MONTH) {
while (dt->day >
datetime_days_in_month(dt->year, dt->month, dt->positive)) {
dt->day -=
datetime_days_in_month(dt->year, dt->month, dt->positive);
if (dt->month == 12) { /* carry to year */
dt->year++;
if (dt->year == 0)
dt->year = 1;
dt->month = 1;
}
else /* no carry to year */
dt->month++;
} /* end while */
} /* end if */
/* undo giving year a SIGN, temporarily */
if (!absolute && dt->mode == DATETIME_ABSOLUTE) {
if (dt->year < 0) {
dt->year = -dt->year;
dt->positive = 0;
}
else
dt->positive = 1;
}
return 0;
}
static int _datetime_add_field(DateTime *src, DateTime *incr, int field)
{
switch (field) {
case DATETIME_SECOND:
src->second += incr->second;
break;
case DATETIME_MINUTE:
src->minute += incr->minute;
break;
case DATETIME_HOUR:
src->hour += incr->hour;
break;
case DATETIME_DAY:
src->day += incr->day;
break;
case DATETIME_MONTH:
src->month += incr->month;
break;
case DATETIME_YEAR:
src->year += incr->year;
break;
}
if (src->mode == DATETIME_RELATIVE)
_datetime_carry(src, 1); /* do carries using absolute values */
else
_datetime_carry(src, 0); /* do carries toward future */
return 0;
}
|