htime.cpp 1.8 KB

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