incr1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. static int _datetime_add_field(DateTime *, DateTime *, int);
  9. static int _datetime_subtract_field(DateTime *, DateTime *, int);
  10. /*****************************************************************/
  11. #if 0 /* unused */
  12. static double _debug_decimal(DateTime * dt)
  13. {
  14. double dtdec = 0.0;
  15. if (dt->mode == DATETIME_RELATIVE) {
  16. if (datetime_in_interval_year_month(dt->from)) {
  17. dtdec = dt->year + dt->month / 12.;
  18. }
  19. else {
  20. dtdec = dt->day / 365.25 +
  21. dt->hour / 8766. + dt->minute / 525960.
  22. + dt->second / 31557600.;
  23. }
  24. }
  25. if (dt->positive)
  26. return (dtdec);
  27. return (-dtdec);
  28. }
  29. #endif /* unused */
  30. /*****************************************************************/
  31. /*!
  32. * \brief
  33. *
  34. * This function changes the 'src' date/time data based on the 'incr'
  35. * The type (mode/from/to) of the 'src' can be anything.
  36. * The mode of the 'incr' must be RELATIVE, and the type (mode/from/to) for
  37. * 'incr' must be a valid increment for 'src'. See <b>datetime_is_valid_increment()</b>,
  38. * <b>datetime_check_increment()</b>
  39. * Returns:
  40. * 0: OK
  41. * -1: 'incr' is invalid increment for 'src'
  42. * For src.mode ABSOLUTE,
  43. * <ul>
  44. <li> positive 'incr' moves into the future,
  45. </li>
  46. <li> negative 'incr' moves into the past.
  47. </li>
  48. <li> BC implies the year is negative, but all else is positive. Also, year==0
  49. * is illegal: adding 1 year to 1[bc] gives 1[ad]
  50. </li></ul>
  51. * The 'fracsec' in 'src' is preserved.
  52. * The 'from/to' of the 'src' is preserved.
  53. * A timezone in 'src' is allowed - it's presence is ignored.
  54. * NOTE: There is no datetime_decrement() To decrement, set the 'incr' negative.
  55. *
  56. * \param src
  57. * \param incr
  58. * \return int
  59. */
  60. int datetime_increment(DateTime * src, DateTime * incr)
  61. {
  62. int i, relfrom;
  63. DateTime cpdt, *dt;
  64. if (!datetime_is_valid_increment(src, incr))
  65. return datetime_error_code();
  66. /* special case - incrementing a relative might try to increment
  67. or borrow from a "lower" field than src has,
  68. so we use a copy to change from */
  69. if (src->mode == DATETIME_RELATIVE) {
  70. datetime_copy(&cpdt, src);
  71. relfrom = datetime_in_interval_day_second(src->from)
  72. ? DATETIME_DAY : DATETIME_YEAR;
  73. datetime_change_from_to(&cpdt, relfrom, src->to, -1); /* min. from */
  74. dt = &cpdt;
  75. }
  76. else
  77. dt = src;
  78. /* need to call carry first? (just to make sure?) */
  79. /*
  80. fprintf (stdout,"DEBUG: INCR %.12lf %.12lf = %.12lf\n",
  81. _debug_decimal(dt), _debug_decimal(incr),
  82. _debug_decimal(dt)+_debug_decimal(incr));
  83. */
  84. /* no sign change, just add */
  85. if ((dt->positive && incr->positive) ||
  86. (dt->mode == DATETIME_RELATIVE && !dt->positive && !incr->positive)) {
  87. for (i = incr->to; i >= incr->from; i--) {
  88. _datetime_add_field(dt, incr, i);
  89. }
  90. }
  91. else if (!incr->positive || dt->mode == DATETIME_RELATIVE) {
  92. for (i = incr->to; i >= incr->from; i--) {
  93. _datetime_subtract_field(dt, incr, i);
  94. }
  95. }
  96. /* now only two special cases of bc ABSOLUTE left */
  97. else if (!incr->positive) { /* incr is negative, dt is positive */
  98. for (i = incr->to; i > DATETIME_YEAR; i--) {
  99. _datetime_subtract_field(dt, incr, i);
  100. }
  101. _datetime_add_field(dt, incr, DATETIME_YEAR);
  102. }
  103. else { /* incr is positive, dt is negative */
  104. for (i = incr->to; i > DATETIME_YEAR; i--) {
  105. _datetime_add_field(dt, incr, i);
  106. }
  107. _datetime_subtract_field(dt, incr, DATETIME_YEAR);
  108. }
  109. /*
  110. fprintf (stdout,"DEBUG: INCR RESULT = %.12lf\n", _debug_decimal(dt));
  111. */
  112. if (src->mode == DATETIME_RELATIVE) {
  113. datetime_change_from_to(dt, src->from, src->to, -1);
  114. /* copy dt back into src to return */
  115. datetime_copy(src, dt);
  116. }
  117. return 0;
  118. }
  119. /*****************************************************************/
  120. /*
  121. When calling, the field must be
  122. in the range of src, but this is not enforced here.
  123. The only thing used from the "incr" DateTime is the value of
  124. the field being subtracted and the "from" & "to"
  125. by the time we get here, if src is RELATIVE, src->from should
  126. already be minimized to allow borrowing from "lower" fields
  127. */
  128. static int _datetime_subtract_field(DateTime * src, DateTime * incr,
  129. int field)
  130. {
  131. if (src->mode == DATETIME_RELATIVE) {
  132. DateTime srcinc, tinc;
  133. int borrow = 0;
  134. datetime_copy(&tinc, src);
  135. datetime_copy(&srcinc, incr);
  136. switch (field) {
  137. case DATETIME_SECOND:
  138. /* no "-1" here - remember seconds is floating point */
  139. /* might result in over borrowing, so have to check */
  140. if (src->second < incr->second) {
  141. if ((int)(incr->second - src->second) == (incr->second - src->second)) { /* diff is integer */
  142. borrow = 1 + (incr->second - src->second - 1) / 60;
  143. }
  144. else
  145. borrow = 1 + (incr->second - src->second) / 60;
  146. src->second += borrow * 60;
  147. }
  148. src->second -= incr->second;
  149. if (borrow) {
  150. srcinc.minute = borrow;
  151. _datetime_subtract_field(src, &srcinc, DATETIME_MINUTE);
  152. }
  153. break;
  154. case DATETIME_MINUTE:
  155. if (src->minute < incr->minute) {
  156. borrow = 1 + (incr->minute - src->minute - 1) / 60;
  157. src->minute += borrow * 60;
  158. }
  159. src->minute -= incr->minute;
  160. if (borrow) {
  161. srcinc.hour = borrow;
  162. _datetime_subtract_field(src, &srcinc, DATETIME_HOUR);
  163. }
  164. break;
  165. case DATETIME_HOUR:
  166. if (src->hour < incr->hour) {
  167. borrow = 1 + (incr->hour - src->hour - 1) / 24;
  168. src->hour += borrow * 24;
  169. }
  170. src->hour -= incr->hour;
  171. if (borrow) {
  172. srcinc.day = borrow;
  173. _datetime_subtract_field(src, &srcinc, DATETIME_DAY);
  174. }
  175. break;
  176. case DATETIME_DAY:
  177. if (src->day < incr->day) { /* SIGN CHANGE */
  178. src->day = incr->day - src->day;
  179. datetime_invert_sign(src);
  180. tinc.day = 0;
  181. src->hour = 0;
  182. src->minute = 0;
  183. src->second = 0.0;
  184. datetime_increment(src, &tinc); /* no sign change */
  185. }
  186. else
  187. src->day -= incr->day;
  188. break;
  189. case DATETIME_MONTH:
  190. if (src->month < incr->month) {
  191. borrow = 1 + (incr->month - src->month - 1) / 12;
  192. src->month += borrow * 12;
  193. }
  194. src->month -= incr->month;
  195. if (borrow) {
  196. srcinc.year = borrow;
  197. _datetime_subtract_field(src, &srcinc, DATETIME_YEAR);
  198. }
  199. break;
  200. case DATETIME_YEAR:
  201. if (src->year < incr->year) { /* SIGN CHANGE */
  202. src->year = incr->year - src->year;
  203. datetime_invert_sign(src);
  204. tinc.year = 0;
  205. src->month = 0;
  206. datetime_increment(src, &tinc); /* no sign change */
  207. }
  208. else
  209. src->year -= incr->year;
  210. break;
  211. }
  212. }
  213. else if (src->mode == DATETIME_ABSOLUTE) {
  214. DateTime srcinc, tinc, cpsrc;
  215. int i, newdays, borrow = 0;
  216. datetime_copy(&srcinc, incr); /* makes srcinc valid incr */
  217. switch (field) {
  218. case DATETIME_SECOND:
  219. if (src->second < incr->second) {
  220. borrow = 1 + (incr->second - src->second - 1) / 60;
  221. src->second += borrow * 60;
  222. }
  223. src->second -= incr->second;
  224. if (borrow) {
  225. srcinc.minute = borrow;
  226. _datetime_subtract_field(src, &srcinc, DATETIME_MINUTE);
  227. }
  228. break;
  229. case DATETIME_MINUTE:
  230. if (src->minute < incr->minute) {
  231. borrow = 1 + (incr->minute - src->minute - 1) / 60;
  232. src->minute += borrow * 60;
  233. }
  234. src->minute -= incr->minute;
  235. if (borrow) {
  236. srcinc.hour = borrow;
  237. _datetime_subtract_field(src, &srcinc, DATETIME_HOUR);
  238. }
  239. break;
  240. case DATETIME_HOUR:
  241. if (src->hour < incr->hour) {
  242. borrow = 1 + (incr->hour - src->hour - 1) / 24;
  243. src->hour += borrow * 24;
  244. }
  245. src->hour -= incr->hour;
  246. if (borrow) {
  247. srcinc.day = borrow;
  248. _datetime_subtract_field(src, &srcinc, DATETIME_DAY);
  249. }
  250. break;
  251. case DATETIME_DAY:
  252. if (src->day <= incr->day) {
  253. datetime_copy(&cpsrc, src);
  254. datetime_change_from_to(&cpsrc, DATETIME_YEAR,
  255. DATETIME_MONTH, -1);
  256. datetime_set_increment_type(&cpsrc, &tinc);
  257. tinc.month = 1;
  258. newdays = src->day;
  259. while (newdays <= incr->day) {
  260. _datetime_subtract_field(&cpsrc, &tinc, DATETIME_MONTH);
  261. newdays +=
  262. datetime_days_in_month(cpsrc.year, cpsrc.month,
  263. cpsrc.positive);
  264. borrow++;
  265. }
  266. src->day = newdays;
  267. }
  268. src->day -= incr->day;
  269. if (borrow) {
  270. /*
  271. src->year = cpsrc.year;
  272. src->month = cpsrc.month;
  273. src->positive = cpsrc.positive;
  274. */
  275. /* check here & below - srcinc may be a day-second interval - mess anything up? */
  276. srcinc.month = borrow;
  277. _datetime_subtract_field(src, &srcinc, DATETIME_MONTH);
  278. }
  279. break;
  280. case DATETIME_MONTH:
  281. if (src->month <= incr->month) {
  282. borrow = 1 + (incr->month - src->month) / 12;
  283. src->month += borrow * 12;
  284. }
  285. src->month -= incr->month;
  286. if (borrow) {
  287. srcinc.year = borrow;
  288. _datetime_subtract_field(src, &srcinc, DATETIME_YEAR);
  289. }
  290. break;
  291. case DATETIME_YEAR:
  292. if (src->year <= incr->year) { /* SIGN CHANGE */
  293. datetime_set_increment_type(src, &tinc);
  294. tinc.positive = src->positive;
  295. if (datetime_in_interval_year_month(tinc.to)) {
  296. tinc.month = src->month - 1; /* convert to REL */
  297. src->year = incr->year - src->year + 1;
  298. /* +1 to skip 0 */
  299. datetime_invert_sign(src);
  300. tinc.year = 0;
  301. src->month = 1;
  302. datetime_increment(src, &tinc); /* no sign change */
  303. }
  304. else { /* have to convert to days */
  305. tinc.day = src->day - 1; /* convert to REL */
  306. for (i = src->month - 1; i > 0; i--) {
  307. tinc.day +=
  308. datetime_days_in_month(src->year, i,
  309. src->positive);
  310. }
  311. tinc.hour = src->hour;
  312. tinc.minute = src->minute;
  313. tinc.second = src->second;
  314. src->year = incr->year - src->year + 1;
  315. /* +1 to skip 0 */
  316. datetime_invert_sign(src);
  317. src->month = 1;
  318. src->day = 1;
  319. src->hour = src->minute = 0;
  320. src->second = 0;
  321. datetime_increment(src, &tinc); /* no sign change */
  322. }
  323. }
  324. else
  325. src->year -= incr->year;
  326. break;
  327. }
  328. }
  329. return 0;
  330. }
  331. /*****************************************************************/
  332. /* When absolute is zero, all fields carry toward the future */
  333. /* When absolute is one, sign of datetime is ignored */
  334. static int _datetime_carry(DateTime * dt, int absolute)
  335. {
  336. int i, carry;
  337. /* normalize day-sec (same for ABSOLUTE & RELATIVE) */
  338. for (i = dt->to; i > dt->from && i > DATETIME_DAY; i--) {
  339. switch (i) {
  340. case DATETIME_SECOND:
  341. if (dt->second >= 60.) {
  342. carry = dt->second / 60.;
  343. dt->minute += carry;
  344. dt->second -= carry * 60;
  345. }
  346. break;
  347. case DATETIME_MINUTE:
  348. if (dt->minute >= 60) {
  349. carry = dt->minute / 60;
  350. dt->hour += carry;
  351. dt->minute -= carry * 60;
  352. }
  353. break;
  354. case DATETIME_HOUR:
  355. if (dt->hour >= 24) {
  356. carry = dt->hour / 24;
  357. dt->day += carry;
  358. dt->hour -= carry * 24;
  359. }
  360. break;
  361. }
  362. }
  363. /* give year a SIGN, temporarily */
  364. if (!absolute && !dt->positive && dt->mode == DATETIME_ABSOLUTE) {
  365. dt->year = -dt->year;
  366. }
  367. if (dt->from == DATETIME_YEAR && dt->to >= DATETIME_MONTH) {
  368. /* normalize yr-mo */
  369. if (dt->mode == DATETIME_ABSOLUTE) {
  370. if (dt->month > 12) { /* month will never be zero */
  371. carry = (dt->month - 1) / 12; /* no carry until 13 */
  372. dt->year += carry;
  373. if (dt->year == 0)
  374. dt->year = 1;
  375. dt->month -= carry * 12;
  376. /*
  377. if(dt->month == 0) dt->month = 1;
  378. shouldn't happen */
  379. }
  380. }
  381. else {
  382. if (dt->month >= 12) {
  383. carry = dt->month / 12;
  384. dt->year += carry;
  385. dt->month -= carry * 12;
  386. }
  387. }
  388. }
  389. /* normalize yr-day */
  390. if (dt->mode == DATETIME_ABSOLUTE && dt->to > DATETIME_MONTH) {
  391. while (dt->day >
  392. datetime_days_in_month(dt->year, dt->month, dt->positive)) {
  393. dt->day -=
  394. datetime_days_in_month(dt->year, dt->month, dt->positive);
  395. if (dt->month == 12) { /* carry to year */
  396. dt->year++;
  397. if (dt->year == 0)
  398. dt->year = 1;
  399. dt->month = 1;
  400. }
  401. else /* no carry to year */
  402. dt->month++;
  403. } /* end while */
  404. } /* end if */
  405. /* undo giving year a SIGN, temporarily */
  406. if (!absolute && dt->mode == DATETIME_ABSOLUTE) {
  407. if (dt->year < 0) {
  408. dt->year = -dt->year;
  409. dt->positive = 0;
  410. }
  411. else
  412. dt->positive = 1;
  413. }
  414. return 0;
  415. }
  416. static int _datetime_add_field(DateTime * src, DateTime * incr, int field)
  417. {
  418. switch (field) {
  419. case DATETIME_SECOND:
  420. src->second += incr->second;
  421. break;
  422. case DATETIME_MINUTE:
  423. src->minute += incr->minute;
  424. break;
  425. case DATETIME_HOUR:
  426. src->hour += incr->hour;
  427. break;
  428. case DATETIME_DAY:
  429. src->day += incr->day;
  430. break;
  431. case DATETIME_MONTH:
  432. src->month += incr->month;
  433. break;
  434. case DATETIME_YEAR:
  435. src->year += incr->year;
  436. break;
  437. }
  438. if (src->mode == DATETIME_RELATIVE)
  439. _datetime_carry(src, 1); /* do carries using absolute values */
  440. else
  441. _datetime_carry(src, 0); /* do carries toward future */
  442. return 0;
  443. }