timelib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <ctype.h>
  26. #include <math.h>
  27. #define TIMELIB_TIME_FREE(m) \
  28. if (m) { \
  29. timelib_free(m); \
  30. m = NULL; \
  31. } \
  32. #define TIMELIB_LLABS(y) (y < 0 ? (y * -1) : y)
  33. #define HOUR(a) (int)(a * 60)
  34. timelib_time* timelib_time_ctor(void)
  35. {
  36. timelib_time *t;
  37. t = timelib_calloc(1, sizeof(timelib_time));
  38. return t;
  39. }
  40. timelib_rel_time* timelib_rel_time_ctor(void)
  41. {
  42. timelib_rel_time *t;
  43. t = timelib_calloc(1, sizeof(timelib_rel_time));
  44. return t;
  45. }
  46. timelib_time* timelib_time_clone(timelib_time *orig)
  47. {
  48. timelib_time *tmp = timelib_time_ctor();
  49. memcpy(tmp, orig, sizeof(timelib_time));
  50. if (orig->tz_abbr) {
  51. tmp->tz_abbr = timelib_strdup(orig->tz_abbr);
  52. }
  53. if (orig->tz_info) {
  54. tmp->tz_info = orig->tz_info;
  55. }
  56. return tmp;
  57. }
  58. int timelib_time_compare(timelib_time *t1, timelib_time *t2)
  59. {
  60. if (t1->sse == t2->sse) {
  61. if (t1->f == t2->f) {
  62. return 0;
  63. }
  64. if (t1->sse < 0) {
  65. return (t1->f < t2->f) ? 1 : -1;
  66. } else {
  67. return (t1->f < t2->f) ? -1 : 1;
  68. }
  69. }
  70. return (t1->sse < t2->sse) ? -1 : 1;
  71. }
  72. timelib_rel_time* timelib_rel_time_clone(timelib_rel_time *rel)
  73. {
  74. timelib_rel_time *tmp = timelib_rel_time_ctor();
  75. memcpy(tmp, rel, sizeof(timelib_rel_time));
  76. return tmp;
  77. }
  78. void timelib_time_tz_abbr_update(timelib_time* tm, char* tz_abbr)
  79. {
  80. unsigned int i;
  81. size_t tz_abbr_len = strlen(tz_abbr);
  82. TIMELIB_TIME_FREE(tm->tz_abbr);
  83. tm->tz_abbr = timelib_strdup(tz_abbr);
  84. for (i = 0; i < tz_abbr_len; i++) {
  85. tm->tz_abbr[i] = toupper(tz_abbr[i]);
  86. }
  87. }
  88. void timelib_time_dtor(timelib_time* t)
  89. {
  90. TIMELIB_TIME_FREE(t->tz_abbr);
  91. TIMELIB_TIME_FREE(t);
  92. }
  93. void timelib_rel_time_dtor(timelib_rel_time* t)
  94. {
  95. TIMELIB_TIME_FREE(t);
  96. }
  97. timelib_time_offset* timelib_time_offset_ctor(void)
  98. {
  99. timelib_time_offset *t;
  100. t = timelib_calloc(1, sizeof(timelib_time_offset));
  101. return t;
  102. }
  103. void timelib_time_offset_dtor(timelib_time_offset* t)
  104. {
  105. TIMELIB_TIME_FREE(t->abbr);
  106. TIMELIB_TIME_FREE(t);
  107. }
  108. timelib_tzinfo* timelib_tzinfo_ctor(char *name)
  109. {
  110. timelib_tzinfo *t;
  111. t = timelib_calloc(1, sizeof(timelib_tzinfo));
  112. t->name = timelib_strdup(name);
  113. return t;
  114. }
  115. timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
  116. {
  117. timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
  118. tmp->bit32.ttisgmtcnt = tz->bit32.ttisgmtcnt;
  119. tmp->bit32.ttisstdcnt = tz->bit32.ttisstdcnt;
  120. tmp->bit32.leapcnt = tz->bit32.leapcnt;
  121. tmp->bit32.timecnt = tz->bit32.timecnt;
  122. tmp->bit32.typecnt = tz->bit32.typecnt;
  123. tmp->bit32.charcnt = tz->bit32.charcnt;
  124. tmp->trans = (int32_t *) timelib_malloc(tz->bit32.timecnt * sizeof(int32_t));
  125. tmp->trans_idx = (unsigned char*) timelib_malloc(tz->bit32.timecnt * sizeof(unsigned char));
  126. memcpy(tmp->trans, tz->trans, tz->bit32.timecnt * sizeof(int32_t));
  127. memcpy(tmp->trans_idx, tz->trans_idx, tz->bit32.timecnt * sizeof(unsigned char));
  128. tmp->type = (ttinfo*) timelib_malloc(tz->bit32.typecnt * sizeof(struct ttinfo));
  129. memcpy(tmp->type, tz->type, tz->bit32.typecnt * sizeof(struct ttinfo));
  130. tmp->timezone_abbr = (char*) timelib_malloc(tz->bit32.charcnt);
  131. memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->bit32.charcnt);
  132. tmp->leap_times = (tlinfo*) timelib_malloc(tz->bit32.leapcnt * sizeof(tlinfo));
  133. memcpy(tmp->leap_times, tz->leap_times, tz->bit32.leapcnt * sizeof(tlinfo));
  134. return tmp;
  135. }
  136. void timelib_tzinfo_dtor(timelib_tzinfo *tz)
  137. {
  138. TIMELIB_TIME_FREE(tz->name);
  139. TIMELIB_TIME_FREE(tz->trans);
  140. TIMELIB_TIME_FREE(tz->trans_idx);
  141. TIMELIB_TIME_FREE(tz->type);
  142. TIMELIB_TIME_FREE(tz->timezone_abbr);
  143. TIMELIB_TIME_FREE(tz->leap_times);
  144. TIMELIB_TIME_FREE(tz->location.comments);
  145. TIMELIB_TIME_FREE(tz);
  146. tz = NULL;
  147. }
  148. char *timelib_get_tz_abbr_ptr(timelib_time *t)
  149. {
  150. if (!t->sse_uptodate) {
  151. timelib_update_ts(t, NULL);
  152. };
  153. return t->tz_abbr;
  154. }
  155. void timelib_error_container_dtor(timelib_error_container *errors)
  156. {
  157. int i;
  158. for (i = 0; i < errors->warning_count; i++) {
  159. timelib_free(errors->warning_messages[i].message);
  160. }
  161. timelib_free(errors->warning_messages);
  162. for (i = 0; i < errors->error_count; i++) {
  163. timelib_free(errors->error_messages[i].message);
  164. }
  165. timelib_free(errors->error_messages);
  166. timelib_free(errors);
  167. }
  168. timelib_long timelib_date_to_int(timelib_time *d, int *error)
  169. {
  170. timelib_sll ts;
  171. ts = d->sse;
  172. if (ts < TIMELIB_LONG_MIN || ts > TIMELIB_LONG_MAX) {
  173. if (error) {
  174. *error = 1;
  175. }
  176. return 0;
  177. }
  178. if (error) {
  179. *error = 0;
  180. }
  181. return (timelib_long) d->sse;
  182. }
  183. void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)
  184. {
  185. *hour = floor(h);
  186. *min = floor((h - *hour) * 60);
  187. *sec = (h - *hour - ((float) *min / 60)) * 3600;
  188. }
  189. void timelib_dump_date(timelib_time *d, int options)
  190. {
  191. if ((options & 2) == 2) {
  192. printf("TYPE: %d ", d->zone_type);
  193. }
  194. printf("TS: %lld | %s%04lld-%02lld-%02lld %02lld:%02lld:%02lld",
  195. d->sse, d->y < 0 ? "-" : "", TIMELIB_LLABS(d->y), d->m, d->d, d->h, d->i, d->s);
  196. if (d->f > +0.0) {
  197. printf(" %.5f", d->f);
  198. }
  199. if (d->is_localtime) {
  200. switch (d->zone_type) {
  201. case TIMELIB_ZONETYPE_OFFSET: /* Only offset */
  202. printf(" GMT %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
  203. break;
  204. case TIMELIB_ZONETYPE_ID: /* Timezone struct */
  205. /* Show abbreviation if wanted */
  206. if (d->tz_abbr) {
  207. printf(" %s", d->tz_abbr);
  208. }
  209. /* Do we have a TimeZone struct? */
  210. if (d->tz_info) {
  211. printf(" %s", d->tz_info->name);
  212. }
  213. break;
  214. case TIMELIB_ZONETYPE_ABBR:
  215. printf(" %s", d->tz_abbr);
  216. printf(" %05d%s", d->z, d->dst == 1 ? " (DST)" : "");
  217. break;
  218. }
  219. }
  220. if ((options & 1) == 1) {
  221. if (d->have_relative) {
  222. printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS",
  223. d->relative.y, d->relative.m, d->relative.d, d->relative.h, d->relative.i, d->relative.s);
  224. if (d->relative.first_last_day_of != 0) {
  225. switch (d->relative.first_last_day_of) {
  226. case 1:
  227. printf(" / first day of");
  228. break;
  229. case 2:
  230. printf(" / last day of");
  231. break;
  232. }
  233. }
  234. if (d->relative.have_weekday_relative) {
  235. printf(" / %d.%d", d->relative.weekday, d->relative.weekday_behavior);
  236. }
  237. if (d->relative.have_special_relative) {
  238. switch (d->relative.special.type) {
  239. case TIMELIB_SPECIAL_WEEKDAY:
  240. printf(" / %lld weekday", d->relative.special.amount);
  241. break;
  242. case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
  243. printf(" / x y of z month");
  244. break;
  245. case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
  246. printf(" / last y of z month");
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. printf("\n");
  253. }
  254. void timelib_dump_rel_time(timelib_rel_time *d)
  255. {
  256. printf("%3lldY %3lldM %3lldD / %3lldH %3lldM %3lldS (days: %lld)%s",
  257. d->y, d->m, d->d, d->h, d->i, d->s, d->days, d->invert ? " inverted" : "");
  258. if (d->first_last_day_of != 0) {
  259. switch (d->first_last_day_of) {
  260. case 1:
  261. printf(" / first day of");
  262. break;
  263. case 2:
  264. printf(" / last day of");
  265. break;
  266. }
  267. }
  268. printf("\n");
  269. }
  270. timelib_long timelib_parse_tz_cor(char **ptr)
  271. {
  272. char *begin = *ptr, *end;
  273. timelib_long tmp;
  274. while (isdigit(**ptr) || **ptr == ':') {
  275. ++*ptr;
  276. }
  277. end = *ptr;
  278. switch (end - begin) {
  279. case 1: /* H */
  280. case 2: /* HH */
  281. return HOUR(strtol(begin, NULL, 10));
  282. break;
  283. case 3: /* H:M */
  284. case 4: /* H:MM, HH:M, HHMM */
  285. if (begin[1] == ':') {
  286. tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
  287. return tmp;
  288. } else if (begin[2] == ':') {
  289. tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
  290. return tmp;
  291. } else {
  292. tmp = strtol(begin, NULL, 10);
  293. return HOUR(tmp / 100) + tmp % 100;
  294. }
  295. case 5: /* HH:MM */
  296. tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
  297. return tmp;
  298. }
  299. return 0;
  300. }