o_time.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/e_os2.h>
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
  13. {
  14. struct tm *ts = NULL;
  15. #if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
  16. {
  17. /*
  18. * On VMS, gmtime_r() takes a 32-bit pointer as second argument.
  19. * Since we can't know that |result| is in a space that can easily
  20. * translate to a 32-bit pointer, we must store temporarily on stack
  21. * and copy the result. The stack is always reachable with 32-bit
  22. * pointers.
  23. */
  24. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE
  25. # pragma pointer_size save
  26. # pragma pointer_size 32
  27. #endif
  28. struct tm data, *ts2 = &data;
  29. #if defined OPENSSL_SYS_VMS && __INITIAL_POINTER_SIZE
  30. # pragma pointer_size restore
  31. #endif
  32. if (gmtime_r(timer, ts2) == NULL)
  33. return NULL;
  34. memcpy(result, ts2, sizeof(struct tm));
  35. ts = result;
  36. }
  37. #elif defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_MACOSX)
  38. if (gmtime_r(timer, result) == NULL)
  39. return NULL;
  40. ts = result;
  41. #elif defined (OPENSSL_SYS_WINDOWS) && defined(_MSC_VER) && _MSC_VER >= 1400
  42. if (gmtime_s(result, timer))
  43. return NULL;
  44. ts = result;
  45. #else
  46. ts = gmtime(timer);
  47. if (ts == NULL)
  48. return NULL;
  49. memcpy(result, ts, sizeof(struct tm));
  50. ts = result;
  51. #endif
  52. return ts;
  53. }
  54. /*
  55. * Take a tm structure and add an offset to it. This avoids any OS issues
  56. * with restricted date types and overflows which cause the year 2038
  57. * problem.
  58. */
  59. #define SECS_PER_DAY (24 * 60 * 60)
  60. static long date_to_julian(int y, int m, int d);
  61. static void julian_to_date(long jd, int *y, int *m, int *d);
  62. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  63. long *pday, int *psec);
  64. int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
  65. {
  66. int time_sec, time_year, time_month, time_day;
  67. long time_jd;
  68. /* Convert time and offset into Julian day and seconds */
  69. if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
  70. return 0;
  71. /* Convert Julian day back to date */
  72. julian_to_date(time_jd, &time_year, &time_month, &time_day);
  73. if (time_year < 1900 || time_year > 9999)
  74. return 0;
  75. /* Update tm structure */
  76. tm->tm_year = time_year - 1900;
  77. tm->tm_mon = time_month - 1;
  78. tm->tm_mday = time_day;
  79. tm->tm_hour = time_sec / 3600;
  80. tm->tm_min = (time_sec / 60) % 60;
  81. tm->tm_sec = time_sec % 60;
  82. return 1;
  83. }
  84. int OPENSSL_gmtime_diff(int *pday, int *psec,
  85. const struct tm *from, const struct tm *to)
  86. {
  87. int from_sec, to_sec, diff_sec;
  88. long from_jd, to_jd, diff_day;
  89. if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
  90. return 0;
  91. if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
  92. return 0;
  93. diff_day = to_jd - from_jd;
  94. diff_sec = to_sec - from_sec;
  95. /* Adjust differences so both positive or both negative */
  96. if (diff_day > 0 && diff_sec < 0) {
  97. diff_day--;
  98. diff_sec += SECS_PER_DAY;
  99. }
  100. if (diff_day < 0 && diff_sec > 0) {
  101. diff_day++;
  102. diff_sec -= SECS_PER_DAY;
  103. }
  104. if (pday)
  105. *pday = (int)diff_day;
  106. if (psec)
  107. *psec = diff_sec;
  108. return 1;
  109. }
  110. /* Convert tm structure and offset into julian day and seconds */
  111. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  112. long *pday, int *psec)
  113. {
  114. int offset_hms, offset_day;
  115. long time_jd;
  116. int time_year, time_month, time_day;
  117. /* split offset into days and day seconds */
  118. offset_day = offset_sec / SECS_PER_DAY;
  119. /* Avoid sign issues with % operator */
  120. offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
  121. offset_day += off_day;
  122. /* Add current time seconds to offset */
  123. offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  124. /* Adjust day seconds if overflow */
  125. if (offset_hms >= SECS_PER_DAY) {
  126. offset_day++;
  127. offset_hms -= SECS_PER_DAY;
  128. } else if (offset_hms < 0) {
  129. offset_day--;
  130. offset_hms += SECS_PER_DAY;
  131. }
  132. /*
  133. * Convert date of time structure into a Julian day number.
  134. */
  135. time_year = tm->tm_year + 1900;
  136. time_month = tm->tm_mon + 1;
  137. time_day = tm->tm_mday;
  138. time_jd = date_to_julian(time_year, time_month, time_day);
  139. /* Work out Julian day of new date */
  140. time_jd += offset_day;
  141. if (time_jd < 0)
  142. return 0;
  143. *pday = time_jd;
  144. *psec = offset_hms;
  145. return 1;
  146. }
  147. /*
  148. * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
  149. */
  150. static long date_to_julian(int y, int m, int d)
  151. {
  152. return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
  153. (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
  154. (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
  155. }
  156. static void julian_to_date(long jd, int *y, int *m, int *d)
  157. {
  158. long L = jd + 68569;
  159. long n = (4 * L) / 146097;
  160. long i, j;
  161. L = L - (146097 * n + 3) / 4;
  162. i = (4000 * (L + 1)) / 1461001;
  163. L = L - (1461 * i) / 4 + 31;
  164. j = (80 * L) / 2447;
  165. *d = L - (2447 * j) / 80;
  166. L = j / 11;
  167. *m = j + 2 - (12 * L);
  168. *y = 100 * (n - 49) + i + L;
  169. }