include/openssl: don't include <windows.h> in public headers.
[openssl.git] / crypto / o_time.c
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
10 #include <openssl/e_os2.h>
11 #include <string.h>
12 #include <openssl/crypto.h>
13
14 #ifdef OPENSSL_SYS_VMS
15 # if __CRTL_VER >= 70000000 && \
16      (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
17 #  define VMS_GMTIME_OK
18 # endif
19 # ifndef VMS_GMTIME_OK
20 #  include <libdtdef.h>
21 #  include <lib$routines.h>
22 #  include <lnmdef.h>
23 #  include <starlet.h>
24 #  include <descrip.h>
25 #  include <stdlib.h>
26 # endif                         /* ndef VMS_GMTIME_OK */
27 #endif
28
29 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
30 {
31     struct tm *ts = NULL;
32
33 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
34     /*
35      * should return &data, but doesn't on some systems, so we don't even
36      * look at the return value
37      */
38     gmtime_r(timer, result);
39     ts = result;
40 #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
41     ts = gmtime(timer);
42     if (ts == NULL)
43         return NULL;
44
45     memcpy(result, ts, sizeof(struct tm));
46     ts = result;
47 #endif
48 #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
49     if (ts == NULL) {
50         static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
51         static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
52         char logvalue[256];
53         unsigned int reslen = 0;
54         struct {
55             short buflen;
56             short code;
57             void *bufaddr;
58             unsigned int *reslen;
59         } itemlist[] = {
60             {
61                 0, LNM$_STRING, 0, 0
62             },
63             {
64                 0, 0, 0, 0
65             },
66         };
67         int status;
68         time_t t;
69
70         /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
71         itemlist[0].buflen = sizeof(logvalue);
72         itemlist[0].bufaddr = logvalue;
73         itemlist[0].reslen = &reslen;
74         status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
75         if (!(status & 1))
76             return NULL;
77         logvalue[reslen] = '\0';
78
79         t = *timer;
80
81 /* The following is extracted from the DEC C header time.h */
82         /*
83          **  Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
84          **  have two implementations.  One implementation is provided
85          **  for compatibility and deals with time in terms of local time,
86          **  the other __utc_* deals with time in terms of UTC.
87          */
88         /*
89          * We use the same conditions as in said time.h to check if we should
90          * assume that t contains local time (and should therefore be
91          * adjusted) or UTC (and should therefore be left untouched).
92          */
93 # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
94         /* Get the numerical value of the equivalence string */
95         status = atoi(logvalue);
96
97         /* and use it to move time to GMT */
98         t -= status;
99 # endif
100
101         /* then convert the result to the time structure */
102
103         /*
104          * Since there was no gmtime_r() to do this stuff for us, we have to
105          * do it the hard way.
106          */
107         {
108             /*-
109              * The VMS epoch is the astronomical Smithsonian date,
110                if I remember correctly, which is November 17, 1858.
111                Furthermore, time is measure in tenths of microseconds
112                and stored in quadwords (64 bit integers).  unix_epoch
113                below is January 1st 1970 expressed as a VMS time.  The
114                following code was used to get this number:
115
116                #include <stdio.h>
117                #include <stdlib.h>
118                #include <lib$routines.h>
119                #include <starlet.h>
120
121                main()
122                {
123                  unsigned long systime[2];
124                  unsigned short epoch_values[7] =
125                    { 1970, 1, 1, 0, 0, 0, 0 };
126
127                  lib$cvt_vectim(epoch_values, systime);
128
129                  printf("%u %u", systime[0], systime[1]);
130                }
131             */
132             unsigned long unix_epoch[2] = { 1273708544, 8164711 };
133             unsigned long deltatime[2];
134             unsigned long systime[2];
135             struct vms_vectime {
136                 short year, month, day, hour, minute, second, centi_second;
137             } time_values;
138             long operation;
139
140             /*
141              * Turn the number of seconds since January 1st 1970 to an
142              * internal delta time. Note that lib$cvt_to_internal_time() will
143              * assume that t is signed, and will therefore break on 32-bit
144              * systems some time in 2038.
145              */
146             operation = LIB$K_DELTA_SECONDS;
147             status = lib$cvt_to_internal_time(&operation, &t, deltatime);
148
149             /*
150              * Add the delta time with the Unix epoch and we have the current
151              * UTC time in internal format
152              */
153             status = lib$add_times(unix_epoch, deltatime, systime);
154
155             /* Turn the internal time into a time vector */
156             status = sys$numtim(&time_values, systime);
157
158             /* Fill in the struct tm with the result */
159             result->tm_sec = time_values.second;
160             result->tm_min = time_values.minute;
161             result->tm_hour = time_values.hour;
162             result->tm_mday = time_values.day;
163             result->tm_mon = time_values.month - 1;
164             result->tm_year = time_values.year - 1900;
165
166             operation = LIB$K_DAY_OF_WEEK;
167             status = lib$cvt_from_internal_time(&operation,
168                                                 &result->tm_wday, systime);
169             result->tm_wday %= 7;
170
171             operation = LIB$K_DAY_OF_YEAR;
172             status = lib$cvt_from_internal_time(&operation,
173                                                 &result->tm_yday, systime);
174             result->tm_yday--;
175
176             result->tm_isdst = 0; /* There's no way to know... */
177
178             ts = result;
179         }
180     }
181 #endif
182     return ts;
183 }
184
185 /*
186  * Take a tm structure and add an offset to it. This avoids any OS issues
187  * with restricted date types and overflows which cause the year 2038
188  * problem.
189  */
190
191 #define SECS_PER_DAY (24 * 60 * 60)
192
193 static long date_to_julian(int y, int m, int d);
194 static void julian_to_date(long jd, int *y, int *m, int *d);
195 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
196                       long *pday, int *psec);
197
198 int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
199 {
200     int time_sec, time_year, time_month, time_day;
201     long time_jd;
202
203     /* Convert time and offset into Julian day and seconds */
204     if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
205         return 0;
206
207     /* Convert Julian day back to date */
208
209     julian_to_date(time_jd, &time_year, &time_month, &time_day);
210
211     if (time_year < 1900 || time_year > 9999)
212         return 0;
213
214     /* Update tm structure */
215
216     tm->tm_year = time_year - 1900;
217     tm->tm_mon = time_month - 1;
218     tm->tm_mday = time_day;
219
220     tm->tm_hour = time_sec / 3600;
221     tm->tm_min = (time_sec / 60) % 60;
222     tm->tm_sec = time_sec % 60;
223
224     return 1;
225
226 }
227
228 int OPENSSL_gmtime_diff(int *pday, int *psec,
229                         const struct tm *from, const struct tm *to)
230 {
231     int from_sec, to_sec, diff_sec;
232     long from_jd, to_jd, diff_day;
233     if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
234         return 0;
235     if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
236         return 0;
237     diff_day = to_jd - from_jd;
238     diff_sec = to_sec - from_sec;
239     /* Adjust differences so both positive or both negative */
240     if (diff_day > 0 && diff_sec < 0) {
241         diff_day--;
242         diff_sec += SECS_PER_DAY;
243     }
244     if (diff_day < 0 && diff_sec > 0) {
245         diff_day++;
246         diff_sec -= SECS_PER_DAY;
247     }
248
249     if (pday)
250         *pday = (int)diff_day;
251     if (psec)
252         *psec = diff_sec;
253
254     return 1;
255
256 }
257
258 /* Convert tm structure and offset into julian day and seconds */
259 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
260                       long *pday, int *psec)
261 {
262     int offset_hms, offset_day;
263     long time_jd;
264     int time_year, time_month, time_day;
265     /* split offset into days and day seconds */
266     offset_day = offset_sec / SECS_PER_DAY;
267     /* Avoid sign issues with % operator */
268     offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
269     offset_day += off_day;
270     /* Add current time seconds to offset */
271     offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
272     /* Adjust day seconds if overflow */
273     if (offset_hms >= SECS_PER_DAY) {
274         offset_day++;
275         offset_hms -= SECS_PER_DAY;
276     } else if (offset_hms < 0) {
277         offset_day--;
278         offset_hms += SECS_PER_DAY;
279     }
280
281     /*
282      * Convert date of time structure into a Julian day number.
283      */
284
285     time_year = tm->tm_year + 1900;
286     time_month = tm->tm_mon + 1;
287     time_day = tm->tm_mday;
288
289     time_jd = date_to_julian(time_year, time_month, time_day);
290
291     /* Work out Julian day of new date */
292     time_jd += offset_day;
293
294     if (time_jd < 0)
295         return 0;
296
297     *pday = time_jd;
298     *psec = offset_hms;
299     return 1;
300 }
301
302 /*
303  * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
304  */
305 static long date_to_julian(int y, int m, int d)
306 {
307     return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
308         (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
309         (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
310 }
311
312 static void julian_to_date(long jd, int *y, int *m, int *d)
313 {
314     long L = jd + 68569;
315     long n = (4 * L) / 146097;
316     long i, j;
317
318     L = L - (146097 * n + 3) / 4;
319     i = (4000 * (L + 1)) / 1461001;
320     L = L - (1461 * i) / 4 + 31;
321     j = (80 * L) / 2447;
322     *d = L - (2447 * j) / 80;
323     L = j / 11;
324     *m = j + 2 - (12 * L);
325     *y = 100 * (n - 49) + i + L;
326 }