tm2unixtime.c 14 KB

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