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