blob: 9aa64ce5f9ed343e6b0bcf1f8c1ffcb56389fd71 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
2 * project 2001.
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project 2008.
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
Adam Langley51fcd872014-10-02 15:35:35 -070058#if !defined(_POSIX_C_SOURCE)
Adam Langleyad912f32014-10-02 13:58:19 -070059#define _POSIX_C_SOURCE 201410L /* for gmtime_r */
Adam Langley51fcd872014-10-02 15:35:35 -070060#endif
Adam Langley95c29f32014-06-20 12:00:00 -070061
Adam Langley32ab7b02015-03-20 16:38:14 -070062#if defined(__MINGW32__)
63#define MINGW_HAS_SECURE_API 1 /* supplied by libmingwex */
64#include <sec_api/time_s.h> /* for correct definition of gmtime_s */
65#undef MINGW_HAS_SECURE_API
66#endif
67
Adam Langley95c29f32014-06-20 12:00:00 -070068#include <openssl/time_support.h>
69
70#define SECS_PER_DAY (24 * 60 * 60)
71
72struct tm *OPENSSL_gmtime(const time_t *time, struct tm *result) {
73#if defined(OPENSSL_WINDOWS)
Adam Langleyded93582014-07-31 15:23:51 -070074 if (gmtime_s(result, time)) {
Adam Langley95c29f32014-06-20 12:00:00 -070075 return NULL;
76 }
77 return result;
78#else
79 return gmtime_r(time, result);
80#endif
81}
82
83/* Convert date to and from julian day Uses Fliegel & Van Flandern algorithm */
84static long date_to_julian(int y, int m, int d) {
85 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
86 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
87 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
88}
89
90static void julian_to_date(long jd, int *y, int *m, int *d) {
91 long L = jd + 68569;
92 long n = (4 * L) / 146097;
93 long i, j;
94
95 L = L - (146097 * n + 3) / 4;
96 i = (4000 * (L + 1)) / 1461001;
97 L = L - (1461 * i) / 4 + 31;
98 j = (80 * L) / 2447;
99 *d = L - (2447 * j) / 80;
100 L = j / 11;
101 *m = j + 2 - (12 * L);
102 *y = 100 * (n - 49) + i + L;
103}
104
105/* Convert tm structure and offset into julian day and seconds */
106static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
107 long *pday, int *psec) {
108 int offset_hms, offset_day;
109 long time_jd;
110 int time_year, time_month, time_day;
111 /* split offset into days and day seconds */
112 offset_day = offset_sec / SECS_PER_DAY;
113 /* Avoid sign issues with % operator */
114 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
115 offset_day += off_day;
116 /* Add current time seconds to offset */
117 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
118 /* Adjust day seconds if overflow */
119 if (offset_hms >= SECS_PER_DAY) {
120 offset_day++;
121 offset_hms -= SECS_PER_DAY;
122 } else if (offset_hms < 0) {
123 offset_day--;
124 offset_hms += SECS_PER_DAY;
125 }
126
127 /* Convert date of time structure into a Julian day number. */
128
129 time_year = tm->tm_year + 1900;
130 time_month = tm->tm_mon + 1;
131 time_day = tm->tm_mday;
132
133 time_jd = date_to_julian(time_year, time_month, time_day);
134
135 /* Work out Julian day of new date */
136 time_jd += offset_day;
137
David Benjamin6eb000d2015-02-11 01:17:41 -0500138 if (time_jd < 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700139 return 0;
David Benjamin6eb000d2015-02-11 01:17:41 -0500140 }
Adam Langley95c29f32014-06-20 12:00:00 -0700141
142 *pday = time_jd;
143 *psec = offset_hms;
144 return 1;
145}
146
147int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) {
148 int time_sec, time_year, time_month, time_day;
149 long time_jd;
150
151 /* Convert time and offset into julian day and seconds */
David Benjamin6eb000d2015-02-11 01:17:41 -0500152 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700153 return 0;
David Benjamin6eb000d2015-02-11 01:17:41 -0500154 }
Adam Langley95c29f32014-06-20 12:00:00 -0700155
156 /* Convert Julian day back to date */
157
158 julian_to_date(time_jd, &time_year, &time_month, &time_day);
159
David Benjamin6eb000d2015-02-11 01:17:41 -0500160 if (time_year < 1900 || time_year > 9999) {
Adam Langley95c29f32014-06-20 12:00:00 -0700161 return 0;
David Benjamin6eb000d2015-02-11 01:17:41 -0500162 }
Adam Langley95c29f32014-06-20 12:00:00 -0700163
164 /* Update tm structure */
165
166 tm->tm_year = time_year - 1900;
167 tm->tm_mon = time_month - 1;
168 tm->tm_mday = time_day;
169
170 tm->tm_hour = time_sec / 3600;
171 tm->tm_min = (time_sec / 60) % 60;
172 tm->tm_sec = time_sec % 60;
173
174 return 1;
175}
176
177int OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
178 const struct tm *to) {
179 int from_sec, to_sec, diff_sec;
180 long from_jd, to_jd, diff_day;
181
182 if (!julian_adj(from, 0, 0, &from_jd, &from_sec)) {
183 return 0;
184 }
185 if (!julian_adj(to, 0, 0, &to_jd, &to_sec)) {
186 return 0;
187 }
188
189 diff_day = to_jd - from_jd;
190 diff_sec = to_sec - from_sec;
191 /* Adjust differences so both positive or both negative */
192 if (diff_day > 0 && diff_sec < 0) {
193 diff_day--;
194 diff_sec += SECS_PER_DAY;
195 }
196 if (diff_day < 0 && diff_sec > 0) {
197 diff_day++;
198 diff_sec -= SECS_PER_DAY;
199 }
200
201 if (pday) {
202 *pday = (int)diff_day;
203 }
204 if (psec) {
205 *psec = diff_sec;
206 }
207
208 return 1;
209}