interval.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
  28. {
  29. timelib_rel_time *rt;
  30. timelib_time *swp;
  31. timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
  32. timelib_time one_backup, two_backup;
  33. rt = timelib_rel_time_ctor();
  34. rt->invert = 0;
  35. if (
  36. (one->sse > two->sse) ||
  37. (one->sse == two->sse && one->us > two->us)
  38. ) {
  39. swp = two;
  40. two = one;
  41. one = swp;
  42. rt->invert = 1;
  43. }
  44. /* Calculate correction for DST change over, but only if the TZ type is ID
  45. * and it's the same */
  46. if (one->zone_type == 3 && two->zone_type == 3
  47. && (strcmp(one->tz_info->name, two->tz_info->name) == 0)
  48. && (one->z != two->z))
  49. {
  50. dst_corr = two->z - one->z;
  51. dst_h_corr = dst_corr / 3600;
  52. dst_m_corr = (dst_corr % 3600) / 60;
  53. }
  54. /* Save old TZ info */
  55. memcpy(&one_backup, one, sizeof(one_backup));
  56. memcpy(&two_backup, two, sizeof(two_backup));
  57. timelib_apply_localtime(one, 0);
  58. timelib_apply_localtime(two, 0);
  59. rt->y = two->y - one->y;
  60. rt->m = two->m - one->m;
  61. rt->d = two->d - one->d;
  62. rt->h = two->h - one->h;
  63. rt->i = two->i - one->i;
  64. rt->s = two->s - one->s;
  65. rt->us = two->us - one->us;
  66. if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
  67. rt->h += dst_h_corr;
  68. rt->i += dst_m_corr;
  69. }
  70. rt->days = fabs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
  71. timelib_do_rel_normalize(rt->invert ? one : two, rt);
  72. /* We need to do this after normalisation otherwise we can't get "24H" */
  73. if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
  74. if (two->sse < one->sse + 86400 - dst_corr) {
  75. rt->d--;
  76. rt->h = 24;
  77. } else {
  78. rt->h += dst_h_corr;
  79. rt->i += dst_m_corr;
  80. }
  81. }
  82. /* Restore old TZ info */
  83. memcpy(one, &one_backup, sizeof(one_backup));
  84. memcpy(two, &two_backup, sizeof(two_backup));
  85. return rt;
  86. }
  87. timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
  88. {
  89. int bias = 1;
  90. timelib_time *t = timelib_time_clone(old_time);
  91. if (interval->have_weekday_relative || interval->have_special_relative) {
  92. memcpy(&t->relative, interval, sizeof(timelib_rel_time));
  93. } else {
  94. if (interval->invert) {
  95. bias = -1;
  96. }
  97. memset(&t->relative, 0, sizeof(timelib_rel_time));
  98. t->relative.y = interval->y * bias;
  99. t->relative.m = interval->m * bias;
  100. t->relative.d = interval->d * bias;
  101. t->relative.h = interval->h * bias;
  102. t->relative.i = interval->i * bias;
  103. t->relative.s = interval->s * bias;
  104. t->relative.us = interval->us * bias;
  105. }
  106. t->have_relative = 1;
  107. t->sse_uptodate = 0;
  108. timelib_update_ts(t, NULL);
  109. // printf("%lld %lld %d\n", old_time->dst, t->dst, (t->sse - old_time->sse));
  110. /* Adjust for backwards DST changeover */
  111. if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
  112. t->sse -= old_time->z;
  113. t->sse += t->z;
  114. }
  115. timelib_update_from_sse(t);
  116. t->have_relative = 0;
  117. return t;
  118. }
  119. timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
  120. {
  121. int bias = 1;
  122. timelib_time *t = timelib_time_clone(old_time);
  123. if (interval->invert) {
  124. bias = -1;
  125. }
  126. memset(&t->relative, 0, sizeof(timelib_rel_time));
  127. t->relative.y = 0 - (interval->y * bias);
  128. t->relative.m = 0 - (interval->m * bias);
  129. t->relative.d = 0 - (interval->d * bias);
  130. t->relative.h = 0 - (interval->h * bias);
  131. t->relative.i = 0 - (interval->i * bias);
  132. t->relative.s = 0 - (interval->s * bias);
  133. t->relative.us = 0 - (interval->us * bias);
  134. t->have_relative = 1;
  135. t->sse_uptodate = 0;
  136. timelib_update_ts(t, NULL);
  137. /* Adjust for backwards DST changeover */
  138. if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
  139. t->sse -= old_time->z;
  140. t->sse += t->z;
  141. }
  142. /* Adjust for forwards DST changeover */
  143. if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
  144. t->sse -= old_time->z;
  145. t->sse += t->z;
  146. }
  147. timelib_update_from_sse(t);
  148. t->have_relative = 0;
  149. return t;
  150. }