summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/o_time.c
diff options
context:
space:
mode:
authorbeck <>2021-10-27 09:50:57 +0000
committerbeck <>2021-10-27 09:50:57 +0000
commit5b11389ce53ed7fffc2ec4099109bd281776223b (patch)
treee390498509bc0c0f8cb6b474d56f38580879b460 /src/lib/libcrypto/o_time.c
parent9708d91b72dd881ff18329e76e72608f97555822 (diff)
downloadopenbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.tar.gz
openbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.tar.bz2
openbsd-5b11389ce53ed7fffc2ec4099109bd281776223b.zip
Add ASN1_TIME_diff from OpenSSL.
The symbol is not yet exposed and will show up with tb@'s forthcoming bump ok tb@ jsing@
Diffstat (limited to 'src/lib/libcrypto/o_time.c')
-rw-r--r--src/lib/libcrypto/o_time.c83
1 files changed, 82 insertions, 1 deletions
diff --git a/src/lib/libcrypto/o_time.c b/src/lib/libcrypto/o_time.c
index 9b2e7e5b5e..3f164c7fde 100644
--- a/src/lib/libcrypto/o_time.c
+++ b/src/lib/libcrypto/o_time.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: o_time.c,v 1.15 2014/06/12 15:49:27 deraadt Exp $ */ 1/* $OpenBSD: o_time.c,v 1.16 2021/10/27 09:50:56 beck Exp $ */
2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL 2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2001. 3 * project 2001.
4 */ 4 */
@@ -72,6 +72,8 @@
72 72
73static long date_to_julian(int y, int m, int d); 73static long date_to_julian(int y, int m, int d);
74static void julian_to_date(long jd, int *y, int *m, int *d); 74static void julian_to_date(long jd, int *y, int *m, int *d);
75static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
76 long *pday, int *psec);
75 77
76int 78int
77OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec) 79OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
@@ -131,6 +133,85 @@ OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
131 133
132} 134}
133 135
136int
137OPENSSL_gmtime_diff(int *pday, int *psec, const struct tm *from,
138 const struct tm *to)
139{
140 int from_sec, to_sec, diff_sec;
141 long from_jd, to_jd, diff_day;
142
143 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
144 return 0;
145 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
146 return 0;
147 diff_day = to_jd - from_jd;
148 diff_sec = to_sec - from_sec;
149 /* Adjust differences so both positive or both negative */
150 if (diff_day > 0 && diff_sec < 0) {
151 diff_day--;
152 diff_sec += SECS_PER_DAY;
153 }
154 if (diff_day < 0 && diff_sec > 0) {
155 diff_day++;
156 diff_sec -= SECS_PER_DAY;
157 }
158
159 if (pday)
160 *pday = (int)diff_day;
161 if (psec)
162 *psec = diff_sec;
163
164 return 1;
165
166}
167
168/* Convert tm structure and offset into julian day and seconds */
169static int
170julian_adj(const struct tm *tm, int off_day, long offset_sec, long *pday,
171 int *psec)
172{
173 int time_year, time_month, time_day;
174 long offset_day, time_jd;
175 int offset_hms;
176
177 /* split offset into days and day seconds */
178 offset_day = offset_sec / SECS_PER_DAY;
179 /* Avoid sign issues with % operator */
180 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
181 offset_day += off_day;
182 /* Add current time seconds to offset */
183 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
184 /* Adjust day seconds if overflow */
185 if (offset_hms >= SECS_PER_DAY) {
186 offset_day++;
187 offset_hms -= SECS_PER_DAY;
188 } else if (offset_hms < 0) {
189 offset_day--;
190 offset_hms += SECS_PER_DAY;
191 }
192
193 /*
194 * Convert date of time structure into a Julian day number.
195 */
196
197 time_year = tm->tm_year + 1900;
198 time_month = tm->tm_mon + 1;
199 time_day = tm->tm_mday;
200
201 time_jd = date_to_julian(time_year, time_month, time_day);
202
203 /* Work out Julian day of new date */
204 time_jd += offset_day;
205
206 if (time_jd < 0)
207 return 0;
208
209 *pday = time_jd;
210 *psec = offset_hms;
211
212 return 1;
213}
214
134/* Convert date to and from julian day 215/* Convert date to and from julian day
135 * Uses Fliegel & Van Flandern algorithm 216 * Uses Fliegel & Van Flandern algorithm
136 */ 217 */