_time.c 24 KB

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