Run util/openssl-format-source -v -c .
[openssl.git] / crypto / o_time.c
1 /* crypto/o_time.c -*- mode:C; c-file-style: "eay" -*- */
2 /*
3  * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4  * 2001.
5  */
6 /* ====================================================================
7  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <openssl/e_os2.h>
61 #include <string.h>
62 #include "o_time.h"
63
64 #ifdef OPENSSL_SYS_VMS
65 # include <libdtdef.h>
66 # include <lib$routines.h>
67 # include <lnmdef.h>
68 # include <starlet.h>
69 # include <descrip.h>
70 # include <stdlib.h>
71 #endif
72
73 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
74 {
75     struct tm *ts = NULL;
76
77 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && !defined(__CYGWIN32__) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
78     /*
79      * should return &data, but doesn't on some systems, so we don't even
80      * look at the return value
81      */
82     gmtime_r(timer, result);
83     ts = result;
84 #elif !defined(OPENSSL_SYS_VMS)
85     ts = gmtime(timer);
86     if (ts == NULL)
87         return NULL;
88
89     memcpy(result, ts, sizeof(struct tm));
90     ts = result;
91 #endif
92 #ifdef OPENSSL_SYS_VMS
93     if (ts == NULL) {
94         static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
95         static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
96         char logvalue[256];
97         unsigned int reslen = 0;
98         struct {
99             short buflen;
100             short code;
101             void *bufaddr;
102             unsigned int *reslen;
103         } itemlist[] = {
104             {
105                 0, LNM$_STRING, 0, 0
106             },
107             {
108                 0, 0, 0, 0
109             },
110         };
111         int status;
112         time_t t;
113
114         /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
115         itemlist[0].buflen = sizeof(logvalue);
116         itemlist[0].bufaddr = logvalue;
117         itemlist[0].reslen = &reslen;
118         status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
119         if (!(status & 1))
120             return NULL;
121         logvalue[reslen] = '\0';
122
123         t = *timer;
124
125 /* The following is extracted from the DEC C header time.h */
126         /*
127          **  Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
128          **  have two implementations.  One implementation is provided
129          **  for compatibility and deals with time in terms of local time,
130          **  the other __utc_* deals with time in terms of UTC.
131          */
132         /*
133          * We use the same conditions as in said time.h to check if we should
134          * assume that t contains local time (and should therefore be
135          * adjusted) or UTC (and should therefore be left untouched).
136          */
137 # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
138         /* Get the numerical value of the equivalence string */
139         status = atoi(logvalue);
140
141         /* and use it to move time to GMT */
142         t -= status;
143 # endif
144
145         /* then convert the result to the time structure */
146
147         /*
148          * Since there was no gmtime_r() to do this stuff for us, we have to
149          * do it the hard way.
150          */
151         {
152                 /*-
153                  * The VMS epoch is the astronomical Smithsonian date,
154                    if I remember correctly, which is November 17, 1858.
155                    Furthermore, time is measure in thenths of microseconds
156                    and stored in quadwords (64 bit integers).  unix_epoch
157                    below is January 1st 1970 expressed as a VMS time.  The
158                    following code was used to get this number:
159
160                    #include <stdio.h>
161                    #include <stdlib.h>
162                    #include <lib$routines.h>
163                    #include <starlet.h>
164
165                    main()
166                    {
167                      unsigned long systime[2];
168                      unsigned short epoch_values[7] =
169                        { 1970, 1, 1, 0, 0, 0, 0 };
170
171                      lib$cvt_vectim(epoch_values, systime);
172
173                      printf("%u %u", systime[0], systime[1]);
174                    }
175                 */
176             unsigned long unix_epoch[2] = { 1273708544, 8164711 };
177             unsigned long deltatime[2];
178             unsigned long systime[2];
179             struct vms_vectime {
180                 short year, month, day, hour, minute, second, centi_second;
181             } time_values;
182             long operation;
183
184             /*
185              * Turn the number of seconds since January 1st 1970 to an
186              * internal delta time. Note that lib$cvt_to_internal_time() will
187              * assume that t is signed, and will therefore break on 32-bit
188              * systems some time in 2038.
189              */
190             operation = LIB$K_DELTA_SECONDS;
191             status = lib$cvt_to_internal_time(&operation, &t, deltatime);
192
193             /*
194              * Add the delta time with the Unix epoch and we have the current
195              * UTC time in internal format
196              */
197             status = lib$add_times(unix_epoch, deltatime, systime);
198
199             /* Turn the internal time into a time vector */
200             status = sys$numtim(&time_values, systime);
201
202             /* Fill in the struct tm with the result */
203             result->tm_sec = time_values.second;
204             result->tm_min = time_values.minute;
205             result->tm_hour = time_values.hour;
206             result->tm_mday = time_values.day;
207             result->tm_mon = time_values.month - 1;
208             result->tm_year = time_values.year - 1900;
209
210             operation = LIB$K_DAY_OF_WEEK;
211             status = lib$cvt_from_internal_time(&operation,
212                                                 &result->tm_wday, systime);
213             result->tm_wday %= 7;
214
215             operation = LIB$K_DAY_OF_YEAR;
216             status = lib$cvt_from_internal_time(&operation,
217                                                 &result->tm_yday, systime);
218             result->tm_yday--;
219
220             result->tm_isdst = 0; /* There's no way to know... */
221
222             ts = result;
223         }
224     }
225 #endif
226     return ts;
227 }