summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/o_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/o_time.c')
-rw-r--r--src/lib/libcrypto/o_time.c79
1 files changed, 40 insertions, 39 deletions
diff --git a/src/lib/libcrypto/o_time.c b/src/lib/libcrypto/o_time.c
index 5fb5d4e6d7..44f7ba3b8c 100644
--- a/src/lib/libcrypto/o_time.c
+++ b/src/lib/libcrypto/o_time.c
@@ -63,14 +63,14 @@
63#include <string.h> 63#include <string.h>
64#include "o_time.h" 64#include "o_time.h"
65 65
66struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result) 66struct tm
67 { 67*OPENSSL_gmtime(const time_t *timer, struct tm *result) {
68 struct tm *ts = NULL; 68 struct tm *ts = NULL;
69 69
70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS) 70#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
71 /* should return &data, but doesn't on some systems, 71 /* should return &data, but doesn't on some systems,
72 so we don't even look at the return value */ 72 so we don't even look at the return value */
73 gmtime_r(timer,result); 73 gmtime_r(timer, result);
74 ts = result; 74 ts = result;
75#else 75#else
76 ts = gmtime(timer); 76 ts = gmtime(timer);
@@ -81,7 +81,7 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
81 ts = result; 81 ts = result;
82#endif 82#endif
83 return ts; 83 return ts;
84 } 84}
85 85
86/* Take a tm structure and add an offset to it. This avoids any OS issues 86/* Take a tm structure and add an offset to it. This avoids any OS issues
87 * with restricted date types and overflows which cause the year 2038 87 * with restricted date types and overflows which cause the year 2038
@@ -93,29 +93,27 @@ struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
93static long date_to_julian(int y, int m, int d); 93static long date_to_julian(int y, int m, int d);
94static void julian_to_date(long jd, int *y, int *m, int *d); 94static void julian_to_date(long jd, int *y, int *m, int *d);
95 95
96int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 96int
97 { 97OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
98{
98 int offset_hms, offset_day; 99 int offset_hms, offset_day;
99 long time_jd; 100 long time_jd;
100 int time_year, time_month, time_day; 101 int time_year, time_month, time_day;
101 /* split offset into days and day seconds */ 102 /* split offset into days and day seconds */
102 offset_day = offset_sec / SECS_PER_DAY; 103 offset_day = offset_sec / SECS_PER_DAY;
103 /* Avoid sign issues with % operator */ 104 /* Avoid sign issues with % operator */
104 offset_hms = offset_sec - (offset_day * SECS_PER_DAY); 105 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
105 offset_day += off_day; 106 offset_day += off_day;
106 /* Add current time seconds to offset */ 107 /* Add current time seconds to offset */
107 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; 108 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
108 /* Adjust day seconds if overflow */ 109 /* Adjust day seconds if overflow */
109 if (offset_hms >= SECS_PER_DAY) 110 if (offset_hms >= SECS_PER_DAY) {
110 {
111 offset_day++; 111 offset_day++;
112 offset_hms -= SECS_PER_DAY; 112 offset_hms -= SECS_PER_DAY;
113 } 113 } else if (offset_hms < 0) {
114 else if (offset_hms < 0)
115 {
116 offset_day--; 114 offset_day--;
117 offset_hms += SECS_PER_DAY; 115 offset_hms += SECS_PER_DAY;
118 } 116 }
119 117
120 /* Convert date of time structure into a Julian day number. 118 /* Convert date of time structure into a Julian day number.
121 */ 119 */
@@ -150,22 +148,24 @@ int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
150 tm->tm_sec = offset_hms % 60; 148 tm->tm_sec = offset_hms % 60;
151 149
152 return 1; 150 return 1;
153 151
154} 152}
155 153
156/* Convert date to and from julian day 154/* Convert date to and from julian day
157 * Uses Fliegel & Van Flandern algorithm 155 * Uses Fliegel & Van Flandern algorithm
158 */ 156 */
159static long date_to_julian(int y, int m, int d) 157static long
158date_to_julian(int y, int m, int d)
160{ 159{
161 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + 160 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
162 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - 161 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
163 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + 162 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 +
164 d - 32075; 163 d - 32075;
165} 164}
166 165
167static void julian_to_date(long jd, int *y, int *m, int *d) 166static void
168 { 167julian_to_date(long jd, int *y, int *m, int *d)
168{
169 long L = jd + 68569; 169 long L = jd + 68569;
170 long n = (4 * L) / 146097; 170 long n = (4 * L) / 146097;
171 long i, j; 171 long i, j;
@@ -178,7 +178,7 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
178 L = j / 11; 178 L = j / 11;
179 *m = j + 2 - (12 * L); 179 *m = j + 2 - (12 * L);
180 *y = 100 * (n - 49) + i + L; 180 *y = 100 * (n - 49) + i + L;
181 } 181}
182 182
183#ifdef OPENSSL_TIME_TEST 183#ifdef OPENSSL_TIME_TEST
184 184
@@ -189,20 +189,21 @@ static void julian_to_date(long jd, int *y, int *m, int *d)
189 * trigger the very errors the routines fix. 189 * trigger the very errors the routines fix.
190 */ 190 */
191 191
192int main(int argc, char **argv) 192int
193 { 193main(int argc, char **argv)
194{
194 long offset; 195 long offset;
195 for (offset = 0; offset < 1000000; offset++) 196 for (offset = 0; offset < 1000000; offset++) {
196 {
197 check_time(offset); 197 check_time(offset);
198 check_time(-offset); 198 check_time(-offset);
199 check_time(offset * 1000); 199 check_time(offset * 1000);
200 check_time(-offset * 1000); 200 check_time(-offset * 1000);
201 }
202 } 201 }
202}
203 203
204int check_time(long offset) 204int
205 { 205check_time(long offset)
206{
206 struct tm tm1, tm2; 207 struct tm tm1, tm2;
207 time_t t1, t2; 208 time_t t1, t2;
208 time(&t1); 209 time(&t1);
@@ -211,20 +212,20 @@ int check_time(long offset)
211 OPENSSL_gmtime(&t1, &tm1); 212 OPENSSL_gmtime(&t1, &tm1);
212 OPENSSL_gmtime_adj(&tm1, 0, offset); 213 OPENSSL_gmtime_adj(&tm1, 0, offset);
213 if ((tm1.tm_year == tm2.tm_year) && 214 if ((tm1.tm_year == tm2.tm_year) &&
214 (tm1.tm_mon == tm2.tm_mon) && 215 (tm1.tm_mon == tm2.tm_mon) &&
215 (tm1.tm_mday == tm2.tm_mday) && 216 (tm1.tm_mday == tm2.tm_mday) &&
216 (tm1.tm_hour == tm2.tm_hour) && 217 (tm1.tm_hour == tm2.tm_hour) &&
217 (tm1.tm_min == tm2.tm_min) && 218 (tm1.tm_min == tm2.tm_min) &&
218 (tm1.tm_sec == tm2.tm_sec)) 219 (tm1.tm_sec == tm2.tm_sec))
219 return 1; 220 return 1;
220 fprintf(stderr, "TIME ERROR!!\n"); 221 fprintf(stderr, "TIME ERROR!!\n");
221 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n", 222 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
222 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900, 223 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
223 tm2.tm_hour, tm2.tm_min, tm2.tm_sec); 224 tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
224 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n", 225 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
225 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900, 226 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
226 tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 227 tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
227 return 0; 228 return 0;
228 } 229}
229 230
230#endif 231#endif