htime.cpp 1.4 KB

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