display_time.c 777 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdio.h>
  2. #define PY_SSIZE_T_CLEAN
  3. #include <Python.h>
  4. #include "display_time.h"
  5. void display_time()
  6. {
  7. #if defined(PYTHON3)
  8. wchar_t* program = Py_DecodeLocale("display_time", NULL);
  9. if (program == NULL) {
  10. fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
  11. exit(1);
  12. }
  13. char* cmd = "from time import time,ctime\n"
  14. "print('Today is', ctime(time()))\n";
  15. #else
  16. char* program = "display_time";
  17. char* cmd = "from time import time,ctime\n"
  18. "print 'Today is', ctime(time())\n";
  19. #endif
  20. Py_SetProgramName(program); /* optional but recommended */
  21. Py_Initialize();
  22. PyRun_SimpleString(cmd);
  23. #if defined(PYTHON3)
  24. if (Py_FinalizeEx() < 0) {
  25. exit(120);
  26. }
  27. PyMem_RawFree(program);
  28. #else
  29. Py_Finalize();
  30. #endif
  31. }