o_time.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2001-2016 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 temporarly 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. #else
  42. ts = gmtime(timer);
  43. if (ts == NULL)
  44. return NULL;
  45. memcpy(result, ts, sizeof(struct tm));
  46. ts = result;
  47. #endif
  48. return ts;
  49. }
  50. /*
  51. * Take a tm structure and add an offset to it. This avoids any OS issues
  52. * with restricted date types and overflows which cause the year 2038
  53. * problem.
  54. */
  55. #define SECS_PER_DAY (24 * 60 * 60)
  56. static long date_to_julian(int y, int m, int d);
  57. static void julian_to_date(long jd, int *y, int *m, int *d);
  58. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  59. long *pday, int *psec);
  60. int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
  61. {
  62. int time_sec, time_year, time_month, time_day;
  63. long time_jd;
  64. /* Convert time and offset into Julian day and seconds */
  65. if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
  66. return 0;
  67. /* Convert Julian day back to date */
  68. julian_to_date(time_jd, &time_year, &time_month, &time_day);
  69. if (time_year < 1900 || time_year > 9999)
  70. return 0;
  71. /* Update tm structure */
  72. tm->tm_year = time_year - 1900;
  73. tm->tm_mon = time_month - 1;
  74. tm->tm_mday = time_day;
  75. tm->tm_hour = time_sec / 3600;
  76. tm->tm_min = (time_sec / 60) % 60;
  77. tm->tm_sec = time_sec % 60;
  78. return 1;
  79. }
  80. int OPENSSL_gmtime_diff(int *pday, int *psec,
  81. const struct tm *from, const struct tm *to)
  82. {
  83. int from_sec, to_sec, diff_sec;
  84. long from_jd, to_jd, diff_day;
  85. if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
  86. return 0;
  87. if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
  88. return 0;
  89. diff_day = to_jd - from_jd;
  90. diff_sec = to_sec - from_sec;
  91. /* Adjust differences so both positive or both negative */
  92. if (diff_day > 0 && diff_sec < 0) {
  93. diff_day--;
  94. diff_sec += SECS_PER_DAY;
  95. }
  96. if (diff_day < 0 && diff_sec > 0) {
  97. diff_day++;
  98. diff_sec -= SECS_PER_DAY;
  99. }
  100. if (pday)
  101. *pday = (int)diff_day;
  102. if (psec)
  103. *psec = diff_sec;
  104. return 1;
  105. }
  106. /* Convert tm structure and offset into julian day and seconds */
  107. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  108. long *pday, int *psec)
  109. {
  110. int offset_hms, offset_day;
  111. long time_jd;
  112. int time_year, time_month, time_day;
  113. /* split offset into days and day seconds */
  114. offset_day = offset_sec / SECS_PER_DAY;
  115. /* Avoid sign issues with % operator */
  116. offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
  117. offset_day += off_day;
  118. /* Add current time seconds to offset */
  119. offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  120. /* Adjust day seconds if overflow */
  121. if (offset_hms >= SECS_PER_DAY) {
  122. offset_day++;
  123. offset_hms -= SECS_PER_DAY;
  124. } else if (offset_hms < 0) {
  125. offset_day--;
  126. offset_hms += SECS_PER_DAY;
  127. }
  128. /*
  129. * Convert date of time structure into a Julian day number.
  130. */
  131. time_year = tm->tm_year + 1900;
  132. time_month = tm->tm_mon + 1;
  133. time_day = tm->tm_mday;
  134. time_jd = date_to_julian(time_year, time_month, time_day);
  135. /* Work out Julian day of new date */
  136. time_jd += offset_day;
  137. if (time_jd < 0)
  138. return 0;
  139. *pday = time_jd;
  140. *psec = offset_hms;
  141. return 1;
  142. }
  143. /*
  144. * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
  145. */
  146. static long date_to_julian(int y, int m, int d)
  147. {
  148. return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
  149. (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
  150. (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
  151. }
  152. static void julian_to_date(long jd, int *y, int *m, int *d)
  153. {
  154. long L = jd + 68569;
  155. long n = (4 * L) / 146097;
  156. long i, j;
  157. L = L - (146097 * n + 3) / 4;
  158. i = (4000 * (L + 1)) / 1461001;
  159. L = L - (1461 * i) / 4 + 31;
  160. j = (80 * L) / 2447;
  161. *d = L - (2447 * j) / 80;
  162. L = j / 11;
  163. *m = j + 2 - (12 * L);
  164. *y = 100 * (n - 49) + i + L;
  165. }