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 */
30 * Needed to pick up the correct definitions and declarations in some of the
31 * DEC C Header Files (*.H).
33 # define __NEW_STARLET 1
35 # if (defined(__alpha) || defined(__ia64))
40 typedef struct _ile3 { /* Copied from ILEDEF.H for Alpha */
41 # pragma __nomember_alignment
42 unsigned short int ile3$w_length; /* Length of buffer in bytes */
43 unsigned short int ile3$w_code; /* Item code value */
44 void *ile3$ps_bufaddr; /* Buffer address */
45 unsigned short int *ile3$ps_retlen_addr; /* Address of word for returned length */
47 # endif /* alpha || ia64 */
48 #endif /* OPENSSL_SYS_VMS */
50 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
54 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
56 * should return &data, but doesn't on some systems, so we don't even
57 * look at the return value
59 if (gmtime_r(timer, result) == NULL)
62 #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
67 memcpy(result, ts, sizeof(struct tm));
70 #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
72 static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
73 static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
75 unsigned int reslen = 0;
76 # if __INITIAL_POINTER_SIZE == 64
77 ILEB_64 itemlist[2], *pitem;
79 ILE3 itemlist[2], *pitem;
86 * Setup an itemlist for the call to $TRNLNM - Translate Logical Name.
90 # if __INITIAL_POINTER_SIZE == 64
91 pitem->ileb_64$w_mbo = 1;
92 pitem->ileb_64$w_code = LNM$_STRING;
93 pitem->ileb_64$l_mbmo = -1;
94 pitem->ileb_64$q_length = sizeof (logvalue);
95 pitem->ileb_64$pq_bufaddr = logvalue;
96 pitem->ileb_64$pq_retlen_addr = (unsigned __int64 *) &reslen;
98 /* Last item of the item list is null terminated */
99 pitem->ileb_64$q_length = pitem->ileb_64$w_code = 0;
101 pitem->ile3$w_length = sizeof (logvalue);
102 pitem->ile3$w_code = LNM$_STRING;
103 pitem->ile3$ps_bufaddr = logvalue;
104 pitem->ile3$ps_retlen_addr = (unsigned short int *) &reslen;
106 /* Last item of the item list is null terminated */
107 pitem->ile3$w_length = pitem->ile3$w_code = 0;
111 /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
112 status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
115 logvalue[reslen] = '\0';
119 /* The following is extracted from the DEC C header time.h */
121 ** Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
122 ** have two implementations. One implementation is provided
123 ** for compatibility and deals with time in terms of local time,
124 ** the other __utc_* deals with time in terms of UTC.
127 * We use the same conditions as in said time.h to check if we should
128 * assume that t contains local time (and should therefore be
129 * adjusted) or UTC (and should therefore be left untouched).
131 # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
132 /* Get the numerical value of the equivalence string */
133 status = atoi(logvalue);
135 /* and use it to move time to GMT */
139 /* then convert the result to the time structure */
142 * Since there was no gmtime_r() to do this stuff for us, we have to
143 * do it the hard way.
147 * The VMS epoch is the astronomical Smithsonian date,
148 if I remember correctly, which is November 17, 1858.
149 Furthermore, time is measure in tenths of microseconds
150 and stored in quadwords (64 bit integers). unix_epoch
151 below is January 1st 1970 expressed as a VMS time. The
152 following code was used to get this number:
156 #include <lib$routines.h>
161 unsigned long systime[2];
162 unsigned short epoch_values[7] =
163 { 1970, 1, 1, 0, 0, 0, 0 };
165 lib$cvt_vectim(epoch_values, systime);
167 printf("%u %u", systime[0], systime[1]);
170 unsigned long unix_epoch[2] = { 1273708544, 8164711 };
171 unsigned long deltatime[2];
172 unsigned long systime[2];
174 short year, month, day, hour, minute, second, centi_second;
179 * Turn the number of seconds since January 1st 1970 to an
180 * internal delta time. Note that lib$cvt_to_internal_time() will
181 * assume that t is signed, and will therefore break on 32-bit
182 * systems some time in 2038.
184 operation = LIB$K_DELTA_SECONDS;
185 status = lib$cvt_to_internal_time(&operation, &t, deltatime);
188 * Add the delta time with the Unix epoch and we have the current
189 * UTC time in internal format
191 status = lib$add_times(unix_epoch, deltatime, systime);
193 /* Turn the internal time into a time vector */
194 status = sys$numtim(&time_values, systime);
196 /* Fill in the struct tm with the result */
197 result->tm_sec = time_values.second;
198 result->tm_min = time_values.minute;
199 result->tm_hour = time_values.hour;
200 result->tm_mday = time_values.day;
201 result->tm_mon = time_values.month - 1;
202 result->tm_year = time_values.year - 1900;
204 operation = LIB$K_DAY_OF_WEEK;
205 status = lib$cvt_from_internal_time(&operation,
206 &result->tm_wday, systime);
207 result->tm_wday %= 7;
209 operation = LIB$K_DAY_OF_YEAR;
210 status = lib$cvt_from_internal_time(&operation,
211 &result->tm_yday, systime);
214 result->tm_isdst = 0; /* There's no way to know... */
224 * Take a tm structure and add an offset to it. This avoids any OS issues
225 * with restricted date types and overflows which cause the year 2038
229 #define SECS_PER_DAY (24 * 60 * 60)
231 static long date_to_julian(int y, int m, int d);
232 static void julian_to_date(long jd, int *y, int *m, int *d);
233 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
234 long *pday, int *psec);
236 int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
238 int time_sec, time_year, time_month, time_day;
241 /* Convert time and offset into Julian day and seconds */
242 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
245 /* Convert Julian day back to date */
247 julian_to_date(time_jd, &time_year, &time_month, &time_day);
249 if (time_year < 1900 || time_year > 9999)
252 /* Update tm structure */
254 tm->tm_year = time_year - 1900;
255 tm->tm_mon = time_month - 1;
256 tm->tm_mday = time_day;
258 tm->tm_hour = time_sec / 3600;
259 tm->tm_min = (time_sec / 60) % 60;
260 tm->tm_sec = time_sec % 60;
266 int OPENSSL_gmtime_diff(int *pday, int *psec,
267 const struct tm *from, const struct tm *to)
269 int from_sec, to_sec, diff_sec;
270 long from_jd, to_jd, diff_day;
271 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
273 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
275 diff_day = to_jd - from_jd;
276 diff_sec = to_sec - from_sec;
277 /* Adjust differences so both positive or both negative */
278 if (diff_day > 0 && diff_sec < 0) {
280 diff_sec += SECS_PER_DAY;
282 if (diff_day < 0 && diff_sec > 0) {
284 diff_sec -= SECS_PER_DAY;
288 *pday = (int)diff_day;
296 /* Convert tm structure and offset into julian day and seconds */
297 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
298 long *pday, int *psec)
300 int offset_hms, offset_day;
302 int time_year, time_month, time_day;
303 /* split offset into days and day seconds */
304 offset_day = offset_sec / SECS_PER_DAY;
305 /* Avoid sign issues with % operator */
306 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
307 offset_day += off_day;
308 /* Add current time seconds to offset */
309 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
310 /* Adjust day seconds if overflow */
311 if (offset_hms >= SECS_PER_DAY) {
313 offset_hms -= SECS_PER_DAY;
314 } else if (offset_hms < 0) {
316 offset_hms += SECS_PER_DAY;
320 * Convert date of time structure into a Julian day number.
323 time_year = tm->tm_year + 1900;
324 time_month = tm->tm_mon + 1;
325 time_day = tm->tm_mday;
327 time_jd = date_to_julian(time_year, time_month, time_day);
329 /* Work out Julian day of new date */
330 time_jd += offset_day;
341 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
343 static long date_to_julian(int y, int m, int d)
345 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
346 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
347 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
350 static void julian_to_date(long jd, int *y, int *m, int *d)
353 long n = (4 * L) / 146097;
356 L = L - (146097 * n + 3) / 4;
357 i = (4000 * (L + 1)) / 1461001;
358 L = L - (1461 * i) / 4 + 31;
360 *d = L - (2447 * j) / 80;
362 *m = j + 2 - (12 * L);
363 *y = 100 * (n - 49) + i + L;