tm2unixtime.c 14 KB

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