o_time.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <openssl/e_os2.h>
  59. #include <string.h>
  60. #include "o_time.h"
  61. #ifdef OPENSSL_SYS_VMS
  62. # include <libdtdef.h>
  63. # include <lib$routines.h>
  64. # include <lnmdef.h>
  65. # include <starlet.h>
  66. # include <descrip.h>
  67. # include <stdlib.h>
  68. #endif
  69. struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
  70. {
  71. struct tm *ts = NULL;
  72. #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)
  73. /* should return &data, but doesn't on some systems,
  74. so we don't even look at the return value */
  75. gmtime_r(timer,result);
  76. ts = result;
  77. #elif !defined(OPENSSL_SYS_VMS)
  78. ts = gmtime(timer);
  79. if (ts == NULL)
  80. return NULL;
  81. memcpy(result, ts, sizeof(struct tm));
  82. ts = result;
  83. #endif
  84. #ifdef OPENSSL_SYS_VMS
  85. if (ts == NULL)
  86. {
  87. static $DESCRIPTOR(tabnam,"LNM$DCL_LOGICAL");
  88. static $DESCRIPTOR(lognam,"SYS$TIMEZONE_DIFFERENTIAL");
  89. char logvalue[256];
  90. unsigned int reslen = 0;
  91. struct {
  92. short buflen;
  93. short code;
  94. void *bufaddr;
  95. unsigned int *reslen;
  96. } itemlist[] = {
  97. { 0, LNM$_STRING, 0, 0 },
  98. { 0, 0, 0, 0 },
  99. };
  100. int status;
  101. time_t t;
  102. /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
  103. itemlist[0].buflen = sizeof(logvalue);
  104. itemlist[0].bufaddr = logvalue;
  105. itemlist[0].reslen = &reslen;
  106. status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
  107. if (!(status & 1))
  108. return NULL;
  109. logvalue[reslen] = '\0';
  110. /* Get the numerical value of the equivalence string */
  111. status = atoi(logvalue);
  112. /* and use it to move time to GMT */
  113. t = *timer - status;
  114. /* then convert the result to the time structure */
  115. #ifndef OPENSSL_THREADS
  116. ts=(struct tm *)localtime(&t);
  117. #else
  118. /* Since there was no gmtime_r() to do this stuff for us,
  119. we have to do it the hard way. */
  120. {
  121. /* The VMS epoch is the astronomical Smithsonian date,
  122. if I remember correctly, which is November 17, 1858.
  123. Furthermore, time is measure in thenths of microseconds
  124. and stored in quadwords (64 bit integers). unix_epoch
  125. below is January 1st 1970 expressed as a VMS time. The
  126. following code was used to get this number:
  127. #include <stdio.h>
  128. #include <stdlib.h>
  129. #include <lib$routines.h>
  130. #include <starlet.h>
  131. main()
  132. {
  133. unsigned long systime[2];
  134. unsigned short epoch_values[7] =
  135. { 1970, 1, 1, 0, 0, 0, 0 };
  136. lib$cvt_vectim(epoch_values, systime);
  137. printf("%u %u", systime[0], systime[1]);
  138. }
  139. */
  140. unsigned long unix_epoch[2] = { 1273708544, 8164711 };
  141. unsigned long deltatime[2];
  142. unsigned long systime[2];
  143. struct vms_vectime
  144. {
  145. short year, month, day, hour, minute, second,
  146. centi_second;
  147. } time_values;
  148. long operation;
  149. /* Turn the number of seconds since January 1st 1970 to
  150. an internal delta time.
  151. Note that lib$cvt_to_internal_time() will assume
  152. that t is signed, and will therefore break on 32-bit
  153. systems some time in 2038.
  154. */
  155. operation = LIB$K_DELTA_SECONDS;
  156. status = lib$cvt_to_internal_time(&operation,
  157. &t, deltatime);
  158. /* Add the delta time with the Unix epoch and we have
  159. the current UTC time in internal format */
  160. status = lib$add_times(unix_epoch, deltatime, systime);
  161. /* Turn the internal time into a time vector */
  162. status = sys$numtim(&time_values, systime);
  163. /* Fill in the struct tm with the result */
  164. result->tm_sec = time_values.second;
  165. result->tm_min = time_values.minute;
  166. result->tm_hour = time_values.hour;
  167. result->tm_mday = time_values.day;
  168. result->tm_mon = time_values.month - 1;
  169. result->tm_year = time_values.year - 1900;
  170. operation = LIB$K_DAY_OF_WEEK;
  171. status = lib$cvt_from_internal_time(&operation,
  172. &result->tm_wday, systime);
  173. result->tm_wday %= 7;
  174. operation = LIB$K_DAY_OF_YEAR;
  175. status = lib$cvt_from_internal_time(&operation,
  176. &result->tm_yday, systime);
  177. result->tm_yday--;
  178. result->tm_isdst = 0; /* There's no way to know... */
  179. ts = result;
  180. #endif
  181. }
  182. }
  183. #endif
  184. return ts;
  185. }