htime.cpp 1.5 KB

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