_time.c 24 KB

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