_time.c 18 KB

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