1
0

htime.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "htime.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. void msleep(unsigned long ms) {
  5. #ifdef _WIN32
  6. Sleep(ms);
  7. #else
  8. usleep(ms*1000);
  9. #endif
  10. }
  11. uint64 gettick() {
  12. #ifdef _WIN32
  13. return GetTickCount();
  14. #else
  15. struct timeval tv;
  16. gettimeofday(&tv, NULL);
  17. return tv.tv_sec*1000 + tv.tv_usec/1000;
  18. #endif
  19. }
  20. datetime_t get_datetime() {
  21. datetime_t dt;
  22. #ifdef _WIN32
  23. SYSTEMTIME tm;
  24. GetLocalTime(&tm);
  25. dt.year = tm.wYear;
  26. dt.month = tm.wMonth;
  27. dt.day = tm.wDay;
  28. dt.hour = tm.wHour;
  29. dt.min = tm.wMinute;
  30. dt.sec = tm.wSecond;
  31. dt.ms = tm.wMilliseconds;
  32. #else
  33. struct timeval tv;
  34. struct tm* tm = NULL;
  35. gettimeofday(&tv, NULL);
  36. time_t tt = tv.tv_sec;
  37. tm = localtime(&tt);
  38. dt.year = tm->tm_year + 1900;
  39. dt.month = tm->tm_mon + 1;
  40. dt.day = tm->tm_mday;
  41. dt.hour = tm->tm_hour;
  42. dt.min = tm->tm_min;
  43. dt.sec = tm->tm_sec;
  44. dt.ms = tv.tv_usec/1000;
  45. #endif
  46. return dt;
  47. }
  48. static const char* s_month[] = {"January", "February", "March", "April", "May", "June",
  49. "July", "August", "September", "October", "November", "December"};
  50. int month_atoi(const char* month) {
  51. for (int i = 0; i < ARRAY_SIZE(s_month); ++i) {
  52. if (strnicmp(month, s_month[i], strlen(month)) == 0)
  53. return i+1;
  54. }
  55. return 0;
  56. }
  57. const char* month_itoa(int month) {
  58. if (month < 1 || month > 12) {
  59. return NULL;
  60. }
  61. return s_month[month-1];
  62. }
  63. datetime_t get_compile_datetime() {
  64. static datetime_t dt;
  65. char month[32];
  66. sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);
  67. sscanf(__TIME__, "%d %d %d", &dt.hour, &dt.min, &dt.sec);
  68. dt.month = month_atoi(month);
  69. return dt;
  70. }