_time.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 VMSignal g_PikaVMSignal;
  11. volatile int g_pika_local_timezone = 8;
  12. static void _do_sleep_ms_tick(uint32_t ms) {
  13. int64_t tick = pika_platform_get_tick();
  14. while (pika_platform_get_tick() - tick < ms) {
  15. #if PIKA_EVENT_ENABLE
  16. if (!g_PikaVMSignal.event_thread_inited) {
  17. _VMEvent_pickupEvent();
  18. }
  19. #endif
  20. pika_platform_thread_delay();
  21. }
  22. }
  23. void _time_sleep_ms(PikaObj* self, int ms) {
  24. pika_GIL_EXIT();
  25. global_do_sleep_ms(ms);
  26. pika_GIL_ENTER();
  27. }
  28. void _time_sleep_s(PikaObj* self, int s) {
  29. pika_GIL_EXIT();
  30. for (int i = 0; i < s; i++) {
  31. global_do_sleep_ms(1000);
  32. }
  33. pika_GIL_ENTER();
  34. }
  35. void _time_platformGetTick(PikaObj* self) {
  36. obj_setInt(self, "tick", __platform_getTick());
  37. }
  38. /*
  39. * @Author: Once day
  40. * @LastEditTime: 2022-06-04 12:10:52
  41. * Encoder=utf-8,Email:once_day@qq.com
  42. */
  43. #include "stdint.h"
  44. #include "stdio.h"
  45. // 结构体时间类型定义(来源c标准库corect_wtime.h)
  46. // 无论是16位整数还是32位整数都满足需求
  47. typedef struct _tm {
  48. int tm_sec; // seconds after the minute - [0, 60] including leap second
  49. int tm_min; // minutes after the hour - [0, 59]
  50. int tm_hour; // hours since midnight - [0, 23]
  51. int tm_mday; // day of the month - [1, 31]
  52. int tm_mon; // months since January - [0, 11]
  53. int tm_year; // years since 1900
  54. int tm_wday; // days since Sunday - [0, 6]
  55. int tm_yday; // days since January 1 - [0, 365]
  56. int tm_isdst; // daylight savings time flag
  57. } _tm;
  58. // 时间戳时间类型定义(来源c标准库time.h)
  59. // 直接支持64位秒数时间,附加时间精度为ns,根据设备决定,需要1GHz及以上时钟频率才能支持1ns级别时间精度
  60. // 内部时间比对数据类型,传递给外界的时候会使用浮点数,所以精度会降低
  61. // 但内部使用复合数据类型比对,以实现平台支持的最小时间精度比较
  62. typedef struct {
  63. int64_t tv_sec; // Seconds - >= 0
  64. int32_t tv_nsec; // Nanoseconds - [0, 999999999]
  65. } _timespec;
  66. // 错误处理
  67. typedef int status;
  68. #define TIME_OK 0
  69. #define TIME_ERROR -1
  70. #define TIME_GET_TIME_FAIL 1
  71. #define TIME_GET_TICK_FAIL 2
  72. #define TIME_LESS_THAN_ZERO 3
  73. #define TIME_OVER_3200 4
  74. #define TIME_LESS_THAN_1970 5
  75. #define TIME_ERROR_STRUCT_TIME 6
  76. // 错误状态处理函数
  77. void status_deal(status s) {
  78. // 输出异常信息
  79. #define time_printf(...) __platform_printf(__VA_ARGS__)
  80. time_printf("\n[Error-info]Checking a exception : ");
  81. switch (s) {
  82. case TIME_ERROR:
  83. time_printf("Unknow error!!!\n");
  84. break;
  85. case TIME_GET_TIME_FAIL:
  86. time_printf("Fail to get Unix-time from hardware !\n");
  87. break;
  88. case TIME_GET_TICK_FAIL:
  89. time_printf("Fail to get Tick-time from hardware !\n");
  90. break;
  91. case TIME_LESS_THAN_ZERO:
  92. time_printf("Input a negative Unix timestamp !\n");
  93. break;
  94. case TIME_OVER_3200:
  95. time_printf("The time point exceeds 3200 AD !\n");
  96. break;
  97. case TIME_LESS_THAN_1970:
  98. time_printf("The time point less-than 1970 AD !\n");
  99. break;
  100. case TIME_ERROR_STRUCT_TIME:
  101. time_printf("The struct-time's range is wrong !\n");
  102. break;
  103. default:
  104. break;
  105. }
  106. time_printf("\n");
  107. }
  108. // 获取硬件平台的Unix时间戳,时间精度为1s级别,
  109. status time_get_unix_time(PikaObj* self, _timespec* this_timespec) {
  110. this_timespec->tv_sec = (int64_t)(obj_getInt(self, "tick") / 1000);
  111. return TIME_OK;
  112. }
  113. // 获取硬件平台的Tick时间,时间精度为1s级别以下
  114. // 即1s的小数部分
  115. status time_get_tick_ns(PikaObj* self, _timespec* this_timespec) {
  116. this_timespec->tv_nsec = (obj_getInt(self, "tick") % 1000) * 1000000;
  117. return TIME_OK;
  118. }
  119. // 标准time()方法,返回以浮点数表示的从 epoch 开始的秒数的时间值。
  120. // epoch 是 1970 年 1 月 1 日 00:00:00 (UTC),
  121. pika_float time_time(PikaObj* self) {
  122. status res = 0; // 状态响应
  123. _timespec temp_timespec = {0};
  124. // 调用硬件平台函数,获取当前时间
  125. res = time_get_unix_time(self, &temp_timespec);
  126. if (res) {
  127. status_deal(res);
  128. } // 异常处理
  129. res = time_get_tick_ns(self, &temp_timespec);
  130. if (res) {
  131. status_deal(res);
  132. } // 异常处理
  133. // 以浮点数返回时间,float
  134. return temp_timespec.tv_sec +
  135. (pika_float)temp_timespec.tv_nsec / 1000000000;
  136. }
  137. // 标准time_ns()方法,返回以整数表示的从 epoch 开始的纳秒数的时间值。
  138. // epoch 是 1970 年 1 月 1 日 00:00:00 (UTC),
  139. int64_t time_time_ns(PikaObj* self) {
  140. status res = 0; // 状态响应
  141. _timespec temp_timespec = {0};
  142. // 调用硬件平台函数,获取当前时间
  143. res = time_get_unix_time(self, &temp_timespec);
  144. if (res) {
  145. status_deal(res);
  146. } // 异常处理
  147. res = time_get_tick_ns(self, &temp_timespec);
  148. if (res) {
  149. status_deal(res);
  150. } // 异常处理
  151. // 以浮点数返回时间,float
  152. return temp_timespec.tv_sec * 1000000000 + temp_timespec.tv_nsec;
  153. }
  154. // 利用基姆拉尔森计算公式计算星期
  155. int time_get_week(const _tm* this_tm) {
  156. // 月份要+1
  157. int month = this_tm->tm_mon + 1;
  158. int year = this_tm->tm_year;
  159. int day = this_tm->tm_mday;
  160. int w;
  161. if (month == 1 || month == 2) {
  162. month += 12;
  163. year -= 1;
  164. w = day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 -
  165. year / 100 + year / 400 + 1; // 0~6,星期日 ~ 星期六
  166. w = w % 7;
  167. } else {
  168. w = day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 -
  169. year / 100 + year / 400 + 1; // 0~6,星期日 ~ 星期六
  170. w = w % 7;
  171. }
  172. return w;
  173. }
  174. // 由Unix时间戳计算标准UTC时间
  175. status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) {
  176. int32_t total_day;
  177. int32_t extra_second;
  178. int year_400, year_100, year_4, year_1;
  179. int february_offset, temp; // 二月偏移量,零时变量
  180. // 判断是否输入小于0的时间戳
  181. if (unix_time < 0) {
  182. // 暂不支持小于0的时间戳
  183. return TIME_LESS_THAN_ZERO;
  184. }
  185. // Unix时间戳每天秒数是固定的 62*60*24
  186. #define DAY_SECOND (86400)
  187. total_day = unix_time / DAY_SECOND;
  188. extra_second = unix_time - total_day * DAY_SECOND;
  189. // 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断
  190. // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年
  191. // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数
  192. #define YEAR_START (1600) // 初始年份
  193. #define DAY_OFFSET (135140) // 时间偏移量
  194. total_day += DAY_OFFSET;
  195. // 从1600年到3200年有1600/4-(1600/100-1600/400)=388个闰年
  196. // 即 MAX_DAY 1600*365+388=584388 day
  197. #define MAX_DAY (584388) // 最大可判断时间天数
  198. if (total_day > MAX_DAY) {
  199. // 超过3200年的换算暂不支持
  200. return TIME_OVER_3200;
  201. } else {
  202. // 从1600年开始,天数都要多减一天,因为1600年是闰年
  203. // 但是由于日期不包含当天时间,即2月2号,实际是2月1号+时:分:秒
  204. // 所以算出来的日期要加上一天
  205. // 两者配合,无需加减
  206. // 从400年,100年,4年逐渐缩小范围
  207. // 400个公历年天数为365*400+97=146097天
  208. // 400年内的100个公历年天数为365*100+24=36524天
  209. // 100年内的4年365*4+1=1461天
  210. #define DAY_OF_400Y (146097)
  211. #define DAY_OF_100Y (36524)
  212. #define DAY_OF_4Y (1461)
  213. #define DAY_OF_1Y (365)
  214. // 400年也要注意,要实际401年才可
  215. year_400 = (total_day - 366) / DAY_OF_400Y;
  216. total_day -= year_400 * DAY_OF_400Y;
  217. // 计算400年内的情况
  218. year_100 = (total_day - 1) / DAY_OF_100Y;
  219. total_day -= year_100 * DAY_OF_100Y;
  220. // 计算100年内的情况,要到第二年的第一天才算,即365+1
  221. year_4 = (total_day - 366) / DAY_OF_4Y;
  222. // 计算4年,需要格外注意0-5-8年,才会计算一个闰年,因为它才包含了4这个闰年,但并不包含8
  223. total_day -= year_4 * DAY_OF_4Y;
  224. // 计算4年内的情况
  225. // 需要减去1天,因为当天是不存在的
  226. // 需要注意闰年会多一天
  227. // 所有闰年都放在这里来考虑,即只要当前是闰年,那么这里就会剩下第一年闰年和第四年闰年两种情况
  228. if (year_100 == 4) {
  229. // 第一年是闰年,此时为400*n+1年内
  230. year_1 = 0;
  231. february_offset = 1;
  232. } else if (total_day <= DAY_OF_1Y * 4) {
  233. // 100*n+(4,8,...96)+1年,都是从第二年算起,非闰年
  234. // 非闰年,需要减去1天,因为当天是不存在的
  235. year_1 = (total_day - 1) / DAY_OF_1Y;
  236. total_day -= year_1 * DAY_OF_1Y;
  237. february_offset = 0;
  238. } else {
  239. // 第四年是闰年
  240. year_1 = 4;
  241. total_day -= year_1 * DAY_OF_1Y;
  242. february_offset = 1;
  243. }
  244. // 计算出当前年份
  245. this_tm->tm_year =
  246. (year_400 * 400 + year_100 * 100 + year_4 * 4 + year_1) +
  247. YEAR_START;
  248. // 保存一年的天数
  249. this_tm->tm_yday = total_day;
  250. // 剩下的天数为1年内的天数,直接计算月和日
  251. // 根据当前是否为闰年设置二月偏移量是否为1
  252. // 能被4整除且不被100整除或者能被400整除
  253. // 闰年需要减去一天再计算
  254. total_day -= february_offset;
  255. // 使用二分法快速定位月份,使用平年计算,在月份确定到2月时,再考虑闰年
  256. // 判断是否在1-6月里面
  257. // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  258. if (total_day <= 181) {
  259. // 判断是否在1-3月里面
  260. if (total_day <= 90) {
  261. // 判断是否在1-2月里面
  262. if (total_day <= 59) {
  263. total_day += february_offset; // 去掉二月的偏置
  264. if (total_day <= 31) {
  265. // 1月
  266. temp = 0;
  267. } else {
  268. total_day -= 31;
  269. // 2月
  270. temp = 1;
  271. }
  272. } else {
  273. total_day -= 59;
  274. // 3月
  275. temp = 2;
  276. }
  277. } else {
  278. // 4-6月
  279. total_day -= 90;
  280. // 是否在4月里面
  281. if (total_day <= 30) {
  282. // 4月
  283. temp = 3;
  284. } else {
  285. // 5-6月
  286. total_day -= 30;
  287. if (total_day <= 31) {
  288. // 5月
  289. temp = 4;
  290. } else {
  291. total_day -= 31;
  292. // 6月
  293. temp = 5;
  294. }
  295. }
  296. }
  297. } else {
  298. total_day -= 181;
  299. // 判断是否在7-9月里面
  300. if (total_day <= 92) {
  301. // 是否在7-8月
  302. if (total_day <= 62) {
  303. if (total_day <= 31) {
  304. // 7月
  305. temp = 6;
  306. } else {
  307. total_day -= 31;
  308. // 8月
  309. temp = 7;
  310. }
  311. } else {
  312. // 9月
  313. total_day -= 62;
  314. temp = 8;
  315. }
  316. } else {
  317. // 10-12月
  318. total_day -= 92;
  319. // 是否在10-11月
  320. if (total_day <= 61) {
  321. if (total_day <= 31) {
  322. // 10月
  323. temp = 9;
  324. } else {
  325. // 11 月
  326. total_day -= 31;
  327. temp = 10;
  328. }
  329. } else {
  330. // 12月
  331. total_day -= 61;
  332. temp = 11;
  333. }
  334. }
  335. }
  336. // 记录当前月份和天数
  337. this_tm->tm_mon = temp; // 月份 [0,11]
  338. this_tm->tm_mday = total_day; // 天数
  339. // 利用额外秒数计算时-分-秒
  340. temp = extra_second / 3600;
  341. this_tm->tm_hour = temp;
  342. extra_second = extra_second - temp * 3600;
  343. temp = extra_second / 60;
  344. this_tm->tm_min = temp;
  345. extra_second = extra_second - temp * 60;
  346. this_tm->tm_sec = extra_second;
  347. // 计算出当前日期的星期数
  348. this_tm->tm_wday = time_get_week(this_tm);
  349. // 夏令时不明
  350. this_tm->tm_isdst = -1;
  351. }
  352. return TIME_OK;
  353. }
  354. // 由标准UTC时间生成Unix时间戳
  355. status utc_struct_time_to_unix_time(const _tm* this_tm, int64_t* unix_time) {
  356. int32_t total_day, total_leap_year, dyear;
  357. int february_offset; // 二月偏移量,零时变量
  358. // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  359. // 每个月份对应前面所有月的天数
  360. const int month_day[] = {0, 31, 59, 90, 120, 151,
  361. 181, 212, 243, 273, 304, 334};
  362. // 每天总秒数一定,将UTC时间(年月)转换成天数
  363. // 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断
  364. // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年
  365. // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数
  366. if (this_tm->tm_year < 1970) {
  367. // 暂不支持1970之前的时间
  368. *unix_time = 0;
  369. return TIME_LESS_THAN_1970;
  370. }
  371. if (this_tm->tm_year >= 3200) {
  372. // 暂不支持3200及以后的时间
  373. *unix_time = 0;
  374. return TIME_OVER_3200;
  375. }
  376. // 计算总年数要去掉尾巴,如年数20年,那么实际应该4个闰年,因为20这一年没有包含在里面
  377. // 要减去一年来算闰年次数
  378. // 先计算到相对1600年的天数,再转换到1970年
  379. dyear = this_tm->tm_year - YEAR_START - 1;
  380. total_leap_year = dyear / 4 - (dyear / 100 - dyear / 400 - 1);
  381. // 恢复减去的一年
  382. dyear += 1;
  383. total_day = dyear * 365 + total_leap_year;
  384. // 减去1970到1600的总天数
  385. total_day -= DAY_OFFSET;
  386. // 增加月和日的总天数
  387. // 判断是否是闰年
  388. // 能被4整除且不被100整除或者能被400整除
  389. if (((dyear % 4 == 0) && (dyear % 100 != 0)) || (dyear % 400 == 0)) {
  390. // 闰年
  391. february_offset = 1;
  392. } else {
  393. february_offset = 0;
  394. }
  395. // 计算含月和日的总天数,日期要减去当天
  396. total_day += month_day[this_tm->tm_mon] + this_tm->tm_mday - 1;
  397. // 二月以上需要加上偏移量
  398. if (this_tm->tm_mon > 1) {
  399. total_day += february_offset;
  400. }
  401. // 根据天数以及时分秒计算Unix时间戳
  402. *unix_time = (int64_t)total_day * DAY_SECOND + this_tm->tm_hour * 3600 +
  403. this_tm->tm_min * 60 + this_tm->tm_sec;
  404. return TIME_OK;
  405. }
  406. void time_struct_format(const _tm* this_tm, char* str) {
  407. sprintf(str,
  408. "time.struct_time(tm_year=%d, tm_mon=%d,tm_mday=%d, tm_hour=%d, "
  409. "tm_min=%d, tm_sec=%d, tm_wday=%d,tm_yday=%d, tm_isdst=%d)",
  410. this_tm->tm_year, this_tm->tm_mon + 1, this_tm->tm_mday,
  411. this_tm->tm_hour, this_tm->tm_min, this_tm->tm_sec,
  412. this_tm->tm_wday, this_tm->tm_yday, this_tm->tm_isdst);
  413. }
  414. // 标准库函数gmtime,将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time
  415. void time_gmtime(pika_float unix_time, _tm* this_tm) {
  416. status res;
  417. // 转化时间
  418. res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time);
  419. if (res) {
  420. status_deal(res); // 异常情况处理
  421. // 返回默认值
  422. // note: 异常情况返回默认时间起始点
  423. unix_time_to_utc_struct_time(this_tm, (int64_t)0);
  424. }
  425. }
  426. // 标准库函数localtime,将以自 epoch 开始的秒数表示的时间转换为当地时间的
  427. // struct_time
  428. void time_localtime(pika_float unix_time, _tm* this_tm, int locale) {
  429. status res;
  430. int local_offset;
  431. // 获取本地时间偏移量(小时)
  432. local_offset = locale * 60 * 60;
  433. // 转化时间
  434. res = unix_time_to_utc_struct_time(this_tm,
  435. (int64_t)unix_time + local_offset);
  436. if (res) {
  437. status_deal(res); // 异常情况处理
  438. // 这里处理的策略和标准库不同,标准库最初始的时间是1970-1-1,00:00:00,对于不同时区来说,其值是不一样的
  439. // 但本函数是要求各时区的起始时间不超过1970-1-1,00:00:00,实际上UTC时间可以更前,可靠的最早时间可到1600年
  440. // 对于西时区来说,时间会缺失
  441. unix_time_to_utc_struct_time(this_tm, (int64_t)0);
  442. }
  443. }
  444. // 检测结构体时间是否在合适的范围内,但不检查它的正确性
  445. status time_check_struct_time(const _tm* this_tm) {
  446. if (this_tm->tm_sec < 0 || this_tm->tm_sec > 60) {
  447. return TIME_ERROR_STRUCT_TIME;
  448. }
  449. if (this_tm->tm_min < 0 || this_tm->tm_min > 59) {
  450. return TIME_ERROR_STRUCT_TIME;
  451. }
  452. if (this_tm->tm_hour < 0 || this_tm->tm_hour > 23) {
  453. return TIME_ERROR_STRUCT_TIME;
  454. }
  455. if (this_tm->tm_mday < 1 || this_tm->tm_mday > 31) {
  456. return TIME_ERROR_STRUCT_TIME;
  457. }
  458. if (this_tm->tm_mon < 0 || this_tm->tm_mon > 11) {
  459. return TIME_ERROR_STRUCT_TIME;
  460. }
  461. if (this_tm->tm_wday < 0 || this_tm->tm_wday > 6) {
  462. return TIME_ERROR_STRUCT_TIME;
  463. }
  464. if (this_tm->tm_yday < 0 || this_tm->tm_yday > 366) {
  465. return TIME_ERROR_STRUCT_TIME;
  466. }
  467. return TIME_OK;
  468. }
  469. // 标准库函数mktime(t),将当地时间的
  470. // struct_time转换为以自epoch开始的秒数表示的时间
  471. int64_t time_mktime(const _tm* this_tm, int locale) {
  472. status res;
  473. int local_offset;
  474. int64_t unix_time;
  475. // 获取本地时间偏移量(小时)
  476. local_offset = locale * 60 * 60;
  477. // 检测时间结构体范围正确性
  478. res = time_check_struct_time(this_tm);
  479. if (res) {
  480. status_deal(res);
  481. return 0;
  482. } // 异常情况返回时间零点
  483. // 转化时间
  484. res = utc_struct_time_to_unix_time(this_tm, &unix_time);
  485. if (res) {
  486. status_deal(res);
  487. return 0;
  488. } // 异常情况返回时间零点
  489. // 减去本地偏移时间
  490. // 可能出现负数,严格来说,这不影响什么!
  491. unix_time -= local_offset;
  492. // 显示出来
  493. // time_printf("%I64d\n",unix_time);
  494. // 返回数据
  495. return unix_time;
  496. }
  497. // 标准库函数asctime()
  498. // 把结构化时间struct_time元组表示为以下形式的字符串: `'Sun Jun 20 23:21:05
  499. // 1993'`。
  500. void time_asctime(const _tm* this_tm) {
  501. // 星期缩写,python标准库是三个字母,这里并不相同
  502. const char* week[] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
  503. // 月份缩写
  504. const char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  505. "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
  506. char str[100];
  507. sprintf(str, "%s %s %d %02d:%02d:%02d %d", week[this_tm->tm_wday],
  508. month[this_tm->tm_mon], this_tm->tm_mday, this_tm->tm_hour,
  509. this_tm->tm_min, this_tm->tm_sec, this_tm->tm_year);
  510. time_printf("%s\n", str);
  511. }
  512. pika_float _time_time(PikaObj* self) {
  513. /* run platformGetTick() */
  514. PIKA_PYTHON_BEGIN
  515. /* clang-format off */
  516. PIKA_PYTHON(
  517. platformGetTick()
  518. )
  519. /* clang-format on */
  520. const uint8_t bytes[] = {
  521. 0x04, 0x00, 0x00, 0x00, /* instruct array size */
  522. 0x00, 0x82, 0x01, 0x00, /* instruct array */
  523. 0x11, 0x00, 0x00, 0x00, /* const pool size */
  524. 0x00, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
  525. 0x47, 0x65, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x00, /* const pool */
  526. };
  527. PIKA_PYTHON_END
  528. pikaVM_runByteCode(self, (uint8_t*)bytes);
  529. return time_time(self);
  530. }
  531. int _time_time_ns(PikaObj* self) {
  532. return time_time_ns(self);
  533. }
  534. void time_set_tm_value(PikaObj* self, const _tm* this_tm) {
  535. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  536. obj_setErrorCode(self, 1);
  537. obj_setSysOut(
  538. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  539. #else
  540. obj_setInt(self, "tm_sec", this_tm->tm_sec);
  541. obj_setInt(self, "tm_min", this_tm->tm_min);
  542. obj_setInt(self, "tm_hour", this_tm->tm_hour);
  543. obj_setInt(self, "tm_mday", this_tm->tm_mday);
  544. obj_setInt(self, "tm_mon", this_tm->tm_mon);
  545. obj_setInt(self, "tm_year", this_tm->tm_year);
  546. obj_setInt(self, "tm_wday", this_tm->tm_wday);
  547. obj_setInt(self, "tm_yday", this_tm->tm_yday);
  548. obj_setInt(self, "tm_isdst", this_tm->tm_isdst);
  549. #endif
  550. }
  551. void _time_gmtime(PikaObj* self, pika_float unix_time) {
  552. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  553. obj_setErrorCode(self, 1);
  554. obj_setSysOut(
  555. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  556. #else
  557. _tm this_tm;
  558. char str[200];
  559. time_gmtime(unix_time, &this_tm);
  560. time_set_tm_value(self, &this_tm);
  561. // 格式化字符
  562. time_struct_format(&this_tm, str);
  563. // 显示出来
  564. time_printf("%s\n", str);
  565. #endif
  566. }
  567. void _time_localtime(PikaObj* self, pika_float unix_time) {
  568. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  569. obj_setErrorCode(self, 1);
  570. obj_setSysOut(
  571. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  572. #else
  573. _tm this_tm;
  574. char str[200];
  575. int locale = g_pika_local_timezone;
  576. time_localtime(unix_time, &this_tm, locale);
  577. time_set_tm_value(self, &this_tm);
  578. // 格式化字符
  579. time_struct_format(&this_tm, str);
  580. // 显示出来
  581. time_printf("%s\n", str);
  582. #endif
  583. }
  584. void time_get_tm_value(PikaObj* self, _tm* this_tm) {
  585. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  586. obj_setErrorCode(self, 1);
  587. obj_setSysOut(
  588. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  589. #else
  590. this_tm->tm_sec = obj_getInt(self, "tm_sec");
  591. this_tm->tm_min = obj_getInt(self, "tm_min");
  592. this_tm->tm_hour = obj_getInt(self, "tm_hour");
  593. this_tm->tm_mday = obj_getInt(self, "tm_mday");
  594. this_tm->tm_mon = obj_getInt(self, "tm_mon");
  595. this_tm->tm_year = obj_getInt(self, "tm_year");
  596. this_tm->tm_wday = obj_getInt(self, "tm_wday");
  597. this_tm->tm_yday = obj_getInt(self, "tm_yday");
  598. this_tm->tm_isdst = obj_getInt(self, "tm_isdst");
  599. #endif
  600. }
  601. int _time_mktime(PikaObj* self) {
  602. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  603. obj_setErrorCode(self, 1);
  604. obj_setSysOut(
  605. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  606. return 0;
  607. #else
  608. _tm this_tm;
  609. int locale = g_pika_local_timezone;
  610. time_get_tm_value(self, &this_tm);
  611. return time_mktime(&this_tm, locale);
  612. #endif
  613. }
  614. void _time_asctime(PikaObj* self) {
  615. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  616. obj_setErrorCode(self, 1);
  617. obj_setSysOut(
  618. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  619. #else
  620. _time_ctime(self, _time_time(self));
  621. #endif
  622. }
  623. void _time_ctime(PikaObj* self, pika_float unix_time) {
  624. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  625. obj_setErrorCode(self, 1);
  626. obj_setSysOut(
  627. self, "[error] PIKA_STD_DEVICE_UNIX_TIME_ENABLE need to be enable.");
  628. #else
  629. _tm this_tm;
  630. int locale = g_pika_local_timezone;
  631. time_localtime(unix_time, &this_tm, locale);
  632. time_asctime(&this_tm);
  633. #endif
  634. }
  635. void _time___init__(PikaObj* self) {
  636. if (-1 == pika_platform_get_tick()) {
  637. global_do_sleep_ms = pika_platform_sleep_ms;
  638. } else {
  639. global_do_sleep_ms = _do_sleep_ms_tick;
  640. }
  641. #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
  642. #else
  643. _tm this_tm;
  644. g_pika_local_timezone = 8;
  645. time_localtime(0.0, &this_tm, 8);
  646. time_set_tm_value(self, &this_tm);
  647. #endif
  648. }