tm2unixtime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. /* jan feb mrt apr may jun jul aug sep oct nov dec */
  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. /* dec jan feb mrt apr may jun jul aug sep oct nov dec */
  29. static int days_in_month_leap[13] = { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  30. static int days_in_month[13] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  31. static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
  32. {
  33. if (*a < start) {
  34. *b -= (start - *a - 1) / adj + 1;
  35. *a += adj * ((start - *a - 1) / adj + 1);
  36. }
  37. if (*a >= end) {
  38. *b += *a / adj;
  39. *a -= adj * (*a / adj);
  40. }
  41. }
  42. static void inc_month(timelib_sll *y, timelib_sll *m)
  43. {
  44. (*m)++;
  45. if (*m > 12) {
  46. *m -= 12;
  47. (*y)++;
  48. }
  49. }
  50. static void dec_month(timelib_sll *y, timelib_sll *m)
  51. {
  52. (*m)--;
  53. if (*m < 1) {
  54. *m += 12;
  55. (*y)--;
  56. }
  57. }
  58. static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
  59. {
  60. timelib_sll leapyear;
  61. timelib_sll month, year;
  62. timelib_sll days;
  63. do_range_limit(1, 13, 12, base_m, base_y);
  64. year = *base_y;
  65. month = *base_m;
  66. /*
  67. printf( "S: Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days);
  68. */
  69. if (!invert) {
  70. while (*d < 0) {
  71. dec_month(&year, &month);
  72. leapyear = timelib_is_leap(year);
  73. days = leapyear ? days_in_month_leap[month] : days_in_month[month];
  74. /* printf( "I Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days); */
  75. *d += days;
  76. (*m)--;
  77. }
  78. } else {
  79. while (*d < 0) {
  80. leapyear = timelib_is_leap(year);
  81. days = leapyear ? days_in_month_leap[month] : days_in_month[month];
  82. /* printf( "I Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days); */
  83. *d += days;
  84. (*m)--;
  85. inc_month(&year, &month);
  86. }
  87. }
  88. /*
  89. printf( "E: Y%d M%d %d %d %d %d\n", year, month, *y, *m, *d, days);
  90. */
  91. }
  92. static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
  93. {
  94. timelib_sll leapyear;
  95. timelib_sll days_this_month;
  96. timelib_sll last_month, last_year;
  97. timelib_sll days_last_month;
  98. /* can jump an entire leap year period quickly */
  99. if (*d >= DAYS_PER_LYEAR_PERIOD || *d <= -DAYS_PER_LYEAR_PERIOD) {
  100. *y += YEARS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
  101. *d -= DAYS_PER_LYEAR_PERIOD * (*d / DAYS_PER_LYEAR_PERIOD);
  102. }
  103. do_range_limit(1, 13, 12, m, y);
  104. leapyear = timelib_is_leap(*y);
  105. days_this_month = leapyear ? days_in_month_leap[*m] : days_in_month[*m];
  106. last_month = (*m) - 1;
  107. if (last_month < 1) {
  108. last_month += 12;
  109. last_year = (*y) - 1;
  110. } else {
  111. last_year = (*y);
  112. }
  113. leapyear = timelib_is_leap(last_year);
  114. days_last_month = leapyear ? days_in_month_leap[last_month] : days_in_month[last_month];
  115. if (*d <= 0) {
  116. *d += days_last_month;
  117. (*m)--;
  118. return 1;
  119. }
  120. if (*d > days_this_month) {
  121. *d -= days_this_month;
  122. (*m)++;
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. static void do_adjust_for_weekday(timelib_time* time)
  128. {
  129. timelib_sll current_dow, difference;
  130. current_dow = timelib_day_of_week(time->y, time->m, time->d);
  131. if (time->relative.weekday_behavior == 2)
  132. {
  133. /* To make "this week" work, where the current DOW is a "sunday" */
  134. if (current_dow == 0 && time->relative.weekday != 0) {
  135. time->relative.weekday = -6;
  136. }
  137. /* To make "sunday this week" work, where the current DOW is not a
  138. * "sunday" */
  139. if (time->relative.weekday == 0 && current_dow != 0) {
  140. time->relative.weekday = 7;
  141. }
  142. time->d -= current_dow;
  143. time->d += time->relative.weekday;
  144. return;
  145. }
  146. difference = time->relative.weekday - current_dow;
  147. if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
  148. difference += 7;
  149. }
  150. if (time->relative.weekday >= 0) {
  151. time->d += difference;
  152. } else {
  153. time->d -= (7 - (abs(time->relative.weekday) - current_dow));
  154. }
  155. time->relative.have_weekday_relative = 0;
  156. }
  157. void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
  158. {
  159. do_range_limit(0, 60, 60, &rt->s, &rt->i);
  160. do_range_limit(0, 60, 60, &rt->i, &rt->h);
  161. do_range_limit(0, 24, 24, &rt->h, &rt->d);
  162. do_range_limit(0, 12, 12, &rt->m, &rt->y);
  163. do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
  164. do_range_limit(0, 12, 12, &rt->m, &rt->y);
  165. }
  166. void timelib_do_normalize(timelib_time* time)
  167. {
  168. if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
  169. if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
  170. if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
  171. do_range_limit(1, 13, 12, &time->m, &time->y);
  172. do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
  173. do_range_limit(1, 13, 12, &time->m, &time->y);
  174. }
  175. static void do_adjust_relative(timelib_time* time)
  176. {
  177. if (time->relative.have_weekday_relative) {
  178. do_adjust_for_weekday(time);
  179. }
  180. timelib_do_normalize(time);
  181. if (time->have_relative) {
  182. time->s += time->relative.s;
  183. time->i += time->relative.i;
  184. time->h += time->relative.h;
  185. time->d += time->relative.d;
  186. time->m += time->relative.m;
  187. time->y += time->relative.y;
  188. }
  189. switch (time->relative.first_last_day_of) {
  190. case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
  191. time->d = 1;
  192. break;
  193. case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
  194. time->d = 0;
  195. time->m++;
  196. break;
  197. }
  198. timelib_do_normalize(time);
  199. }
  200. static void do_adjust_special_weekday(timelib_time* time)
  201. {
  202. timelib_sll count, dow, rem;
  203. count = time->relative.special.amount;
  204. dow = timelib_day_of_week(time->y, time->m, time->d);
  205. /* Add increments of 5 weekdays as a week, leaving the DOW unchanged. */
  206. time->d += (count / 5) * 7;
  207. /* Deal with the remainder. */
  208. rem = (count % 5);
  209. if (count > 0) {
  210. if (rem == 0) {
  211. /* Head back to Friday if we stop on the weekend. */
  212. if (dow == 0) {
  213. time->d -= 2;
  214. } else if (dow == 6) {
  215. time->d -= 1;
  216. }
  217. } else if (dow == 6) {
  218. /* We ended up on Saturday, but there's still work to do, so move
  219. * to Sunday and continue from there. */
  220. time->d += 1;
  221. } else if (dow + rem > 5) {
  222. /* We're on a weekday, but we're going past Friday, so skip right
  223. * over the weekend. */
  224. time->d += 2;
  225. }
  226. } else {
  227. /* Completely mirror the forward direction. This also covers the 0
  228. * case, since if we start on the weekend, we want to move forward as
  229. * if we stopped there while going backwards. */
  230. if (rem == 0) {
  231. if (dow == 6) {
  232. time->d += 2;
  233. } else if (dow == 0) {
  234. time->d += 1;
  235. }
  236. } else if (dow == 0) {
  237. time->d -= 1;
  238. } else if (dow + rem < 1) {
  239. time->d -= 2;
  240. }
  241. }
  242. time->d += rem;
  243. }
  244. static void do_adjust_special(timelib_time* time)
  245. {
  246. if (time->relative.have_special_relative) {
  247. switch (time->relative.special.type) {
  248. case TIMELIB_SPECIAL_WEEKDAY:
  249. do_adjust_special_weekday(time);
  250. break;
  251. }
  252. }
  253. timelib_do_normalize(time);
  254. memset(&(time->relative.special), 0, sizeof(time->relative.special));
  255. }
  256. static void do_adjust_special_early(timelib_time* time)
  257. {
  258. if (time->relative.have_special_relative) {
  259. switch (time->relative.special.type) {
  260. case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
  261. time->d = 1;
  262. time->m += time->relative.m;
  263. time->relative.m = 0;
  264. break;
  265. case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
  266. time->d = 1;
  267. time->m += time->relative.m + 1;
  268. time->relative.m = 0;
  269. break;
  270. }
  271. }
  272. switch (time->relative.first_last_day_of) {
  273. case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
  274. time->d = 1;
  275. break;
  276. case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
  277. time->d = 0;
  278. time->m++;
  279. break;
  280. }
  281. timelib_do_normalize(time);
  282. }
  283. static timelib_sll do_years(timelib_sll year)
  284. {
  285. timelib_sll i;
  286. timelib_sll res = 0;
  287. timelib_sll eras;
  288. eras = (year - 1970) / 40000;
  289. if (eras != 0) {
  290. year = year - (eras * 40000);
  291. res += (SECS_PER_ERA * eras * 100);
  292. }
  293. if (year >= 1970) {
  294. for (i = year - 1; i >= 1970; i--) {
  295. if (timelib_is_leap(i)) {
  296. res += (DAYS_PER_LYEAR * SECS_PER_DAY);
  297. } else {
  298. res += (DAYS_PER_YEAR * SECS_PER_DAY);
  299. }
  300. }
  301. } else {
  302. for (i = 1969; i >= year; i--) {
  303. if (timelib_is_leap(i)) {
  304. res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
  305. } else {
  306. res -= (DAYS_PER_YEAR * SECS_PER_DAY);
  307. }
  308. }
  309. }
  310. return res;
  311. }
  312. static timelib_sll do_months(timelib_ull month, timelib_ull year)
  313. {
  314. if (timelib_is_leap(year)) {
  315. return ((month_tab_leap[month - 1] + 1) * SECS_PER_DAY);
  316. } else {
  317. return ((month_tab[month - 1]) * SECS_PER_DAY);
  318. }
  319. }
  320. static timelib_sll do_days(timelib_ull day)
  321. {
  322. return ((day - 1) * SECS_PER_DAY);
  323. }
  324. static timelib_sll do_time(timelib_ull hour, timelib_ull minute, timelib_ull second)
  325. {
  326. timelib_sll res = 0;
  327. res += hour * 3600;
  328. res += minute * 60;
  329. res += second;
  330. return res;
  331. }
  332. static timelib_sll do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
  333. {
  334. switch (tz->zone_type) {
  335. case TIMELIB_ZONETYPE_OFFSET:
  336. tz->is_localtime = 1;
  337. return tz->z * 60;
  338. break;
  339. case TIMELIB_ZONETYPE_ABBR: {
  340. timelib_sll tmp;
  341. tz->is_localtime = 1;
  342. tmp = tz->z;
  343. tmp -= tz->dst * 60;
  344. tmp *= 60;
  345. return tmp;
  346. }
  347. break;
  348. case TIMELIB_ZONETYPE_ID:
  349. tzi = tz->tz_info;
  350. /* Break intentionally missing */
  351. default:
  352. /* No timezone in struct, fallback to reference if possible */
  353. if (tzi) {
  354. timelib_time_offset *before, *after;
  355. timelib_sll tmp;
  356. int in_transistion;
  357. tz->is_localtime = 1;
  358. before = timelib_get_time_zone_info(tz->sse, tzi);
  359. after = timelib_get_time_zone_info(tz->sse - before->offset, tzi);
  360. timelib_set_timezone(tz, tzi);
  361. in_transistion = (
  362. ((tz->sse - after->offset) >= (after->transistion_time + (before->offset - after->offset))) &&
  363. ((tz->sse - after->offset) < after->transistion_time)
  364. );
  365. if ((before->offset != after->offset) && !in_transistion) {
  366. tmp = -after->offset;
  367. } else {
  368. tmp = -tz->z;
  369. }
  370. timelib_time_offset_dtor(before);
  371. timelib_time_offset_dtor(after);
  372. {
  373. timelib_time_offset *gmt_offset;
  374. gmt_offset = timelib_get_time_zone_info(tz->sse + tmp, tzi);
  375. tz->z = gmt_offset->offset;
  376. tz->dst = gmt_offset->is_dst;
  377. if (tz->tz_abbr) {
  378. timelib_free(tz->tz_abbr);
  379. }
  380. tz->tz_abbr = timelib_strdup(gmt_offset->abbr);
  381. timelib_time_offset_dtor(gmt_offset);
  382. }
  383. return tmp;
  384. }
  385. }
  386. return 0;
  387. }
  388. void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
  389. {
  390. timelib_sll res = 0;
  391. do_adjust_special_early(time);
  392. do_adjust_relative(time);
  393. do_adjust_special(time);
  394. res += do_years(time->y);
  395. res += do_months(time->m, time->y);
  396. res += do_days(time->d);
  397. res += do_time(time->h, time->i, time->s);
  398. time->sse = res;
  399. res += do_adjust_timezone(time, tzi);
  400. time->sse = res;
  401. time->sse_uptodate = 1;
  402. time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = 0;
  403. }
  404. #if 0
  405. int main(void)
  406. {
  407. timelib_sll res;
  408. timelib_time time;
  409. time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
  410. printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
  411. time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
  412. if (time.have_relative) {
  413. printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
  414. time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
  415. }
  416. if (time.have_weekday_relative) {
  417. printf (" / %d", time.relative.weekday);
  418. }
  419. res = time2unixtime(&time);
  420. printf("%Ld\n", res);
  421. return 0;
  422. }
  423. #endif