unixtime2tm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2021 Derick Rethans
  5. * Copyright (c) 2021 MongoDB
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "timelib.h"
  26. #include "timelib_private.h"
  27. void timelib_unixtime2date(timelib_sll ts, timelib_sll *y, timelib_sll *m, timelib_sll *d)
  28. {
  29. timelib_sll days, era, t;
  30. timelib_ull day_of_era, year_of_era, day_of_year, month_portion;
  31. /* Calculate days since algorithm's epoch (0000-03-01) */
  32. days = ts / SECS_PER_DAY + HINNANT_EPOCH_SHIFT;
  33. /* Adjustment for a negative time portion */
  34. t = ts % SECS_PER_DAY;
  35. days += (t < 0) ? -1 : 0;
  36. /* Calculate year, month, and day. Algorithm from:
  37. * http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
  38. era = (days >= 0 ? days : days - DAYS_PER_ERA + 1) / DAYS_PER_ERA;
  39. day_of_era = days - era * DAYS_PER_ERA;
  40. year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / DAYS_PER_YEAR;
  41. *y = year_of_era + era * YEARS_PER_ERA;
  42. day_of_year = day_of_era - (DAYS_PER_YEAR * year_of_era + year_of_era / 4 - year_of_era / 100);
  43. month_portion = (5 * day_of_year + 2) / 153;
  44. *d = day_of_year - (153 * month_portion + 2) / 5 + 1;
  45. *m = month_portion + (month_portion < 10 ? 3 : -9);
  46. *y += (*m <= 2);
  47. TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, *y, *m, *d););
  48. }
  49. /* Converts a Unix timestamp value into broken down time, in GMT */
  50. void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts)
  51. {
  52. timelib_sll remainder;
  53. timelib_sll hours, minutes, seconds;
  54. timelib_unixtime2date(ts, &tm->y, &tm->m, &tm->d);
  55. remainder = ts % SECS_PER_DAY;
  56. remainder += (remainder < 0) * SECS_PER_DAY;
  57. /* That was the date, now we do the time */
  58. hours = remainder / 3600;
  59. minutes = (remainder - hours * 3600) / 60;
  60. seconds = remainder % 60;
  61. TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
  62. tm->h = hours;
  63. tm->i = minutes;
  64. tm->s = seconds;
  65. tm->z = 0;
  66. tm->dst = 0;
  67. tm->sse = ts;
  68. tm->sse_uptodate = 1;
  69. tm->tim_uptodate = 1;
  70. tm->is_localtime = 0;
  71. }
  72. void timelib_update_from_sse(timelib_time *tm)
  73. {
  74. timelib_sll sse;
  75. int z = tm->z;
  76. signed int dst = tm->dst;
  77. sse = tm->sse;
  78. switch (tm->zone_type) {
  79. case TIMELIB_ZONETYPE_ABBR:
  80. case TIMELIB_ZONETYPE_OFFSET: {
  81. timelib_unixtime2gmt(tm, tm->sse + tm->z + (tm->dst * 3600));
  82. goto cleanup;
  83. }
  84. case TIMELIB_ZONETYPE_ID: {
  85. timelib_time_offset *gmt_offset;
  86. gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
  87. timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
  88. timelib_time_offset_dtor(gmt_offset);
  89. goto cleanup;
  90. }
  91. default:
  92. timelib_unixtime2gmt(tm, tm->sse);
  93. goto cleanup;
  94. }
  95. cleanup:
  96. tm->sse = sse;
  97. tm->is_localtime = 1;
  98. tm->have_zone = 1;
  99. tm->z = z;
  100. tm->dst = dst;
  101. }
  102. void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
  103. {
  104. timelib_time_offset *gmt_offset;
  105. timelib_tzinfo *tz = tm->tz_info;
  106. switch (tm->zone_type) {
  107. case TIMELIB_ZONETYPE_ABBR:
  108. case TIMELIB_ZONETYPE_OFFSET: {
  109. int z = tm->z;
  110. signed int dst = tm->dst;
  111. timelib_unixtime2gmt(tm, ts + tm->z + (tm->dst * 3600));
  112. tm->sse = ts;
  113. tm->z = z;
  114. tm->dst = dst;
  115. break;
  116. }
  117. case TIMELIB_ZONETYPE_ID:
  118. gmt_offset = timelib_get_time_zone_info(ts, tz);
  119. timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
  120. /* we need to reset the sse here as unixtime2gmt modifies it */
  121. tm->sse = ts;
  122. tm->dst = gmt_offset->is_dst;
  123. tm->z = gmt_offset->offset;
  124. tm->tz_info = tz;
  125. timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
  126. timelib_time_offset_dtor(gmt_offset);
  127. break;
  128. default:
  129. tm->is_localtime = 0;
  130. tm->have_zone = 0;
  131. return;
  132. }
  133. tm->is_localtime = 1;
  134. tm->have_zone = 1;
  135. }
  136. void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
  137. {
  138. if (t->tz_abbr) {
  139. timelib_free(t->tz_abbr);
  140. }
  141. t->tz_abbr = NULL;
  142. t->z = utc_offset;
  143. t->have_zone = 1;
  144. t->zone_type = TIMELIB_ZONETYPE_OFFSET;
  145. t->dst = 0;
  146. t->tz_info = NULL;
  147. }
  148. void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)
  149. {
  150. if (t->tz_abbr) {
  151. timelib_free(t->tz_abbr);
  152. }
  153. t->tz_abbr = timelib_strdup(abbr_info.abbr);
  154. t->z = abbr_info.utc_offset;
  155. t->have_zone = 1;
  156. t->zone_type = TIMELIB_ZONETYPE_ABBR;
  157. t->dst = abbr_info.dst;
  158. t->tz_info = NULL;
  159. }
  160. void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
  161. {
  162. timelib_time_offset *gmt_offset;
  163. gmt_offset = timelib_get_time_zone_info(t->sse, tz);
  164. t->z = gmt_offset->offset;
  165. /*
  166. if (t->dst != gmt_offset->is_dst) {
  167. printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
  168. exit(1);
  169. }
  170. */
  171. t->dst = gmt_offset->is_dst;
  172. t->tz_info = tz;
  173. if (t->tz_abbr) {
  174. timelib_free(t->tz_abbr);
  175. }
  176. t->tz_abbr = timelib_strdup(gmt_offset->abbr);
  177. timelib_time_offset_dtor(gmt_offset);
  178. t->have_zone = 1;
  179. t->zone_type = TIMELIB_ZONETYPE_ID;
  180. }
  181. /* Converts the time stored in the struct to localtime if localtime = true,
  182. * otherwise it converts it to gmttime. This is only done when necessary
  183. * of course. */
  184. int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
  185. {
  186. if (localtime) {
  187. /* Converting from GMT time to local time */
  188. TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
  189. /* Check if TZ is set */
  190. if (!t->tz_info) {
  191. TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
  192. return -1;
  193. }
  194. timelib_unixtime2local(t, t->sse);
  195. } else {
  196. /* Converting from local time to GMT time */
  197. TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
  198. timelib_unixtime2gmt(t, t->sse);
  199. }
  200. return 0;
  201. }