2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/e_os2.h>
12 #include <openssl/crypto.h>
14 #ifdef OPENSSL_SYS_VMS
15 # if __CRTL_VER >= 70000000 && \
16 (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
17 # define VMS_GMTIME_OK
19 # ifndef VMS_GMTIME_OK
20 # include <libdtdef.h>
21 # include <lib$routines.h>
26 # endif /* ndef VMS_GMTIME_OK */
29 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
33 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
35 * should return &data, but doesn't on some systems, so we don't even
36 * look at the return value
38 gmtime_r(timer, result);
40 #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
45 memcpy(result, ts, sizeof(struct tm));
48 #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
50 static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
51 static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
53 unsigned int reslen = 0;
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);
77 logvalue[reslen] = '\0';
81 /* The following is extracted from the DEC C header time.h */
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.
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).
93 # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
94 /* Get the numerical value of the equivalence string */
95 status = atoi(logvalue);
97 /* and use it to move time to GMT */
101 /* then convert the result to the time structure */
104 * Since there was no gmtime_r() to do this stuff for us, we have to
105 * do it the hard way.
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:
118 #include <lib$routines.h>
123 unsigned long systime[2];
124 unsigned short epoch_values[7] =
125 { 1970, 1, 1, 0, 0, 0, 0 };
127 lib$cvt_vectim(epoch_values, systime);
129 printf("%u %u", systime[0], systime[1]);
132 unsigned long unix_epoch[2] = { 1273708544, 8164711 };
133 unsigned long deltatime[2];
134 unsigned long systime[2];
136 short year, month, day, hour, minute, second, centi_second;
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.
146 operation = LIB$K_DELTA_SECONDS;
147 status = lib$cvt_to_internal_time(&operation, &t, deltatime);
150 * Add the delta time with the Unix epoch and we have the current
151 * UTC time in internal format
153 status = lib$add_times(unix_epoch, deltatime, systime);
155 /* Turn the internal time into a time vector */
156 status = sys$numtim(&time_values, systime);
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;
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;
171 operation = LIB$K_DAY_OF_YEAR;
172 status = lib$cvt_from_internal_time(&operation,
173 &result->tm_yday, systime);
176 result->tm_isdst = 0; /* There's no way to know... */
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
191 #define SECS_PER_DAY (24 * 60 * 60)
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);
198 int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
200 int time_sec, time_year, time_month, time_day;
203 /* Convert time and offset into Julian day and seconds */
204 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
207 /* Convert Julian day back to date */
209 julian_to_date(time_jd, &time_year, &time_month, &time_day);
211 if (time_year < 1900 || time_year > 9999)
214 /* Update tm structure */
216 tm->tm_year = time_year - 1900;
217 tm->tm_mon = time_month - 1;
218 tm->tm_mday = time_day;
220 tm->tm_hour = time_sec / 3600;
221 tm->tm_min = (time_sec / 60) % 60;
222 tm->tm_sec = time_sec % 60;
228 int OPENSSL_gmtime_diff(int *pday, int *psec,
229 const struct tm *from, const struct tm *to)
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))
235 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
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) {
242 diff_sec += SECS_PER_DAY;
244 if (diff_day < 0 && diff_sec > 0) {
246 diff_sec -= SECS_PER_DAY;
250 *pday = (int)diff_day;
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)
262 int offset_hms, offset_day;
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) {
275 offset_hms -= SECS_PER_DAY;
276 } else if (offset_hms < 0) {
278 offset_hms += SECS_PER_DAY;
282 * Convert date of time structure into a Julian day number.
285 time_year = tm->tm_year + 1900;
286 time_month = tm->tm_mon + 1;
287 time_day = tm->tm_mday;
289 time_jd = date_to_julian(time_year, time_month, time_day);
291 /* Work out Julian day of new date */
292 time_jd += offset_day;
303 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
305 static long date_to_julian(int y, int m, int d)
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;
312 static void julian_to_date(long jd, int *y, int *m, int *d)
315 long n = (4 * L) / 146097;
318 L = L - (146097 * n + 3) / 4;
319 i = (4000 * (L + 1)) / 1461001;
320 L = L - (1461 * i) / 4 + 31;
322 *d = L - (2447 * j) / 80;
324 *m = j + 2 - (12 * L);
325 *y = 100 * (n - 49) + i + L;