unixtime2tm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. static int month_tab_leap[12] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  27. static int month_tab[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  28. /* Converts a Unix timestamp value into broken down time, in GMT */
  29. void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
  30. {
  31. timelib_sll days, remainder, tmp_days;
  32. timelib_sll cur_year = 1970;
  33. timelib_sll i;
  34. timelib_sll hours, minutes, seconds;
  35. int *months;
  36. days = ts / SECS_PER_DAY;
  37. remainder = ts - (days * SECS_PER_DAY);
  38. if (ts < 0 && remainder == 0) {
  39. days++;
  40. remainder -= SECS_PER_DAY;
  41. }
  42. TIMELIB_DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););
  43. if (ts >= 0) {
  44. tmp_days = days + 1;
  45. } else {
  46. tmp_days = days;
  47. }
  48. if (tmp_days > DAYS_PER_LYEAR_PERIOD || tmp_days <= -DAYS_PER_LYEAR_PERIOD) {
  49. cur_year += YEARS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
  50. tmp_days -= DAYS_PER_LYEAR_PERIOD * (tmp_days / DAYS_PER_LYEAR_PERIOD);
  51. }
  52. TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
  53. if (ts >= 0) {
  54. while (tmp_days >= DAYS_PER_LYEAR) {
  55. cur_year++;
  56. if (timelib_is_leap(cur_year)) {
  57. tmp_days -= DAYS_PER_LYEAR;
  58. } else {
  59. tmp_days -= DAYS_PER_YEAR;
  60. }
  61. TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
  62. }
  63. } else {
  64. while (tmp_days <= 0) {
  65. cur_year--;
  66. if (timelib_is_leap(cur_year)) {
  67. tmp_days += DAYS_PER_LYEAR;
  68. } else {
  69. tmp_days += DAYS_PER_YEAR;
  70. }
  71. TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
  72. }
  73. remainder += SECS_PER_DAY;
  74. }
  75. TIMELIB_DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););
  76. months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;
  77. if (timelib_is_leap(cur_year) && cur_year < 1970) {
  78. tmp_days--;
  79. }
  80. i = 11;
  81. while (i > 0) {
  82. TIMELIB_DEBUG(printf("month=%lld (%d)\n", i, months[i]););
  83. if (tmp_days > months[i]) {
  84. break;
  85. }
  86. i--;
  87. }
  88. TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););
  89. /* That was the date, now we do the time */
  90. hours = remainder / 3600;
  91. minutes = (remainder - hours * 3600) / 60;
  92. seconds = remainder % 60;
  93. TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
  94. tm->y = cur_year;
  95. tm->m = i + 1;
  96. tm->d = tmp_days - months[i];
  97. tm->h = hours;
  98. tm->i = minutes;
  99. tm->s = seconds;
  100. tm->z = 0;
  101. tm->dst = 0;
  102. tm->sse = ts;
  103. tm->sse_uptodate = 1;
  104. tm->tim_uptodate = 1;
  105. tm->is_localtime = 0;
  106. }
  107. void timelib_update_from_sse(timelib_time *tm)
  108. {
  109. timelib_sll sse;
  110. int z = tm->z;
  111. signed int dst = tm->dst;
  112. sse = tm->sse;
  113. switch (tm->zone_type) {
  114. case TIMELIB_ZONETYPE_ABBR:
  115. case TIMELIB_ZONETYPE_OFFSET: {
  116. timelib_unixtime2gmt(tm, tm->sse + tm->z + (tm->dst * 3600));
  117. goto cleanup;
  118. }
  119. case TIMELIB_ZONETYPE_ID: {
  120. timelib_time_offset *gmt_offset;
  121. gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
  122. timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
  123. timelib_time_offset_dtor(gmt_offset);
  124. goto cleanup;
  125. }
  126. default:
  127. timelib_unixtime2gmt(tm, tm->sse);
  128. goto cleanup;
  129. }
  130. cleanup:
  131. tm->sse = sse;
  132. tm->is_localtime = 1;
  133. tm->have_zone = 1;
  134. tm->z = z;
  135. tm->dst = dst;
  136. }
  137. void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
  138. {
  139. timelib_time_offset *gmt_offset;
  140. timelib_tzinfo *tz = tm->tz_info;
  141. switch (tm->zone_type) {
  142. case TIMELIB_ZONETYPE_ABBR:
  143. case TIMELIB_ZONETYPE_OFFSET: {
  144. int z = tm->z;
  145. signed int dst = tm->dst;
  146. timelib_unixtime2gmt(tm, ts + tm->z + (tm->dst * 3600));
  147. tm->sse = ts;
  148. tm->z = z;
  149. tm->dst = dst;
  150. break;
  151. }
  152. case TIMELIB_ZONETYPE_ID:
  153. gmt_offset = timelib_get_time_zone_info(ts, tz);
  154. timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
  155. /* we need to reset the sse here as unixtime2gmt modifies it */
  156. tm->sse = ts;
  157. tm->dst = gmt_offset->is_dst;
  158. tm->z = gmt_offset->offset;
  159. tm->tz_info = tz;
  160. timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
  161. timelib_time_offset_dtor(gmt_offset);
  162. break;
  163. default:
  164. tm->is_localtime = 0;
  165. tm->have_zone = 0;
  166. return;
  167. }
  168. tm->is_localtime = 1;
  169. tm->have_zone = 1;
  170. }
  171. void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
  172. {
  173. if (t->tz_abbr) {
  174. timelib_free(t->tz_abbr);
  175. }
  176. t->tz_abbr = NULL;
  177. t->z = utc_offset;
  178. t->have_zone = 1;
  179. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  180. t->dst = 0;
  181. t->tz_info = NULL;
  182. }
  183. void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
  184. {
  185. if (t->tz_abbr) {
  186. timelib_free(t->tz_abbr);
  187. }
  188. t->tz_abbr = timelib_strdup(abbr_info.abbr);
  189. t->z = abbr_info.utc_offset;
  190. t->have_zone = 1;
  191. t->zone_type = TIMELIB_ZONETYPE_ABBR;
  192. t->dst = abbr_info.dst;
  193. t->tz_info = NULL;
  194. }
  195. void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
  196. {
  197. timelib_time_offset *gmt_offset;
  198. gmt_offset = timelib_get_time_zone_info(t->sse, tz);
  199. t->z = gmt_offset->offset;
  200. /*
  201. if (t->dst != gmt_offset->is_dst) {
  202. printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
  203. exit(1);
  204. }
  205. */
  206. t->dst = gmt_offset->is_dst;
  207. t->tz_info = tz;
  208. if (t->tz_abbr) {
  209. timelib_free(t->tz_abbr);
  210. }
  211. t->tz_abbr = timelib_strdup(gmt_offset->abbr);
  212. timelib_time_offset_dtor(gmt_offset);
  213. t->have_zone = 1;
  214. t->zone_type = TIMELIB_ZONETYPE_ID;
  215. }
  216. /* Converts the time stored in the struct to localtime if localtime = true,
  217. * otherwise it converts it to gmttime. This is only done when necessary
  218. * of course. */
  219. int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
  220. {
  221. if (localtime) {
  222. /* Converting from GMT time to local time */
  223. TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
  224. /* Check if TZ is set */
  225. if (!t->tz_info) {
  226. TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
  227. return -1;
  228. }
  229. timelib_unixtime2local(t, t->sse);
  230. } else {
  231. /* Converting from local time to GMT time */
  232. TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
  233. timelib_unixtime2gmt(t, t->sse);
  234. }
  235. return 0;
  236. }