time.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "xmlrpc_config.h"
  2. #include <assert.h>
  3. #include <time.h>
  4. #if !MSVCRT
  5. #include <sys/time.h>
  6. #endif
  7. #if MSVCRT
  8. #include <windows.h>
  9. #endif
  10. #include "xmlrpc-c/string_int.h"
  11. #include "xmlrpc-c/time_int.h"
  12. /* A note about struct timeval and Windows: There is a 'struct
  13. timeval' type in Windows, but it is just an argument to select(),
  14. which is just part of the sockets interface. It's defined
  15. identically to the POSIX type of the same name, but not meant for
  16. general timekeeping as the POSIX type is.
  17. */
  18. #if HAVE_GETTIMEOFDAY
  19. static void
  20. gettimeofdayPosix(xmlrpc_timespec * const todP) {
  21. struct timeval tv;
  22. gettimeofday(&tv, NULL);
  23. todP->tv_sec = tv.tv_sec;
  24. todP->tv_nsec = tv.tv_usec * 1000;
  25. }
  26. #endif
  27. #if MSVCRT
  28. static void
  29. gettimeofdayWindows(xmlrpc_timespec * const todP) {
  30. __int64 const epochOffset = 116444736000000000i64;
  31. /* Number of 100-nanosecond units between the beginning of the
  32. Windows epoch (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
  33. */
  34. FILETIME ft;
  35. LARGE_INTEGER li;
  36. __int64 t;
  37. GetSystemTimeAsFileTime(&ft);
  38. li.LowPart = ft.dwLowDateTime;
  39. li.HighPart = ft.dwHighDateTime;
  40. t = (li.QuadPart - epochOffset) * 100; /* nanoseconds */
  41. todP->tv_sec = (long)(t / 1E9);
  42. todP->tv_nsec = (long)(t - (__int64)todP->tv_sec * 1E9);
  43. }
  44. #endif
  45. void
  46. xmlrpc_gettimeofday(xmlrpc_timespec * const todP) {
  47. assert(todP);
  48. #if HAVE_GETTIMEOFDAY
  49. gettimeofdayPosix(todP);
  50. #else
  51. #if MSVCRT
  52. gettimeofdayWindows(todP);
  53. #else
  54. #error "We don't know how to get the time of day on this system"
  55. #endif
  56. #endif /* HAVE_GETTIMEOFDAY */
  57. }
  58. static bool
  59. isLeapYear(unsigned int const yearOfAd) {
  60. return
  61. (yearOfAd % 4) == 0 &&
  62. ((yearOfAd % 100) != 0 || (yearOfAd % 400) == 0);
  63. }
  64. void
  65. xmlrpc_timegm(const struct tm * const tmP,
  66. time_t * const timeValueP,
  67. const char ** const errorP) {
  68. /*----------------------------------------------------------------------------
  69. This does what GNU libc's timegm() does.
  70. -----------------------------------------------------------------------------*/
  71. if (tmP->tm_year < 70 ||
  72. tmP->tm_mon > 11 ||
  73. tmP->tm_mon < 0 ||
  74. tmP->tm_mday > 31 ||
  75. tmP->tm_min > 60 ||
  76. tmP->tm_sec > 60 ||
  77. tmP->tm_hour > 24) {
  78. xmlrpc_asprintf(errorP, "Invalid time specification; a member "
  79. "of struct tm is out of range");
  80. } else {
  81. static unsigned int const monthDaysNonLeap[12] =
  82. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  83. unsigned int totalDays;
  84. unsigned int year;
  85. unsigned int month;
  86. totalDays = 0; /* initial value */
  87. for (year = 70; year < (unsigned int)tmP->tm_year; ++year)
  88. totalDays += isLeapYear(1900 + year) ? 366 : 365;
  89. for (month = 0; month < (unsigned int)tmP->tm_mon; ++month)
  90. totalDays += monthDaysNonLeap[month];
  91. if (tmP->tm_mon > 1 && isLeapYear(1900 + tmP->tm_year))
  92. totalDays += 1;
  93. totalDays += tmP->tm_mday - 1;
  94. *errorP = NULL;
  95. *timeValueP = ((totalDays * 24 +
  96. tmP->tm_hour) * 60 +
  97. tmP->tm_min) * 60 +
  98. tmP->tm_sec;
  99. }
  100. }
  101. void
  102. xmlrpc_localtime(time_t const datetime,
  103. struct tm * const tmP) {
  104. /*----------------------------------------------------------------------------
  105. Convert datetime from standard to broken-down format in the local
  106. time zone.
  107. For Windows, this is not thread-safe. If you run a version of Abyss
  108. with multiple threads, you can get arbitrary results here.
  109. -----------------------------------------------------------------------------*/
  110. #if HAVE_LOCALTIME_R
  111. localtime_r(&datetime, tmP);
  112. #else
  113. *tmP = *localtime(&datetime);
  114. #endif
  115. }
  116. void
  117. xmlrpc_gmtime(time_t const datetime,
  118. struct tm * const resultP) {
  119. /*----------------------------------------------------------------------------
  120. Convert datetime from standard to broken-down UTC format.
  121. For Windows, this is not thread-safe. If you run a version of Abyss
  122. with multiple threads, you can get arbitrary results here.
  123. -----------------------------------------------------------------------------*/
  124. #if HAVE_GMTIME_R
  125. gmtime_r(&datetime, resultP);
  126. #else
  127. *resultP = *gmtime(&datetime);
  128. #endif
  129. }