2001-07-05 18:20:07 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2001-05-16 16:44:09 +08:00
|
|
|
*
|
2018-12-06 21:03:01 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2001-05-16 16:44:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/e_os2.h>
|
2001-08-03 18:54:00 +08:00
|
|
|
#include <string.h>
|
2014-10-12 02:31:53 +08:00
|
|
|
#include <openssl/crypto.h>
|
2001-05-16 16:44:09 +08:00
|
|
|
|
|
|
|
struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
|
|
|
|
{
|
|
|
|
struct tm *ts = NULL;
|
|
|
|
|
2017-03-01 17:33:20 +08:00
|
|
|
#if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* On VMS, gmtime_r() takes a 32-bit pointer as second argument.
|
|
|
|
* Since we can't know that |result| is in a space that can easily
|
2017-08-04 13:10:41 +08:00
|
|
|
* translate to a 32-bit pointer, we must store temporarily on stack
|
2017-03-01 17:33:20 +08:00
|
|
|
* and copy the result. The stack is always reachable with 32-bit
|
|
|
|
* pointers.
|
|
|
|
*/
|
|
|
|
#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE
|
|
|
|
# pragma pointer_size save
|
|
|
|
# pragma pointer_size 32
|
|
|
|
#endif
|
|
|
|
struct tm data, *ts2 = &data;
|
|
|
|
#if defined OPENSSL_SYS_VMS && __INITIAL_POINTER_SIZE
|
|
|
|
# pragma pointer_size restore
|
|
|
|
#endif
|
|
|
|
if (gmtime_r(timer, ts2) == NULL)
|
|
|
|
return NULL;
|
|
|
|
memcpy(result, ts2, sizeof(struct tm));
|
|
|
|
ts = result;
|
|
|
|
}
|
|
|
|
#elif defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_MACOSX)
|
2016-09-23 04:48:44 +08:00
|
|
|
if (gmtime_r(timer, result) == NULL)
|
|
|
|
return NULL;
|
2001-05-16 16:44:09 +08:00
|
|
|
ts = result;
|
2020-04-12 03:10:57 +08:00
|
|
|
#elif defined (OPENSSL_SYS_WINDOWS) && defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_WIN32_WCE)
|
2018-03-22 16:20:43 +08:00
|
|
|
if (gmtime_s(result, timer))
|
|
|
|
return NULL;
|
|
|
|
ts = result;
|
2017-02-28 15:15:31 +08:00
|
|
|
#else
|
2001-05-16 16:44:09 +08:00
|
|
|
ts = gmtime(timer);
|
2003-03-13 22:13:53 +08:00
|
|
|
if (ts == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(result, ts, sizeof(struct tm));
|
2001-05-16 16:44:09 +08:00
|
|
|
ts = result;
|
|
|
|
#endif
|
|
|
|
return ts;
|
2008-10-08 06:55:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a tm structure and add an offset to it. This avoids any OS issues
|
|
|
|
* with restricted date types and overflows which cause the year 2038
|
|
|
|
* problem.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define SECS_PER_DAY (24 * 60 * 60)
|
|
|
|
|
|
|
|
static long date_to_julian(int y, int m, int d);
|
|
|
|
static void julian_to_date(long jd, int *y, int *m, int *d);
|
2012-11-21 22:13:20 +08:00
|
|
|
static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
|
2010-04-15 21:25:26 +08:00
|
|
|
long *pday, int *psec);
|
2008-10-08 06:55:27 +08:00
|
|
|
|
|
|
|
int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
|
|
|
|
{
|
2010-04-15 21:25:26 +08:00
|
|
|
int time_sec, time_year, time_month, time_day;
|
|
|
|
long time_jd;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-02-06 04:23:54 +08:00
|
|
|
/* Convert time and offset into Julian day and seconds */
|
2010-04-15 21:25:26 +08:00
|
|
|
if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Convert Julian day back to date */
|
|
|
|
|
|
|
|
julian_to_date(time_jd, &time_year, &time_month, &time_day);
|
|
|
|
|
|
|
|
if (time_year < 1900 || time_year > 9999)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Update tm structure */
|
|
|
|
|
|
|
|
tm->tm_year = time_year - 1900;
|
|
|
|
tm->tm_mon = time_month - 1;
|
|
|
|
tm->tm_mday = time_day;
|
|
|
|
|
|
|
|
tm->tm_hour = time_sec / 3600;
|
|
|
|
tm->tm_min = (time_sec / 60) % 60;
|
|
|
|
tm->tm_sec = time_sec % 60;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-21 22:13:20 +08:00
|
|
|
int OPENSSL_gmtime_diff(int *pday, int *psec,
|
|
|
|
const struct tm *from, const struct tm *to)
|
2010-04-15 21:25:26 +08:00
|
|
|
{
|
|
|
|
int from_sec, to_sec, diff_sec;
|
|
|
|
long from_jd, to_jd, diff_day;
|
|
|
|
if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
|
|
|
|
return 0;
|
|
|
|
if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
|
|
|
|
return 0;
|
|
|
|
diff_day = to_jd - from_jd;
|
|
|
|
diff_sec = to_sec - from_sec;
|
|
|
|
/* Adjust differences so both positive or both negative */
|
|
|
|
if (diff_day > 0 && diff_sec < 0) {
|
|
|
|
diff_day--;
|
|
|
|
diff_sec += SECS_PER_DAY;
|
|
|
|
}
|
|
|
|
if (diff_day < 0 && diff_sec > 0) {
|
|
|
|
diff_day++;
|
|
|
|
diff_sec -= SECS_PER_DAY;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2012-11-20 23:20:40 +08:00
|
|
|
if (pday)
|
|
|
|
*pday = (int)diff_day;
|
|
|
|
if (psec)
|
|
|
|
*psec = diff_sec;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2010-04-15 21:25:26 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2010-04-15 21:25:26 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2010-04-15 21:25:26 +08:00
|
|
|
/* Convert tm structure and offset into julian day and seconds */
|
2012-11-21 22:13:20 +08:00
|
|
|
static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
|
2010-04-15 21:25:26 +08:00
|
|
|
long *pday, int *psec)
|
|
|
|
{
|
2021-02-20 02:02:27 +08:00
|
|
|
int offset_hms;
|
|
|
|
long offset_day, time_jd;
|
2008-10-08 06:55:27 +08:00
|
|
|
int time_year, time_month, time_day;
|
|
|
|
/* split offset into days and day seconds */
|
|
|
|
offset_day = offset_sec / SECS_PER_DAY;
|
|
|
|
/* Avoid sign issues with % operator */
|
|
|
|
offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
|
|
|
|
offset_day += off_day;
|
|
|
|
/* Add current time seconds to offset */
|
|
|
|
offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
|
|
|
|
/* Adjust day seconds if overflow */
|
|
|
|
if (offset_hms >= SECS_PER_DAY) {
|
|
|
|
offset_day++;
|
|
|
|
offset_hms -= SECS_PER_DAY;
|
|
|
|
} else if (offset_hms < 0) {
|
|
|
|
offset_day--;
|
|
|
|
offset_hms += SECS_PER_DAY;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
/*
|
|
|
|
* Convert date of time structure into a Julian day number.
|
|
|
|
*/
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
time_year = tm->tm_year + 1900;
|
|
|
|
time_month = tm->tm_mon + 1;
|
|
|
|
time_day = tm->tm_mday;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
time_jd = date_to_julian(time_year, time_month, time_day);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
/* Work out Julian day of new date */
|
|
|
|
time_jd += offset_day;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
if (time_jd < 0)
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2010-04-15 21:25:26 +08:00
|
|
|
*pday = time_jd;
|
|
|
|
*psec = offset_hms;
|
2010-04-15 21:24:20 +08:00
|
|
|
return 1;
|
2010-04-15 21:25:26 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
/*
|
|
|
|
* Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
|
|
|
|
*/
|
|
|
|
static long date_to_julian(int y, int m, int d)
|
|
|
|
{
|
|
|
|
return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
|
|
|
|
(367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
|
|
|
|
(3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void julian_to_date(long jd, int *y, int *m, int *d)
|
|
|
|
{
|
|
|
|
long L = jd + 68569;
|
|
|
|
long n = (4 * L) / 146097;
|
|
|
|
long i, j;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2008-10-08 06:55:27 +08:00
|
|
|
L = L - (146097 * n + 3) / 4;
|
|
|
|
i = (4000 * (L + 1)) / 1461001;
|
|
|
|
L = L - (1461 * i) / 4 + 31;
|
|
|
|
j = (80 * L) / 2447;
|
|
|
|
*d = L - (2447 * j) / 80;
|
|
|
|
L = j / 11;
|
|
|
|
*m = j + 2 - (12 * L);
|
|
|
|
*y = 100 * (n - 49) + i + L;
|
|
|
|
}
|