interval.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2019 Derick Rethans
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "timelib.h"
  25. #include "timelib_private.h"
  26. #include <math.h>
  27. static void swap_times(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
  28. {
  29. timelib_time *swp;
  30. swp = *two;
  31. *two = *one;
  32. *one = swp;
  33. rt->invert = 1;
  34. }
  35. static void swap_if_negative(timelib_rel_time *rt)
  36. {
  37. if (rt->y == 0 && rt->m == 0 && rt->d == 0 && rt->h == 0 && rt->i == 0 && rt->s == 0 && rt->us == 0) {
  38. return;
  39. }
  40. if (rt->y >= 0 && rt->m >= 0 && rt->d >= 0 && rt->h >= 0 && rt->i >= 0 && rt->s >= 0 && rt->us >= 0) {
  41. return;
  42. }
  43. rt->invert = 1 - rt->invert;
  44. rt->y = 0 - rt->y;
  45. rt->m = 0 - rt->m;
  46. rt->d = 0 - rt->d;
  47. rt->h = 0 - rt->h;
  48. rt->i = 0 - rt->i;
  49. rt->s = 0 - rt->s;
  50. rt->us = 0 - rt->us;
  51. }
  52. static void sort_old_to_new(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
  53. {
  54. /* Check whether date/times need to be inverted. If both times are
  55. * TIMELIB_ZONETYPE_ID times with the same TZID, we use the y-s + us fields. */
  56. if (
  57. (*one)->zone_type == TIMELIB_ZONETYPE_ID &&
  58. (*two)->zone_type == TIMELIB_ZONETYPE_ID &&
  59. (strcmp((*one)->tz_info->name, (*two)->tz_info->name) != 0)
  60. ) {
  61. if (
  62. ((*one)->y > (*two)->y) ||
  63. ((*one)->y == (*two)->y && (*one)->m > (*two)->m) ||
  64. ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d > (*two)->d) ||
  65. ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h > (*two)->h) ||
  66. ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i > (*two)->i) ||
  67. ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s > (*two)->s) ||
  68. ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s == (*two)->s && (*one)->us > (*two)->us)
  69. ) {
  70. swap_times(one, two, rt);
  71. }
  72. return;
  73. }
  74. /* Fall back to using the SSE instead to rearrange */
  75. if (
  76. ((*one)->sse > (*two)->sse) ||
  77. ((*one)->sse == (*two)->sse && (*one)->us > (*two)->us)
  78. ) {
  79. swap_times(one, two, rt);
  80. }
  81. }
  82. static timelib_rel_time *timelib_diff_with_tzid(timelib_time *one, timelib_time *two)
  83. {
  84. timelib_rel_time *rt;
  85. timelib_sll dst_corr = 0, dst_h_corr = 0, dst_m_corr = 0;
  86. timelib_time_offset *trans = NULL;
  87. rt = timelib_rel_time_ctor();
  88. rt->invert = 0;
  89. sort_old_to_new(&one, &two, rt);
  90. /* Calculate correction for UTC offset changes between first and second SSE */
  91. dst_corr = two->z - one->z;
  92. dst_h_corr = dst_corr / 3600;
  93. dst_m_corr = (dst_corr % 3600) / 60;
  94. rt->y = two->y - one->y;
  95. rt->m = two->m - one->m;
  96. rt->d = two->d - one->d;
  97. rt->h = two->h - one->h;
  98. rt->i = two->i - one->i;
  99. rt->s = two->s - one->s;
  100. rt->us = two->us - one->us;
  101. rt->days = timelib_diff_days(one, two);
  102. /* Fall Back: Cater for transition period, where rt->invert is 0, but there are negative numbers */
  103. if (one->dst == 1 && two->dst == 0) {
  104. /* First for two "Type 3" times */
  105. if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID) {
  106. trans = timelib_get_time_zone_info(two->sse, two->tz_info);
  107. if (trans) {
  108. if (one->sse < trans->transition_time && one->sse >= trans->transition_time + dst_corr) {
  109. timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
  110. rt->h = flipped / SECS_PER_HOUR;
  111. rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
  112. rt->s = flipped % 60;
  113. }
  114. timelib_time_offset_dtor(trans);
  115. trans = NULL;
  116. }
  117. } else if (rt->h == 0 && (rt->i < 0 || rt->s < 0)) {
  118. /* Then for all the others */
  119. timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
  120. rt->h = flipped / SECS_PER_HOUR;
  121. rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
  122. rt->s = flipped % 60;
  123. dst_corr += SECS_PER_HOUR;
  124. dst_h_corr++;
  125. }
  126. }
  127. timelib_do_rel_normalize(rt->invert ? one : two, rt);
  128. /* Do corrections for "Type 3" times with the same TZID */
  129. if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID && strcmp(one->tz_info->name, two->tz_info->name) == 0) {
  130. if (one->dst == 1 && two->dst == 0) { /* Fall Back */
  131. if (two->tz_info) {
  132. trans = timelib_get_time_zone_info(two->sse, two->tz_info);
  133. if (
  134. trans &&
  135. two->sse >= trans->transition_time &&
  136. ((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
  137. ) {
  138. rt->h -= dst_h_corr;
  139. rt->i -= dst_m_corr;
  140. }
  141. }
  142. } else if (one->dst == 0 && two->dst == 1) { /* Spring Forward */
  143. if (two->tz_info) {
  144. trans = timelib_get_time_zone_info(two->sse, two->tz_info);
  145. if (
  146. trans &&
  147. !((one->sse + SECS_PER_DAY > trans->transition_time) && (one->sse + SECS_PER_DAY <= (trans->transition_time + dst_corr))) &&
  148. two->sse >= trans->transition_time &&
  149. ((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
  150. ) {
  151. rt->h -= dst_h_corr;
  152. rt->i -= dst_m_corr;
  153. }
  154. }
  155. } else if (two->sse - one->sse >= SECS_PER_DAY) {
  156. /* Check whether we're in the period to the next transition time */
  157. trans = timelib_get_time_zone_info(two->sse - two->z, two->tz_info);
  158. dst_corr = one->z - trans->offset;
  159. if (two->sse >= trans->transition_time - dst_corr && two->sse < trans->transition_time) {
  160. rt->d--;
  161. rt->h = 24;
  162. }
  163. }
  164. } else {
  165. rt->h -= dst_h_corr;
  166. rt->i -= dst_m_corr;
  167. swap_if_negative(rt);
  168. timelib_do_rel_normalize(rt->invert ? one : two, rt);
  169. }
  170. if (trans) {
  171. timelib_time_offset_dtor(trans);
  172. }
  173. return rt;
  174. }
  175. timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
  176. {
  177. timelib_rel_time *rt;
  178. if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID) {
  179. return timelib_diff_with_tzid(one, two);
  180. }
  181. rt = timelib_rel_time_ctor();
  182. rt->invert = 0;
  183. sort_old_to_new(&one, &two, rt);
  184. rt->y = two->y - one->y;
  185. rt->m = two->m - one->m;
  186. rt->d = two->d - one->d;
  187. rt->h = two->h - one->h;
  188. if (one->zone_type != TIMELIB_ZONETYPE_ID) {
  189. rt->h = rt->h + one->dst;
  190. }
  191. if (two->zone_type != TIMELIB_ZONETYPE_ID) {
  192. rt->h = rt->h - two->dst;
  193. }
  194. rt->i = two->i - one->i;
  195. rt->s = two->s - one->s - two->z + one->z;
  196. rt->us = two->us - one->us;
  197. rt->days = timelib_diff_days(one, two);
  198. timelib_do_rel_normalize(rt->invert ? one : two, rt);
  199. return rt;
  200. }
  201. int timelib_diff_days(timelib_time *one, timelib_time *two)
  202. {
  203. int days = 0;
  204. if (timelib_same_timezone(one, two)) {
  205. timelib_time *earliest, *latest;
  206. double earliest_time, latest_time;
  207. if (timelib_time_compare(one, two) < 0) {
  208. earliest = one;
  209. latest = two;
  210. } else {
  211. earliest = two;
  212. latest = one;
  213. }
  214. timelib_hmsf_to_decimal_hour(earliest->h, earliest->i, earliest->s, earliest->us, &earliest_time);
  215. timelib_hmsf_to_decimal_hour(latest->h, latest->i, latest->s, latest->us, &latest_time);
  216. days = llabs(timelib_epoch_days_from_time(one) - timelib_epoch_days_from_time(two));
  217. if (latest_time < earliest_time && days > 0) {
  218. days--;
  219. }
  220. } else {
  221. days = fabs(floor(one->sse - two->sse) / 86400);
  222. }
  223. return days;
  224. }
  225. timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
  226. {
  227. int bias = 1;
  228. timelib_time *t = timelib_time_clone(old_time);
  229. if (interval->have_weekday_relative || interval->have_special_relative) {
  230. memcpy(&t->relative, interval, sizeof(timelib_rel_time));
  231. } else {
  232. if (interval->invert) {
  233. bias = -1;
  234. }
  235. memset(&t->relative, 0, sizeof(timelib_rel_time));
  236. t->relative.y = interval->y * bias;
  237. t->relative.m = interval->m * bias;
  238. t->relative.d = interval->d * bias;
  239. t->relative.h = interval->h * bias;
  240. t->relative.i = interval->i * bias;
  241. t->relative.s = interval->s * bias;
  242. t->relative.us = interval->us * bias;
  243. }
  244. t->have_relative = 1;
  245. t->sse_uptodate = 0;
  246. timelib_update_ts(t, NULL);
  247. timelib_update_from_sse(t);
  248. t->have_relative = 0;
  249. return t;
  250. }
  251. timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
  252. {
  253. int bias = 1;
  254. timelib_time *t = timelib_time_clone(old_time);
  255. if (interval->invert) {
  256. bias = -1;
  257. }
  258. memset(&t->relative, 0, sizeof(timelib_rel_time));
  259. t->relative.y = 0 - (interval->y * bias);
  260. t->relative.m = 0 - (interval->m * bias);
  261. t->relative.d = 0 - (interval->d * bias);
  262. t->relative.h = 0 - (interval->h * bias);
  263. t->relative.i = 0 - (interval->i * bias);
  264. t->relative.s = 0 - (interval->s * bias);
  265. t->relative.us = 0 - (interval->us * bias);
  266. t->have_relative = 1;
  267. t->sse_uptodate = 0;
  268. timelib_update_ts(t, NULL);
  269. timelib_update_from_sse(t);
  270. t->have_relative = 0;
  271. return t;
  272. }
  273. static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
  274. {
  275. if (*a < start) {
  276. *b -= (start - *a - 1) / adj + 1;
  277. *a += adj * ((start - *a - 1) / adj + 1);
  278. }
  279. if (*a >= end) {
  280. *b += *a / adj;
  281. *a -= adj * (*a / adj);
  282. }
  283. }
  284. timelib_time *timelib_add_wall(timelib_time *old_time, timelib_rel_time *interval)
  285. {
  286. int bias = 1;
  287. timelib_time *t = timelib_time_clone(old_time);
  288. t->have_relative = 1;
  289. t->sse_uptodate = 0;
  290. if (interval->have_weekday_relative || interval->have_special_relative) {
  291. memcpy(&t->relative, interval, sizeof(timelib_rel_time));
  292. timelib_update_ts(t, NULL);
  293. timelib_update_from_sse(t);
  294. } else {
  295. if (interval->invert) {
  296. bias = -1;
  297. }
  298. memset(&t->relative, 0, sizeof(timelib_rel_time));
  299. t->relative.y = interval->y * bias;
  300. t->relative.m = interval->m * bias;
  301. t->relative.d = interval->d * bias;
  302. if (t->relative.y || t->relative.m || t->relative.d) {
  303. timelib_update_ts(t, NULL);
  304. }
  305. if (interval->us == 0) {
  306. t->sse += bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
  307. timelib_update_from_sse(t);
  308. } else {
  309. timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
  310. do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
  311. t->sse += bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
  312. timelib_update_from_sse(t);
  313. t->us += temp_interval->us * bias;
  314. timelib_do_normalize(t);
  315. timelib_update_ts(t, NULL);
  316. timelib_rel_time_dtor(temp_interval);
  317. }
  318. timelib_do_normalize(t);
  319. }
  320. if (t->zone_type == TIMELIB_ZONETYPE_ID) {
  321. timelib_set_timezone(t, t->tz_info);
  322. }
  323. t->have_relative = 0;
  324. return t;
  325. }
  326. timelib_time *timelib_sub_wall(timelib_time *old_time, timelib_rel_time *interval)
  327. {
  328. int bias = 1;
  329. timelib_time *t = timelib_time_clone(old_time);
  330. t->have_relative = 1;
  331. t->sse_uptodate = 0;
  332. if (interval->have_weekday_relative || interval->have_special_relative) {
  333. memcpy(&t->relative, interval, sizeof(timelib_rel_time));
  334. timelib_update_ts(t, NULL);
  335. timelib_update_from_sse(t);
  336. } else {
  337. if (interval->invert) {
  338. bias = -1;
  339. }
  340. memset(&t->relative, 0, sizeof(timelib_rel_time));
  341. t->relative.y = 0 - (interval->y * bias);
  342. t->relative.m = 0 - (interval->m * bias);
  343. t->relative.d = 0 - (interval->d * bias);
  344. if (t->relative.y || t->relative.m || t->relative.d) {
  345. timelib_update_ts(t, NULL);
  346. }
  347. if (interval->us == 0) {
  348. t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
  349. timelib_update_from_sse(t);
  350. } else {
  351. timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
  352. do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
  353. t->sse -= bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
  354. timelib_update_from_sse(t);
  355. t->us -= temp_interval->us * bias;
  356. timelib_do_normalize(t);
  357. timelib_update_ts(t, NULL);
  358. timelib_rel_time_dtor(temp_interval);
  359. }
  360. timelib_do_normalize(t);
  361. }
  362. if (t->zone_type == TIMELIB_ZONETYPE_ID) {
  363. timelib_set_timezone(t, t->tz_info);
  364. }
  365. t->have_relative = 0;
  366. return t;
  367. }