_time.c 24 KB

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