o_time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  3. * 2001.
  4. */
  5. /*
  6. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  7. * 2008.
  8. */
  9. /* ====================================================================
  10. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * 3. All advertising materials mentioning features or use of this
  25. * software must display the following acknowledgment:
  26. * "This product includes software developed by the OpenSSL Project
  27. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  28. *
  29. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  30. * endorse or promote products derived from this software without
  31. * prior written permission. For written permission, please contact
  32. * licensing@OpenSSL.org.
  33. *
  34. * 5. Products derived from this software may not be called "OpenSSL"
  35. * nor may "OpenSSL" appear in their names without prior written
  36. * permission of the OpenSSL Project.
  37. *
  38. * 6. Redistributions of any form whatsoever must retain the following
  39. * acknowledgment:
  40. * "This product includes software developed by the OpenSSL Project
  41. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  44. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  46. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  47. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  52. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  54. * OF THE POSSIBILITY OF SUCH DAMAGE.
  55. * ====================================================================
  56. *
  57. * This product includes cryptographic software written by Eric Young
  58. * (eay@cryptsoft.com). This product includes software written by Tim
  59. * Hudson (tjh@cryptsoft.com).
  60. *
  61. */
  62. #include <openssl/e_os2.h>
  63. #include <string.h>
  64. #include <openssl/crypto.h>
  65. #ifdef OPENSSL_SYS_VMS
  66. # if __CRTL_VER >= 70000000 && \
  67. (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
  68. # define VMS_GMTIME_OK
  69. # endif
  70. # ifndef VMS_GMTIME_OK
  71. # include <libdtdef.h>
  72. # include <lib$routines.h>
  73. # include <lnmdef.h>
  74. # include <starlet.h>
  75. # include <descrip.h>
  76. # include <stdlib.h>
  77. # endif /* ndef VMS_GMTIME_OK */
  78. #endif
  79. struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
  80. {
  81. struct tm *ts = NULL;
  82. #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
  83. /*
  84. * should return &data, but doesn't on some systems, so we don't even
  85. * look at the return value
  86. */
  87. gmtime_r(timer, result);
  88. ts = result;
  89. #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
  90. ts = gmtime(timer);
  91. if (ts == NULL)
  92. return NULL;
  93. memcpy(result, ts, sizeof(struct tm));
  94. ts = result;
  95. #endif
  96. #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
  97. if (ts == NULL) {
  98. static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
  99. static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
  100. char logvalue[256];
  101. unsigned int reslen = 0;
  102. struct {
  103. short buflen;
  104. short code;
  105. void *bufaddr;
  106. unsigned int *reslen;
  107. } itemlist[] = {
  108. {
  109. 0, LNM$_STRING, 0, 0
  110. },
  111. {
  112. 0, 0, 0, 0
  113. },
  114. };
  115. int status;
  116. time_t t;
  117. /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
  118. itemlist[0].buflen = sizeof(logvalue);
  119. itemlist[0].bufaddr = logvalue;
  120. itemlist[0].reslen = &reslen;
  121. status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
  122. if (!(status & 1))
  123. return NULL;
  124. logvalue[reslen] = '\0';
  125. t = *timer;
  126. /* The following is extracted from the DEC C header time.h */
  127. /*
  128. ** Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
  129. ** have two implementations. One implementation is provided
  130. ** for compatibility and deals with time in terms of local time,
  131. ** the other __utc_* deals with time in terms of UTC.
  132. */
  133. /*
  134. * We use the same conditions as in said time.h to check if we should
  135. * assume that t contains local time (and should therefore be
  136. * adjusted) or UTC (and should therefore be left untouched).
  137. */
  138. # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
  139. /* Get the numerical value of the equivalence string */
  140. status = atoi(logvalue);
  141. /* and use it to move time to GMT */
  142. t -= status;
  143. # endif
  144. /* then convert the result to the time structure */
  145. /*
  146. * Since there was no gmtime_r() to do this stuff for us, we have to
  147. * do it the hard way.
  148. */
  149. {
  150. /*-
  151. * The VMS epoch is the astronomical Smithsonian date,
  152. if I remember correctly, which is November 17, 1858.
  153. Furthermore, time is measure in thenths of microseconds
  154. and stored in quadwords (64 bit integers). unix_epoch
  155. below is January 1st 1970 expressed as a VMS time. The
  156. following code was used to get this number:
  157. #include <stdio.h>
  158. #include <stdlib.h>
  159. #include <lib$routines.h>
  160. #include <starlet.h>
  161. main()
  162. {
  163. unsigned long systime[2];
  164. unsigned short epoch_values[7] =
  165. { 1970, 1, 1, 0, 0, 0, 0 };
  166. lib$cvt_vectim(epoch_values, systime);
  167. printf("%u %u", systime[0], systime[1]);
  168. }
  169. */
  170. unsigned long unix_epoch[2] = { 1273708544, 8164711 };
  171. unsigned long deltatime[2];
  172. unsigned long systime[2];
  173. struct vms_vectime {
  174. short year, month, day, hour, minute, second, centi_second;
  175. } time_values;
  176. long operation;
  177. /*
  178. * Turn the number of seconds since January 1st 1970 to an
  179. * internal delta time. Note that lib$cvt_to_internal_time() will
  180. * assume that t is signed, and will therefore break on 32-bit
  181. * systems some time in 2038.
  182. */
  183. operation = LIB$K_DELTA_SECONDS;
  184. status = lib$cvt_to_internal_time(&operation, &t, deltatime);
  185. /*
  186. * Add the delta time with the Unix epoch and we have the current
  187. * UTC time in internal format
  188. */
  189. status = lib$add_times(unix_epoch, deltatime, systime);
  190. /* Turn the internal time into a time vector */
  191. status = sys$numtim(&time_values, systime);
  192. /* Fill in the struct tm with the result */
  193. result->tm_sec = time_values.second;
  194. result->tm_min = time_values.minute;
  195. result->tm_hour = time_values.hour;
  196. result->tm_mday = time_values.day;
  197. result->tm_mon = time_values.month - 1;
  198. result->tm_year = time_values.year - 1900;
  199. operation = LIB$K_DAY_OF_WEEK;
  200. status = lib$cvt_from_internal_time(&operation,
  201. &result->tm_wday, systime);
  202. result->tm_wday %= 7;
  203. operation = LIB$K_DAY_OF_YEAR;
  204. status = lib$cvt_from_internal_time(&operation,
  205. &result->tm_yday, systime);
  206. result->tm_yday--;
  207. result->tm_isdst = 0; /* There's no way to know... */
  208. ts = result;
  209. }
  210. }
  211. #endif
  212. return ts;
  213. }
  214. /*
  215. * Take a tm structure and add an offset to it. This avoids any OS issues
  216. * with restricted date types and overflows which cause the year 2038
  217. * problem.
  218. */
  219. #define SECS_PER_DAY (24 * 60 * 60)
  220. static long date_to_julian(int y, int m, int d);
  221. static void julian_to_date(long jd, int *y, int *m, int *d);
  222. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  223. long *pday, int *psec);
  224. int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
  225. {
  226. int time_sec, time_year, time_month, time_day;
  227. long time_jd;
  228. /* Convert time and offset into julian day and seconds */
  229. if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
  230. return 0;
  231. /* Convert Julian day back to date */
  232. julian_to_date(time_jd, &time_year, &time_month, &time_day);
  233. if (time_year < 1900 || time_year > 9999)
  234. return 0;
  235. /* Update tm structure */
  236. tm->tm_year = time_year - 1900;
  237. tm->tm_mon = time_month - 1;
  238. tm->tm_mday = time_day;
  239. tm->tm_hour = time_sec / 3600;
  240. tm->tm_min = (time_sec / 60) % 60;
  241. tm->tm_sec = time_sec % 60;
  242. return 1;
  243. }
  244. int OPENSSL_gmtime_diff(int *pday, int *psec,
  245. const struct tm *from, const struct tm *to)
  246. {
  247. int from_sec, to_sec, diff_sec;
  248. long from_jd, to_jd, diff_day;
  249. if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
  250. return 0;
  251. if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
  252. return 0;
  253. diff_day = to_jd - from_jd;
  254. diff_sec = to_sec - from_sec;
  255. /* Adjust differences so both positive or both negative */
  256. if (diff_day > 0 && diff_sec < 0) {
  257. diff_day--;
  258. diff_sec += SECS_PER_DAY;
  259. }
  260. if (diff_day < 0 && diff_sec > 0) {
  261. diff_day++;
  262. diff_sec -= SECS_PER_DAY;
  263. }
  264. if (pday)
  265. *pday = (int)diff_day;
  266. if (psec)
  267. *psec = diff_sec;
  268. return 1;
  269. }
  270. /* Convert tm structure and offset into julian day and seconds */
  271. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  272. long *pday, int *psec)
  273. {
  274. int offset_hms, offset_day;
  275. long time_jd;
  276. int time_year, time_month, time_day;
  277. /* split offset into days and day seconds */
  278. offset_day = offset_sec / SECS_PER_DAY;
  279. /* Avoid sign issues with % operator */
  280. offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
  281. offset_day += off_day;
  282. /* Add current time seconds to offset */
  283. offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  284. /* Adjust day seconds if overflow */
  285. if (offset_hms >= SECS_PER_DAY) {
  286. offset_day++;
  287. offset_hms -= SECS_PER_DAY;
  288. } else if (offset_hms < 0) {
  289. offset_day--;
  290. offset_hms += SECS_PER_DAY;
  291. }
  292. /*
  293. * Convert date of time structure into a Julian day number.
  294. */
  295. time_year = tm->tm_year + 1900;
  296. time_month = tm->tm_mon + 1;
  297. time_day = tm->tm_mday;
  298. time_jd = date_to_julian(time_year, time_month, time_day);
  299. /* Work out Julian day of new date */
  300. time_jd += offset_day;
  301. if (time_jd < 0)
  302. return 0;
  303. *pday = time_jd;
  304. *psec = offset_hms;
  305. return 1;
  306. }
  307. /*
  308. * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
  309. */
  310. static long date_to_julian(int y, int m, int d)
  311. {
  312. return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
  313. (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
  314. (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
  315. }
  316. static void julian_to_date(long jd, int *y, int *m, int *d)
  317. {
  318. long L = jd + 68569;
  319. long n = (4 * L) / 146097;
  320. long i, j;
  321. L = L - (146097 * n + 3) / 4;
  322. i = (4000 * (L + 1)) / 1461001;
  323. L = L - (1461 * i) / 4 + 31;
  324. j = (80 * L) / 2447;
  325. *d = L - (2447 * j) / 80;
  326. L = j / 11;
  327. *m = j + 2 - (12 * L);
  328. *y = 100 * (n - 49) + i + L;
  329. }