unixtime2tm.c 7.5 KB

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