tm2unixtime.c 14 KB

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