tm_cvt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/cc/torint.h"
  7. #include "lib/cc/compat_compiler.h"
  8. #include "lib/wallclock/tm_cvt.h"
  9. #include "lib/string/printf.h"
  10. #include <errno.h>
  11. #include <time.h>
  12. #include <string.h>
  13. #if !defined(_WIN32)
  14. /** Defined iff we need to add locks when defining fake versions of reentrant
  15. * versions of time-related functions. */
  16. #define TIME_FNS_NEED_LOCKS
  17. #endif
  18. /** Helper: Deal with confused or out-of-bounds values from localtime_r and
  19. * friends. (On some platforms, they can give out-of-bounds values or can
  20. * return NULL.) If <b>islocal</b>, this is a localtime result; otherwise
  21. * it's from gmtime. The function returns <b>r</b>, when given <b>timep</b>
  22. * as its input. If we need to store new results, store them in
  23. * <b>resultbuf</b>. */
  24. static struct tm *
  25. correct_tm(int islocal, const time_t *timep, struct tm *resultbuf,
  26. struct tm *r, char **err_out)
  27. {
  28. const char *outcome;
  29. if (PREDICT_LIKELY(r)) {
  30. /* We can't strftime dates after 9999 CE, and we want to avoid dates
  31. * before 1 CE (avoiding the year 0 issue and negative years). */
  32. if (r->tm_year > 8099) {
  33. r->tm_year = 8099;
  34. r->tm_mon = 11;
  35. r->tm_mday = 31;
  36. r->tm_yday = 364;
  37. r->tm_wday = 6;
  38. r->tm_hour = 23;
  39. r->tm_min = 59;
  40. r->tm_sec = 59;
  41. } else if (r->tm_year < (1-1900)) {
  42. r->tm_year = (1-1900);
  43. r->tm_mon = 0;
  44. r->tm_mday = 1;
  45. r->tm_yday = 0;
  46. r->tm_wday = 0;
  47. r->tm_hour = 0;
  48. r->tm_min = 0;
  49. r->tm_sec = 0;
  50. }
  51. return r;
  52. }
  53. /* If we get here, gmtime or localtime returned NULL. It might have done
  54. * this because of overrun or underrun, or it might have done it because of
  55. * some other weird issue. */
  56. if (timep) {
  57. if (*timep < 0) {
  58. r = resultbuf;
  59. r->tm_year = 70; /* 1970 CE */
  60. r->tm_mon = 0;
  61. r->tm_mday = 1;
  62. r->tm_yday = 0;
  63. r->tm_wday = 0;
  64. r->tm_hour = 0;
  65. r->tm_min = 0 ;
  66. r->tm_sec = 0;
  67. outcome = "Rounding up to 1970";
  68. goto done;
  69. } else if (*timep >= INT32_MAX) {
  70. /* Rounding down to INT32_MAX isn't so great, but keep in mind that we
  71. * only do it if gmtime/localtime tells us NULL. */
  72. r = resultbuf;
  73. r->tm_year = 137; /* 2037 CE */
  74. r->tm_mon = 11;
  75. r->tm_mday = 31;
  76. r->tm_yday = 364;
  77. r->tm_wday = 6;
  78. r->tm_hour = 23;
  79. r->tm_min = 59;
  80. r->tm_sec = 59;
  81. outcome = "Rounding down to 2037";
  82. goto done;
  83. }
  84. }
  85. /* If we get here, then gmtime/localtime failed without getting an extreme
  86. * value for *timep */
  87. /* LCOV_EXCL_START */
  88. r = resultbuf;
  89. memset(resultbuf, 0, sizeof(struct tm));
  90. outcome="can't recover";
  91. /* LCOV_EXCL_STOP */
  92. done:
  93. if (err_out) {
  94. tor_asprintf(err_out, "%s("I64_FORMAT") failed with error %s: %s",
  95. islocal?"localtime":"gmtime",
  96. timep?I64_PRINTF_ARG(*timep):0,
  97. strerror(errno),
  98. outcome);
  99. }
  100. return r;
  101. }
  102. /** @{ */
  103. /** As localtime_r, but defined for platforms that don't have it:
  104. *
  105. * Convert *<b>timep</b> to a struct tm in local time, and store the value in
  106. * *<b>result</b>. Return the result on success, or NULL on failure.
  107. */
  108. #ifdef HAVE_LOCALTIME_R
  109. struct tm *
  110. tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  111. {
  112. struct tm *r;
  113. r = localtime_r(timep, result);
  114. return correct_tm(1, timep, result, r, err_out);
  115. }
  116. #elif defined(TIME_FNS_NEED_LOCKS)
  117. struct tm *
  118. tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  119. {
  120. struct tm *r;
  121. static tor_mutex_t *m=NULL;
  122. if (!m) { m=tor_mutex_new(); }
  123. raw_assert(result);
  124. tor_mutex_acquire(m);
  125. r = localtime(timep);
  126. if (r)
  127. memcpy(result, r, sizeof(struct tm));
  128. tor_mutex_release(m);
  129. return correct_tm(1, timep, result, r, err_out);
  130. }
  131. #else
  132. struct tm *
  133. tor_localtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  134. {
  135. struct tm *r;
  136. raw_assert(result);
  137. r = localtime(timep);
  138. if (r)
  139. memcpy(result, r, sizeof(struct tm));
  140. return correct_tm(1, timep, result, rm, err_out);
  141. }
  142. #endif /* defined(HAVE_LOCALTIME_R) || ... */
  143. /** @} */
  144. /** @{ */
  145. /** As gmtime_r, but defined for platforms that don't have it:
  146. *
  147. * Convert *<b>timep</b> to a struct tm in UTC, and store the value in
  148. * *<b>result</b>. Return the result on success, or NULL on failure.
  149. */
  150. #ifdef HAVE_GMTIME_R
  151. struct tm *
  152. tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  153. {
  154. struct tm *r;
  155. r = gmtime_r(timep, result);
  156. return correct_tm(0, timep, result, r, err_out);
  157. }
  158. #elif defined(TIME_FNS_NEED_LOCKS)
  159. struct tm *
  160. tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  161. {
  162. struct tm *r;
  163. static tor_mutex_t *m=NULL;
  164. if (!m) { m=tor_mutex_new(); }
  165. raw_assert(result);
  166. tor_mutex_acquire(m);
  167. r = gmtime(timep);
  168. if (r)
  169. memcpy(result, r, sizeof(struct tm));
  170. tor_mutex_release(m);
  171. return correct_tm(0, timep, result, r, err_out);
  172. }
  173. #else
  174. struct tm *
  175. tor_gmtime_r_msg(const time_t *timep, struct tm *result, char **err_out)
  176. {
  177. struct tm *r;
  178. raw_assert(result);
  179. r = gmtime(timep);
  180. if (r)
  181. memcpy(result, r, sizeof(struct tm));
  182. return correct_tm(0, timep, result, r, err_out);
  183. }
  184. #endif /* defined(HAVE_GMTIME_R) || ... */