_time.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #include "_time.h"
  2. #include "PikaVM.h"
  3. #if defined(__linux)
  4. #include <unistd.h>
  5. #endif
  6. #if defined(_WIN32)
  7. #include <windows.h>
  8. #endif
  9. void (*global_do_sleep_ms)(uint32_t);
  10. extern volatile VMState g_PikaVMState;
  11. volatile int g_pika_local_timezone = 8;
  12. static void _do_sleep_ms_tick(uint32_t ms) {
  13. pika_sleep_ms(ms);
  14. }
  15. void _time_sleep_ms(PikaObj* self, int ms) {
  16. pika_GIL_EXIT();
  17. global_do_sleep_ms(ms);
  18. pika_GIL_ENTER();
  19. }
  20. void _time_sleep_s(PikaObj* self, int s) {
  21. pika_GIL_EXIT();
  22. for (int i = 0; i < s; i++) {
  23. global_do_sleep_ms(1000);
  24. }
  25. pika_GIL_ENTER();
  26. }
  27. void _time_platformGetTick(PikaObj* self) {
  28. obj_setInt(self, "tick", __platform_getTick());
  29. }
  30. /*
  31. * @Author: Once day
  32. * @LastEditTime: 2022-06-04 12:10:52
  33. * Encoder=utf-8,Email:once_day@qq.com
  34. */
  35. #include "stdint.h"
  36. #include "stdio.h"
  37. typedef struct _tm {
  38. int tm_sec; // seconds after the minute - [0, 60] including leap second
  39. int tm_min; // minutes after the hour - [0, 59]
  40. int tm_hour; // hours since midnight - [0, 23]
  41. int tm_mday; // day of the month - [1, 31]
  42. int tm_mon; // months since January - [0, 11]
  43. int tm_year; // years since 1900
  44. int tm_wday; // days since Sunday - [0, 6]
  45. int tm_yday; // days since January 1 - [0, 365]
  46. int tm_isdst; // daylight savings time flag
  47. } _tm;
  48. typedef struct {
  49. int64_t tv_sec; // Seconds - >= 0
  50. int32_t tv_nsec; // Nanoseconds - [0, 999999999]
  51. } _timespec;
  52. typedef int status;
  53. #define TIME_OK 0
  54. #define TIME_ERROR -1
  55. #define TIME_GET_TIME_FAIL 1
  56. #define TIME_GET_TICK_FAIL 2
  57. #define TIME_LESS_THAN_ZERO 3
  58. #define TIME_OVER_3200 4
  59. #define TIME_LESS_THAN_1970 5
  60. #define TIME_ERROR_STRUCT_TIME 6
  61. void status_deal(status s) {
  62. #define time_printf(...) __platform_printf(__VA_ARGS__)
  63. time_printf("\n[Error-info]Checking a exception : ");
  64. switch (s) {
  65. case TIME_ERROR:
  66. time_printf("Unknow error!!!\n");
  67. break;
  68. case TIME_GET_TIME_FAIL:
  69. time_printf("Fail to get Unix-time from hardware !\n");
  70. break;
  71. case TIME_GET_TICK_FAIL:
  72. time_printf("Fail to get Tick-time from hardware !\n");
  73. break;
  74. case TIME_LESS_THAN_ZERO:
  75. time_printf("Input a negative Unix timestamp !\n");
  76. break;
  77. case TIME_OVER_3200:
  78. time_printf("The time point exceeds 3200 AD !\n");
  79. break;
  80. case TIME_LESS_THAN_1970:
  81. time_printf("The time point less-than 1970 AD !\n");
  82. break;
  83. case TIME_ERROR_STRUCT_TIME:
  84. time_printf("The struct-time's range is wrong !\n");
  85. break;
  86. default:
  87. break;
  88. }
  89. time_printf("\n");
  90. }
  91. status time_get_unix_time(PikaObj* self, _timespec* this_timespec) {
  92. this_timespec->tv_sec = (int64_t)(obj_getInt(self, "tick") / 1000);
  93. return TIME_OK;
  94. }
  95. status time_get_tick_ns(PikaObj* self, _timespec* this_timespec) {
  96. this_timespec->tv_nsec = (obj_getInt(self, "tick") % 1000) * 1000000;
  97. return TIME_OK;
  98. }
  99. pika_float time_time(PikaObj* self) {
  100. status res = 0;
  101. _timespec temp_timespec = {0};
  102. res = time_get_unix_time(self, &temp_timespec);
  103. if (res) {
  104. status_deal(res);
  105. }
  106. res = time_get_tick_ns(self, &temp_timespec);
  107. if (res) {
  108. status_deal(res);
  109. }
  110. return temp_timespec.tv_sec +
  111. (pika_float)temp_timespec.tv_nsec / 1000000000;
  112. }
  113. int64_t time_time_ns(PikaObj* self) {
  114. status res = 0;
  115. _timespec temp_timespec = {0};
  116. res = time_get_unix_time(self, &temp_timespec);
  117. if (res) {
  118. status_deal(res);
  119. }
  120. res = time_get_tick_ns(self, &temp_timespec);
  121. if (res) {
  122. status_deal(res);
  123. }
  124. return temp_timespec.tv_sec * 1000000000 + temp_timespec.tv_nsec;
  125. }
  126. int time_get_week(const _tm* this_tm) {
  127. int month = this_tm->tm_mon + 1;
  128. int year = this_tm->tm_year;
  129. int day = this_tm->tm_mday;
  130. int w;
  131. if (month == 1 || month == 2) {
  132. month += 12;
  133. year -= 1;
  134. w = day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 -
  135. year / 100 + year / 400 + 1; //
  136. w = w % 7;
  137. } else {
  138. w = day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 -
  139. year / 100 + year / 400 + 1; //
  140. w = w % 7;
  141. }
  142. return w;
  143. }
  144. status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) {
  145. int32_t total_day;
  146. int32_t extra_second;
  147. int year_400, year_100, year_4, year_1;
  148. int february_offset, temp;
  149. if (unix_time < 0) {
  150. return TIME_LESS_THAN_ZERO;
  151. }
  152. #define DAY_SECOND (86400)
  153. total_day = unix_time / DAY_SECOND;
  154. extra_second = unix_time - total_day * DAY_SECOND;
  155. #define YEAR_START (1600)
  156. #define DAY_OFFSET (135140)
  157. total_day += DAY_OFFSET;
  158. #define MAX_DAY (584388)
  159. if (total_day > MAX_DAY) {
  160. return TIME_OVER_3200;
  161. } else {
  162. #define DAY_OF_400Y (146097)
  163. #define DAY_OF_100Y (36524)
  164. #define DAY_OF_4Y (1461)
  165. #define DAY_OF_1Y (365)
  166. year_400 = (total_day - 366) / DAY_OF_400Y;
  167. total_day -= year_400 * DAY_OF_400Y;
  168. year_100 = (total_day - 1) / DAY_OF_100Y;
  169. total_day -= year_100 * DAY_OF_100Y;
  170. year_4 = (total_day - 366) / DAY_OF_4Y;
  171. total_day -= year_4 * DAY_OF_4Y;
  172. if (year_100 == 4) {
  173. year_1 = 0;
  174. february_offset = 1;
  175. } else if (total_day <= DAY_OF_1Y * 4) {
  176. year_1 = (total_day - 1) / DAY_OF_1Y;
  177. total_day -= year_1 * DAY_OF_1Y;
  178. february_offset = 0;
  179. } else {
  180. year_1 = 4;
  181. total_day -= year_1 * DAY_OF_1Y;
  182. february_offset = 1;
  183. }
  184. this_tm->tm_year =
  185. (year_400 * 400 + year_100 * 100 + year_4 * 4 + year_1) +
  186. YEAR_START;
  187. this_tm->tm_yday = total_day;
  188. total_day -= february_offset;
  189. if (total_day <= 181) {
  190. if (total_day <= 90) {
  191. if (total_day <= 59) {
  192. total_day += february_offset;
  193. if (total_day <= 31) {
  194. temp = 0;
  195. } else {
  196. total_day -= 31;
  197. temp = 1;
  198. }
  199. } else {
  200. total_day -= 59;
  201. temp = 2;
  202. }
  203. } else {
  204. total_day -= 90;
  205. if (total_day <= 30) {
  206. temp = 3;
  207. } else {
  208. total_day -= 30;
  209. if (total_day <= 31) {
  210. temp = 4;
  211. } else {
  212. total_day -= 31;
  213. temp = 5;
  214. }
  215. }
  216. }
  217. } else {
  218. total_day -= 181;
  219. if (total_day <= 92) {
  220. if (total_day <= 62) {
  221. if (total_day <= 31) {
  222. temp = 6;
  223. } else {
  224. total_day -= 31;
  225. temp = 7;
  226. }
  227. } else {
  228. total_day -= 62;
  229. temp = 8;
  230. }
  231. } else {
  232. total_day -= 92;
  233. if (total_day <= 61) {
  234. if (total_day <= 31) {
  235. temp = 9;
  236. } else {
  237. total_day -= 31;
  238. temp = 10;
  239. }
  240. } else {
  241. total_day -= 61;
  242. temp = 11;
  243. }
  244. }
  245. }
  246. this_tm->tm_mon = temp;
  247. this_tm->tm_mday = total_day;
  248. temp = extra_second / 3600;
  249. this_tm->tm_hour = temp;
  250. extra_second = extra_second - temp * 3600;
  251. temp = extra_second / 60;
  252. this_tm->tm_min = temp;
  253. extra_second = extra_second - temp * 60;
  254. this_tm->tm_sec = extra_second;
  255. this_tm->tm_wday = time_get_week(this_tm);
  256. this_tm->tm_isdst = -1;
  257. }
  258. return TIME_OK;
  259. }
  260. status utc_struct_time_to_unix_time(const _tm* this_tm, int64_t* unix_time) {
  261. int32_t total_day, total_leap_year, dyear;
  262. int february_offset;
  263. // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  264. const int month_day[] = {0, 31, 59, 90, 120, 151,
  265. 181, 212, 243, 273, 304, 334};
  266. if (this_tm->tm_year < 1970) {
  267. *unix_time = 0;
  268. return TIME_LESS_THAN_1970;
  269. }
  270. if (this_tm->tm_year >= 3200) {
  271. *unix_time = 0;
  272. return TIME_OVER_3200;
  273. }
  274. dyear = this_tm->tm_year - YEAR_START - 1;
  275. total_leap_year = dyear / 4 - (dyear / 100 - dyear / 400 - 1);
  276. dyear += 1;
  277. total_day = dyear * 365 + total_leap_year;
  278. total_day -= DAY_OFFSET;
  279. if (((dyear % 4 == 0) && (dyear % 100 != 0)) || (dyear % 400 == 0)) {
  280. february_offset = 1;
  281. } else {
  282. february_offset = 0;
  283. }
  284. total_day += month_day[this_tm->tm_mon] + this_tm->tm_mday - 1;
  285. if (this_tm->tm_mon > 1) {
  286. total_day += february_offset;
  287. }
  288. *unix_time = (int64_t)total_day * DAY_SECOND + this_tm->tm_hour * 3600 +
  289. this_tm->tm_min * 60 + this_tm->tm_sec;
  290. return TIME_OK;
  291. }
  292. void time_struct_format(const _tm* this_tm, char* str) {
  293. sprintf(str,
  294. "time.struct_time(tm_year=%d, tm_mon=%d,tm_mday=%d, tm_hour=%d, "
  295. "tm_min=%d, tm_sec=%d, tm_wday=%d,tm_yday=%d, tm_isdst=%d)",
  296. this_tm->tm_year, this_tm->tm_mon + 1, this_tm->tm_mday,
  297. this_tm->tm_hour, this_tm->tm_min, this_tm->tm_sec,
  298. this_tm->tm_wday, this_tm->tm_yday, this_tm->tm_isdst);
  299. }
  300. void time_gmtime(pika_float unix_time, _tm* this_tm) {
  301. status res;
  302. res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time);
  303. if (res) {
  304. status_deal(res);
  305. unix_time_to_utc_struct_time(this_tm, (int64_t)0);
  306. }
  307. }
  308. // struct_time
  309. void time_localtime(pika_float unix_time, _tm* this_tm, int locale) {
  310. status res;
  311. int local_offset;
  312. local_offset = locale * 60 * 60;
  313. res = unix_time_to_utc_struct_time(this_tm,
  314. (int64_t)unix_time + local_offset);
  315. if (res) {
  316. status_deal(res);
  317. unix_time_to_utc_struct_time(this_tm, (int64_t)0);
  318. }
  319. }
  320. status time_check_struct_time(const _tm* this_tm) {
  321. if (this_tm->tm_sec < 0 || this_tm->tm_sec > 60) {
  322. return TIME_ERROR_STRUCT_TIME;
  323. }
  324. if (this_tm->tm_min < 0 || this_tm->tm_min > 59) {
  325. return TIME_ERROR_STRUCT_TIME;
  326. }
  327. if (this_tm->tm_hour < 0 || this_tm->tm_hour > 23) {
  328. return TIME_ERROR_STRUCT_TIME;
  329. }
  330. if (this_tm->tm_mday < 1 || this_tm->tm_mday > 31) {
  331. return TIME_ERROR_STRUCT_TIME;
  332. }
  333. if (this_tm->tm_mon < 0 || this_tm->tm_mon > 11) {
  334. return TIME_ERROR_STRUCT_TIME;
  335. }
  336. if (this_tm->tm_wday < 0 || this_tm->tm_wday > 6) {
  337. return TIME_ERROR_STRUCT_TIME;
  338. }
  339. if (this_tm->tm_yday < 0 || this_tm->tm_yday > 366) {
  340. return TIME_ERROR_STRUCT_TIME;
  341. }
  342. return TIME_OK;
  343. }
  344. int64_t time_mktime(const _tm* this_tm, int locale) {
  345. status res;
  346. int local_offset;
  347. int64_t unix_time;
  348. local_offset = locale * 60 * 60;
  349. res = time_check_struct_time(this_tm);
  350. if (res) {
  351. status_deal(res);
  352. return 0;
  353. }
  354. res = utc_struct_time_to_unix_time(this_tm, &unix_time);
  355. if (res) {
  356. status_deal(res);
  357. return 0;
  358. }
  359. unix_time -= local_offset;
  360. // time_printf("%I64d\n",unix_time);
  361. return unix_time;
  362. }
  363. void time_asctime(const _tm* this_tm) {
  364. const char* week[] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
  365. const char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  366. "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
  367. char str[100];
  368. sprintf(str, "%s %s %d %02d:%02d:%02d %d", week[this_tm->tm_wday],
  369. month[this_tm->tm_mon], this_tm->tm_mday, this_tm->tm_hour,
  370. this_tm->tm_min, this_tm->tm_sec, this_tm->tm_year);
  371. time_printf("%s\n", str);
  372. }
  373. pika_float _time_time(PikaObj* self) {
  374. /* run platformGetTick() */
  375. PIKA_PYTHON_BEGIN
  376. /* clang-format off */
  377. PIKA_PYTHON(
  378. platformGetTick()
  379. )
  380. /* clang-format on */
  381. const uint8_t bytes[] = {
  382. 0x04, 0x00, 0x00, 0x00, /* instruct array size */
  383. 0x00, 0x82, 0x01, 0x00, /* instruct array */
  384. 0x11, 0x00, 0x00, 0x00, /* const pool size */
  385. 0x00, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
  386. 0x47, 0x65, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x00, /* const pool */
  387. };
  388. PIKA_PYTHON_END
  389. pikaVM_runByteCode(self, (uint8_t*)bytes);
  390. return time_time(self);
  391. }
  392. int _time_time_ns(PikaObj* self) {
  393. return time_time_ns(self);
  394. }
  395. void time_set_tm_value(PikaObj* self, const _tm* this_tm) {
  396. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  397. obj_setErrorCode(self, 1);
  398. obj_setSysOut(
  399. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  400. #else
  401. obj_setInt(self, "tm_sec", this_tm->tm_sec);
  402. obj_setInt(self, "tm_min", this_tm->tm_min);
  403. obj_setInt(self, "tm_hour", this_tm->tm_hour);
  404. obj_setInt(self, "tm_mday", this_tm->tm_mday);
  405. obj_setInt(self, "tm_mon", this_tm->tm_mon);
  406. obj_setInt(self, "tm_year", this_tm->tm_year);
  407. obj_setInt(self, "tm_wday", this_tm->tm_wday);
  408. obj_setInt(self, "tm_yday", this_tm->tm_yday);
  409. obj_setInt(self, "tm_isdst", this_tm->tm_isdst);
  410. #endif
  411. }
  412. void _time_gmtime(PikaObj* self, pika_float unix_time) {
  413. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  414. obj_setErrorCode(self, 1);
  415. obj_setSysOut(
  416. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  417. #else
  418. _tm this_tm;
  419. char str[200];
  420. time_gmtime(unix_time, &this_tm);
  421. time_set_tm_value(self, &this_tm);
  422. time_struct_format(&this_tm, str);
  423. time_printf("%s\n", str);
  424. #endif
  425. }
  426. void _time_localtime(PikaObj* self, pika_float unix_time) {
  427. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  428. obj_setErrorCode(self, 1);
  429. obj_setSysOut(
  430. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  431. #else
  432. _tm this_tm;
  433. char str[200];
  434. int locale = g_pika_local_timezone;
  435. time_localtime(unix_time, &this_tm, locale);
  436. time_set_tm_value(self, &this_tm);
  437. time_struct_format(&this_tm, str);
  438. time_printf("%s\n", str);
  439. #endif
  440. }
  441. void time_get_tm_value(PikaObj* self, _tm* this_tm) {
  442. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  443. obj_setErrorCode(self, 1);
  444. obj_setSysOut(
  445. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  446. #else
  447. this_tm->tm_sec = obj_getInt(self, "tm_sec");
  448. this_tm->tm_min = obj_getInt(self, "tm_min");
  449. this_tm->tm_hour = obj_getInt(self, "tm_hour");
  450. this_tm->tm_mday = obj_getInt(self, "tm_mday");
  451. this_tm->tm_mon = obj_getInt(self, "tm_mon");
  452. this_tm->tm_year = obj_getInt(self, "tm_year");
  453. this_tm->tm_wday = obj_getInt(self, "tm_wday");
  454. this_tm->tm_yday = obj_getInt(self, "tm_yday");
  455. this_tm->tm_isdst = obj_getInt(self, "tm_isdst");
  456. #endif
  457. }
  458. int _time_mktime(PikaObj* self) {
  459. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  460. obj_setErrorCode(self, 1);
  461. obj_setSysOut(
  462. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  463. return 0;
  464. #else
  465. _tm this_tm;
  466. int locale = g_pika_local_timezone;
  467. time_get_tm_value(self, &this_tm);
  468. return time_mktime(&this_tm, locale);
  469. #endif
  470. }
  471. void _time_asctime(PikaObj* self) {
  472. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  473. obj_setErrorCode(self, 1);
  474. obj_setSysOut(
  475. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  476. #else
  477. _time_ctime(self, _time_time(self));
  478. #endif
  479. }
  480. void _time_ctime(PikaObj* self, pika_float unix_time) {
  481. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  482. obj_setErrorCode(self, 1);
  483. obj_setSysOut(
  484. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  485. #else
  486. _tm this_tm;
  487. int locale = g_pika_local_timezone;
  488. time_localtime(unix_time, &this_tm, locale);
  489. time_asctime(&this_tm);
  490. #endif
  491. }
  492. void _time___init__(PikaObj* self) {
  493. if (-1 == pika_platform_get_tick()) {
  494. global_do_sleep_ms = pika_platform_sleep_ms;
  495. } else {
  496. global_do_sleep_ms = _do_sleep_ms_tick;
  497. }
  498. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  499. #else
  500. _tm this_tm;
  501. g_pika_local_timezone = 8;
  502. time_localtime(0.0, &this_tm, 8);
  503. time_set_tm_value(self, &this_tm);
  504. #endif
  505. }