blob: 2a28d7344aab378f60a06c3acfc60b8893c165f7 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina460ba32004-10-20 15:40:04 +00005#define _POSIX_PTHREAD_SEMANTICS /* for Sun */
6#define _REENTRANT /* for Sun */
Lev Walkina9cc46e2004-09-22 16:06:28 +00007#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <GeneralizedTime.h>
9#include <time.h>
10#include <errno.h>
11#ifndef __NO_ASSERT_H__
12#include <assert.h>
13#endif /* __NO_ASSERT_H__ */
14
Lev Walkin2a1646d2004-09-04 04:43:28 +000015#if defined(WIN32)
Lev Walkin64399722004-08-11 07:17:22 +000016#warning PLEASE STOP AND READ!
Lev Walkin93e9fe32004-09-17 06:46:10 +000017#warning localtime_r is implemented via localtime(), which may be not thread-safe.
18#warning gmtime_r is implemented via gmtime(), which may be not thread-safe.
Lev Walkin02a31552004-08-12 03:52:53 +000019#warning
20#warning You must fix the code by inserting appropriate locking
21#warning if you want to use asn_GT2time() or asn_UT2time().
Lev Walkin64399722004-08-11 07:17:22 +000022#warning PLEASE STOP AND READ!
Lev Walkin02a31552004-08-12 03:52:53 +000023
Lev Walkincec43b52004-09-02 05:20:39 +000024static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000025 struct tm *tm;
26 if((tm = localtime(tloc)))
27 return memcpy(result, tm, sizeof(struct tm));
28 return 0;
29}
30
Lev Walkincec43b52004-09-02 05:20:39 +000031static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000032 struct tm *tm;
33 if((tm = gmtime(tloc)))
34 return memcpy(result, tm, sizeof(struct tm));
35 return 0;
36}
37
Lev Walkin93e9fe32004-09-17 06:46:10 +000038#define tzset() _tzset()
Lev Walkina460ba32004-10-20 15:40:04 +000039#define putenv() _putenv()
Lev Walkin93e9fe32004-09-17 06:46:10 +000040#define _EMULATE_TIMEGM
41
42#endif /* WIN32 */
43
Lev Walkina460ba32004-10-20 15:40:04 +000044#if defined(sun)
45#define _EMULATE_TIMEGM
46#endif
47
Lev Walkin93e9fe32004-09-17 06:46:10 +000048/*
49 * Where to look for offset from GMT, Phase I.
50 * Several platforms are known.
51 */
52#if defined(__FreeBSD__) \
53 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
54 || (defined __GLIBC__ && __GLIBC__ >= 2)
55#undef HAVE_TM_GMTOFF
56#define HAVE_TM_GMTOFF
57#endif /* BSDs and newer glibc */
58
59/*
60 * Where to look for offset from GMT, Phase II.
61 */
62#ifdef HAVE_TM_GMTOFF
63#define GMTOFF(tm) ((tm).tm_gmtoff)
64#else /* HAVE_TM_GMTOFF */
65#define GMTOFF(tm) (-timezone)
66#endif /* HAVE_TM_GMTOFF */
67
68/*
69 * Override our GMTOFF decision for other known platforms.
70 */
71#ifdef __CYGWIN__
72#undef GMTOFF
73static long GMTOFF(struct tm a){
74 struct tm *lt;
75 time_t local_time, gmt_time;
76 long zone;
77
78 tzset();
79 gmt_time = time (NULL);
80
81 lt = gmtime(&gmt_time);
82
83 local_time = mktime(lt);
84 return (gmt_time - local_time);
85}
86#define _EMULATE_TIMEGM
87
88#endif /* __CYGWIN__ */
89
90#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +000091static time_t timegm(struct tm *tm) {
92 time_t tloc;
93 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000094 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000095
96 tz = getenv("TZ");
Lev Walkina460ba32004-10-20 15:40:04 +000097 putenv("TZ=UTC");
Lev Walkin93e9fe32004-09-17 06:46:10 +000098 tzset();
Lev Walkindb424002004-08-12 03:58:25 +000099 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +0000100 if (tz) {
Lev Walkin93e9fe32004-09-17 06:46:10 +0000101 int bufsize = strlen(tz) + 4;
102 buf = alloca(bufsize);
103 snprintf(buf, bufsize, "TZ=%s", tz);
Lev Walkine730f2b2004-08-12 07:47:03 +0000104 } else {
105 buf = "TZ=";
106 }
Lev Walkina460ba32004-10-20 15:40:04 +0000107 putenv(buf);
Lev Walkin93e9fe32004-09-17 06:46:10 +0000108 tzset();
Lev Walkindb424002004-08-12 03:58:25 +0000109 return tloc;
110}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000111#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000112
Lev Walkin64399722004-08-11 07:17:22 +0000113
Lev Walkinf15320b2004-06-03 03:38:44 +0000114#ifndef __NO_ASN_TABLE__
115
116/*
117 * GeneralizedTime basic type description.
118 */
Lev Walkin5e033762004-09-29 13:26:15 +0000119static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000120 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
121 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
122 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000123};
Lev Walkin5e033762004-09-29 13:26:15 +0000124asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000126 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 OCTET_STRING_free,
128 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 GeneralizedTime_constraint, /* Check validity of time */
130 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000131 GeneralizedTime_encode_der,
132 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000133 GeneralizedTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000135 asn_DEF_GeneralizedTime_tags,
136 sizeof(asn_DEF_GeneralizedTime_tags)
137 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
138 asn_DEF_GeneralizedTime_tags,
139 sizeof(asn_DEF_GeneralizedTime_tags)
140 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +0000141 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000142 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000143};
144
145#endif /* __NO_ASN_TABLE__ */
146
147/*
148 * Check that the time looks like the time.
149 */
150int
Lev Walkin5e033762004-09-29 13:26:15 +0000151GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000153 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 time_t tloc;
155
156 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000157 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000159 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000160 "%s: Invalid time format: %s (%s:%d)",
161 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 return -1;
163 }
164
165 return 0;
166}
167
Lev Walkina9cc46e2004-09-22 16:06:28 +0000168asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000169GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin99006362004-08-07 03:52:26 +0000170 int tag_mode, ber_tlv_tag_t tag,
171 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000172 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173 asn_enc_rval_t erval;
Lev Walkin99006362004-08-07 03:52:26 +0000174
175 /* If not canonical DER, re-encode into canonical DER. */
Lev Walkin5e033762004-09-29 13:26:15 +0000176 if(st->size && st->buf[st->size-1] != 0x5a) {
Lev Walkin99006362004-08-07 03:52:26 +0000177 struct tm tm;
178 time_t tloc;
179
180 errno = EPERM;
181 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
182 if(tloc == -1 && errno != EPERM) {
183 /* Failed to recognize time. Fail completely. */
184 erval.encoded = -1;
185 erval.failed_type = td;
186 erval.structure_ptr = ptr;
187 return erval;
188 }
189 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
190 if(!st) {
191 /* Memory allocation failure. */
192 erval.encoded = -1;
193 erval.failed_type = td;
194 erval.structure_ptr = ptr;
195 return erval;
196 }
197 }
198
199 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
200
201 if(st != ptr) {
202 FREEMEM(st->buf);
203 FREEMEM(st);
204 }
205
206 return erval;
207}
208
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000210GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211 int ilevel, enum xer_encoder_flags_e flags,
212 asn_app_consume_bytes_f *cb, void *app_key) {
213 OCTET_STRING_t st;
214
215 if(flags & XER_F_CANONICAL) {
216 char buf[32];
217 struct tm tm;
218 ssize_t ret;
219
220 errno = EPERM;
221 if(asn_GT2time((GeneralizedTime_t *)sptr, &tm, 1) == -1
222 && errno != EPERM)
223 _ASN_ENCODE_FAILED;
224
225 ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
226 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
227 tm.tm_hour, tm.tm_min, tm.tm_sec);
228 assert(ret > 0 && ret < (int)sizeof(buf));
229
230 st.buf = (uint8_t *)buf;
231 st.size = ret;
232 sptr = &st;
233 }
234
Lev Walkindc06f6b2004-10-20 15:50:55 +0000235 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000236 cb, app_key);
237}
238
Lev Walkinf15320b2004-06-03 03:38:44 +0000239int
Lev Walkin5e033762004-09-29 13:26:15 +0000240GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000241 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000242 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243
Lev Walkind9bd7752004-06-05 08:17:50 +0000244 (void)td; /* Unused argument */
245 (void)ilevel; /* Unused argument */
246
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 if(st && st->buf) {
248 char buf[32];
249 struct tm tm;
250 int ret;
251
252 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000253 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000254 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000255
256 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000257 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
259 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000260 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000261 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000263 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 }
265}
266
Lev Walkinf15320b2004-06-03 03:38:44 +0000267time_t
Lev Walkin99006362004-08-07 03:52:26 +0000268asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 struct tm tm_s;
270 uint8_t *buf;
271 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000272 int gmtoff_h = 0;
273 int gmtoff_m = 0;
274 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 int offset_specified = 0;
276 time_t tloc;
277
278 if(!st || !st->buf) {
279 errno = EINVAL;
280 return -1;
281 } else {
282 buf = st->buf;
283 end = buf + st->size;
284 }
285
286 if(st->size < 10) {
287 errno = EINVAL;
288 return -1;
289 }
290
291 /*
292 * Decode first 10 bytes: "AAAAMMJJhh"
293 */
294 memset(&tm_s, 0, sizeof(tm_s));
295#undef B2F
296#undef B2T
297#define B2F(var) do { \
298 unsigned ch = *buf; \
299 if(ch < 0x30 && ch > 0x39) { \
300 errno = EINVAL; \
301 return -1; \
302 } else { \
303 var = var * 10 + (ch - 0x30); \
304 buf++; \
305 } \
306 } while(0)
307#define B2T(var) B2F(tm_s.var)
308
309 B2T(tm_year); /* 1: A */
310 B2T(tm_year); /* 2: A */
311 B2T(tm_year); /* 3: A */
312 B2T(tm_year); /* 4: A */
313 B2T(tm_mon); /* 5: M */
314 B2T(tm_mon); /* 6: M */
315 B2T(tm_mday); /* 7: J */
316 B2T(tm_mday); /* 8: J */
317 B2T(tm_hour); /* 9: h */
318 B2T(tm_hour); /* 0: h */
319
320 if(buf == end) goto local_finish;
321
322 /*
323 * Parse [mm[ss[(.|,)ffff]]]
324 * ^^
325 */
326 switch(*buf) {
327 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
328 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
329 tm_s.tm_min = (*buf++) - 0x30;
330 if(buf == end) { errno = EINVAL; return -1; }
331 B2T(tm_min);
332 break;
333 case 0x2B: case 0x2D: /* +, - */
334 goto offset;
335 case 0x5A: /* Z */
336 goto utc_finish;
337 default:
338 errno = EINVAL;
339 return -1;
340 }
341
342 if(buf == end) goto local_finish;
343
344 /*
345 * Parse [mm[ss[(.|,)ffff]]]
346 * ^^
347 */
348 switch(*buf) {
349 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
350 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
351 tm_s.tm_sec = (*buf++) - 0x30;
352 if(buf == end) { errno = EINVAL; return -1; }
353 B2T(tm_sec);
354 break;
355 case 0x2B: case 0x2D: /* +, - */
356 goto offset;
357 case 0x5A: /* Z */
358 goto utc_finish;
359 default:
360 errno = EINVAL;
361 return -1;
362 }
363
364 if(buf == end) goto local_finish;
365
366 /*
367 * Parse [mm[ss[(.|,)ffff]]]
368 * ^ ^
369 */
370 switch(*buf) {
371 case 0x2C: case 0x2E: /* (.|,) */
372 /* Fractions of seconds are not supported
373 * by time_t or struct tm. Skip them */
374 for(buf++; buf < end; buf++) {
375 switch(*buf) {
376 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
377 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
378 continue;
379 default:
380 break;
381 }
382 break;
383 }
384 }
385
386 if(buf == end) goto local_finish;
387
388 switch(*buf) {
389 case 0x2B: case 0x2D: /* +, - */
390 goto offset;
391 case 0x5A: /* Z */
392 goto utc_finish;
393 default:
394 errno = EINVAL;
395 return -1;
396 }
397
398
399offset:
400
401 if(end - buf < 3) {
402 errno = EINVAL;
403 return -1;
404 }
405 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000406 B2F(gmtoff_h);
407 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000409 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000410 else
Lev Walkin99006362004-08-07 03:52:26 +0000411 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000412
413 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000414 B2F(gmtoff_m);
415 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 } else if(end != buf) {
417 errno = EINVAL;
418 return -1;
419 }
420
Lev Walkin99006362004-08-07 03:52:26 +0000421 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000422
423 /* Fall through */
424utc_finish:
425
426 offset_specified = 1;
427
428 /* Fall through */
429local_finish:
430
431 /*
432 * Validation.
433 */
434 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
435 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
436 || (tm_s.tm_hour > 23)
437 || (tm_s.tm_sec > 60)
438 ) {
439 errno = EINVAL;
440 return -1;
441 }
442
443 /* Canonicalize */
444 tm_s.tm_mon -= 1; /* 0 - 11 */
445 tm_s.tm_year -= 1900;
446 tm_s.tm_isdst = -1;
447
Lev Walkin99006362004-08-07 03:52:26 +0000448 tm_s.tm_sec -= gmtoff;
449
450 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
451
Lev Walkin02a31552004-08-12 03:52:53 +0000452 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000453 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000454 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000455 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000456 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000457 * we can only guess that it is a local zone.
458 * Interpret it in this fashion.
459 */
460 tloc = mktime(&tm_s);
461 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000462 if(tloc == -1) {
463 errno = EINVAL;
464 return -1;
465 }
466
Lev Walkin99006362004-08-07 03:52:26 +0000467 if(ret_tm) {
468 if(as_gmt) {
469 if(offset_specified) {
470 *ret_tm = tm_s;
471 } else {
472 if(gmtime_r(&tloc, ret_tm) == 0) {
473 errno = EINVAL;
474 return -1;
475 }
476 }
477 } else {
478 if(localtime_r(&tloc, ret_tm) == 0) {
479 errno = EINVAL;
480 return -1;
481 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 }
483 }
484
Lev Walkinf15320b2004-06-03 03:38:44 +0000485 return tloc;
486}
487
Lev Walkin99006362004-08-07 03:52:26 +0000488
489GeneralizedTime_t *
490asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
491 struct tm tm_s;
492 long gmtoff;
493 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
494 char *buf;
495 char *p;
496 int size;
497
498 /* Check arguments */
499 if(!tm) {
500 errno = EINVAL;
501 return 0;
502 }
503
504 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000505 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000506 if(!buf) return 0;
507
508 gmtoff = GMTOFF(*tm);
509
510 if(force_gmt && gmtoff) {
511 tm_s = *tm;
512 tm_s.tm_sec -= gmtoff;
513 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000514 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000515#ifdef HAVE_TM_GMTOFF
516 assert(!GMTOFF(tm_s)); /* Will fix itself */
517#else
518 gmtoff = 0; /* Intervention required */
519#endif
Lev Walkin99006362004-08-07 03:52:26 +0000520 }
521
522 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
523 tm->tm_year + 1900,
524 tm->tm_mon + 1,
525 tm->tm_mday,
526 tm->tm_hour,
527 tm->tm_min,
528 tm->tm_sec
529 );
530 assert(size == 14);
531
532 p = buf + size;
533 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000534 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000535 *p++ = 0;
536 size++;
537 } else {
538 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
539 gmtoff / 3600, gmtoff % 3600);
540 assert(ret >= 5 && ret <= 7);
541 size += ret;
542 }
543
544 if(opt_gt) {
545 if(opt_gt->buf)
546 FREEMEM(opt_gt->buf);
547 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000548 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000549 if(!opt_gt) { free(buf); return 0; }
550 }
551
Lev Walkin4d9528c2004-08-11 08:10:13 +0000552 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000553 opt_gt->size = size;
554
555 return opt_gt;
556}
557
558