blob: 9c0ed7dc3d74d75ccb9d29c01fa597c09c2d69b5 [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>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkin2a1646d2004-09-04 04:43:28 +000012#if defined(WIN32)
Lev Walkin64399722004-08-11 07:17:22 +000013#warning PLEASE STOP AND READ!
Lev Walkin93e9fe32004-09-17 06:46:10 +000014#warning localtime_r is implemented via localtime(), which may be not thread-safe.
15#warning gmtime_r is implemented via gmtime(), which may be not thread-safe.
Lev Walkin02a31552004-08-12 03:52:53 +000016#warning
17#warning You must fix the code by inserting appropriate locking
18#warning if you want to use asn_GT2time() or asn_UT2time().
Lev Walkin64399722004-08-11 07:17:22 +000019#warning PLEASE STOP AND READ!
Lev Walkin02a31552004-08-12 03:52:53 +000020
Lev Walkincec43b52004-09-02 05:20:39 +000021static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000022 struct tm *tm;
23 if((tm = localtime(tloc)))
24 return memcpy(result, tm, sizeof(struct tm));
25 return 0;
26}
27
Lev Walkincec43b52004-09-02 05:20:39 +000028static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
Lev Walkin02a31552004-08-12 03:52:53 +000029 struct tm *tm;
30 if((tm = gmtime(tloc)))
31 return memcpy(result, tm, sizeof(struct tm));
32 return 0;
33}
34
Lev Walkin93e9fe32004-09-17 06:46:10 +000035#define tzset() _tzset()
Lev Walkin75aacef2005-04-13 14:53:04 +000036#define putenv _putenv
Lev Walkin93e9fe32004-09-17 06:46:10 +000037#define _EMULATE_TIMEGM
38
39#endif /* WIN32 */
40
Lev Walkina460ba32004-10-20 15:40:04 +000041#if defined(sun)
42#define _EMULATE_TIMEGM
43#endif
44
Lev Walkin93e9fe32004-09-17 06:46:10 +000045/*
46 * Where to look for offset from GMT, Phase I.
47 * Several platforms are known.
48 */
49#if defined(__FreeBSD__) \
50 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
51 || (defined __GLIBC__ && __GLIBC__ >= 2)
52#undef HAVE_TM_GMTOFF
53#define HAVE_TM_GMTOFF
54#endif /* BSDs and newer glibc */
55
56/*
57 * Where to look for offset from GMT, Phase II.
58 */
59#ifdef HAVE_TM_GMTOFF
60#define GMTOFF(tm) ((tm).tm_gmtoff)
61#else /* HAVE_TM_GMTOFF */
62#define GMTOFF(tm) (-timezone)
63#endif /* HAVE_TM_GMTOFF */
64
65/*
66 * Override our GMTOFF decision for other known platforms.
67 */
68#ifdef __CYGWIN__
69#undef GMTOFF
70static long GMTOFF(struct tm a){
71 struct tm *lt;
72 time_t local_time, gmt_time;
73 long zone;
74
75 tzset();
76 gmt_time = time (NULL);
77
78 lt = gmtime(&gmt_time);
79
80 local_time = mktime(lt);
81 return (gmt_time - local_time);
82}
83#define _EMULATE_TIMEGM
84
85#endif /* __CYGWIN__ */
86
87#ifdef _EMULATE_TIMEGM
Lev Walkindb424002004-08-12 03:58:25 +000088static time_t timegm(struct tm *tm) {
89 time_t tloc;
90 char *tz;
Lev Walkine730f2b2004-08-12 07:47:03 +000091 char *buf;
Lev Walkindb424002004-08-12 03:58:25 +000092
93 tz = getenv("TZ");
Lev Walkina460ba32004-10-20 15:40:04 +000094 putenv("TZ=UTC");
Lev Walkin93e9fe32004-09-17 06:46:10 +000095 tzset();
Lev Walkindb424002004-08-12 03:58:25 +000096 tloc = mktime(tm);
Lev Walkine730f2b2004-08-12 07:47:03 +000097 if (tz) {
Lev Walkin93e9fe32004-09-17 06:46:10 +000098 int bufsize = strlen(tz) + 4;
99 buf = alloca(bufsize);
100 snprintf(buf, bufsize, "TZ=%s", tz);
Lev Walkine730f2b2004-08-12 07:47:03 +0000101 } else {
102 buf = "TZ=";
103 }
Lev Walkina460ba32004-10-20 15:40:04 +0000104 putenv(buf);
Lev Walkin93e9fe32004-09-17 06:46:10 +0000105 tzset();
Lev Walkindb424002004-08-12 03:58:25 +0000106 return tloc;
107}
Lev Walkin93e9fe32004-09-17 06:46:10 +0000108#endif /* _EMULATE_TIMEGM */
Lev Walkindb424002004-08-12 03:58:25 +0000109
Lev Walkin64399722004-08-11 07:17:22 +0000110
Lev Walkinf15320b2004-06-03 03:38:44 +0000111#ifndef __NO_ASN_TABLE__
112
113/*
114 * GeneralizedTime basic type description.
115 */
Lev Walkin5e033762004-09-29 13:26:15 +0000116static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000117 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
118 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
119 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120};
Lev Walkin5e033762004-09-29 13:26:15 +0000121asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 "GeneralizedTime",
Lev Walkindc06f6b2004-10-20 15:50:55 +0000123 "GeneralizedTime",
Lev Walkina9cc46e2004-09-22 16:06:28 +0000124 OCTET_STRING_free,
125 GeneralizedTime_print,
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 GeneralizedTime_constraint, /* Check validity of time */
127 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000128 GeneralizedTime_encode_der,
129 OCTET_STRING_decode_xer_utf8,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130 GeneralizedTime_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +0000132 asn_DEF_GeneralizedTime_tags,
133 sizeof(asn_DEF_GeneralizedTime_tags)
134 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
135 asn_DEF_GeneralizedTime_tags,
136 sizeof(asn_DEF_GeneralizedTime_tags)
137 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +0000138 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +0000139 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +0000140};
141
142#endif /* __NO_ASN_TABLE__ */
143
144/*
145 * Check that the time looks like the time.
146 */
147int
Lev Walkin5e033762004-09-29 13:26:15 +0000148GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000150 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 time_t tloc;
152
153 errno = EPERM; /* Just an unlikely error code */
Lev Walkin99006362004-08-07 03:52:26 +0000154 tloc = asn_GT2time(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 if(tloc == -1 && errno != EPERM) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000156 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000157 "%s: Invalid time format: %s (%s:%d)",
158 td->name, strerror(errno), __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 return -1;
160 }
161
162 return 0;
163}
164
Lev Walkina9cc46e2004-09-22 16:06:28 +0000165asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000166GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin99006362004-08-07 03:52:26 +0000167 int tag_mode, ber_tlv_tag_t tag,
168 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000169 GeneralizedTime_t *st = (GeneralizedTime_t *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000170 asn_enc_rval_t erval;
Lev Walkin99006362004-08-07 03:52:26 +0000171
172 /* If not canonical DER, re-encode into canonical DER. */
Lev Walkin5e033762004-09-29 13:26:15 +0000173 if(st->size && st->buf[st->size-1] != 0x5a) {
Lev Walkin99006362004-08-07 03:52:26 +0000174 struct tm tm;
175 time_t tloc;
176
177 errno = EPERM;
178 tloc = asn_GT2time(st, &tm, 1); /* Recognize time */
179 if(tloc == -1 && errno != EPERM) {
180 /* Failed to recognize time. Fail completely. */
181 erval.encoded = -1;
182 erval.failed_type = td;
183 erval.structure_ptr = ptr;
184 return erval;
185 }
186 st = asn_time2GT(0, &tm, 1); /* Save time canonically */
187 if(!st) {
188 /* Memory allocation failure. */
189 erval.encoded = -1;
190 erval.failed_type = td;
191 erval.structure_ptr = ptr;
192 return erval;
193 }
194 }
195
196 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
197
198 if(st != ptr) {
199 FREEMEM(st->buf);
200 FREEMEM(st);
201 }
202
203 return erval;
204}
205
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000207GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 int ilevel, enum xer_encoder_flags_e flags,
209 asn_app_consume_bytes_f *cb, void *app_key) {
210 OCTET_STRING_t st;
211
212 if(flags & XER_F_CANONICAL) {
213 char buf[32];
214 struct tm tm;
215 ssize_t ret;
216
217 errno = EPERM;
218 if(asn_GT2time((GeneralizedTime_t *)sptr, &tm, 1) == -1
219 && errno != EPERM)
220 _ASN_ENCODE_FAILED;
221
222 ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
223 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
224 tm.tm_hour, tm.tm_min, tm.tm_sec);
225 assert(ret > 0 && ret < (int)sizeof(buf));
226
227 st.buf = (uint8_t *)buf;
228 st.size = ret;
229 sptr = &st;
230 }
231
Lev Walkindc06f6b2004-10-20 15:50:55 +0000232 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000233 cb, app_key);
234}
235
Lev Walkinf15320b2004-06-03 03:38:44 +0000236int
Lev Walkin5e033762004-09-29 13:26:15 +0000237GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000239 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000240
Lev Walkind9bd7752004-06-05 08:17:50 +0000241 (void)td; /* Unused argument */
242 (void)ilevel; /* Unused argument */
243
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 if(st && st->buf) {
245 char buf[32];
246 struct tm tm;
247 int ret;
248
249 errno = EPERM;
Lev Walkin99006362004-08-07 03:52:26 +0000250 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000251 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000252
253 ret = snprintf(buf, sizeof(buf),
Lev Walkin99006362004-08-07 03:52:26 +0000254 "%04d-%02d-%02d %02d:%02d%02d (GMT)",
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
256 tm.tm_hour, tm.tm_min, tm.tm_sec);
Lev Walkind9bd7752004-06-05 08:17:50 +0000257 assert(ret > 0 && ret < (int)sizeof(buf));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000258 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000260 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 }
262}
263
Lev Walkinf15320b2004-06-03 03:38:44 +0000264time_t
Lev Walkin99006362004-08-07 03:52:26 +0000265asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 struct tm tm_s;
267 uint8_t *buf;
268 uint8_t *end;
Lev Walkin99006362004-08-07 03:52:26 +0000269 int gmtoff_h = 0;
270 int gmtoff_m = 0;
271 int gmtoff = 0; /* h + m */
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 int offset_specified = 0;
273 time_t tloc;
274
275 if(!st || !st->buf) {
276 errno = EINVAL;
277 return -1;
278 } else {
279 buf = st->buf;
280 end = buf + st->size;
281 }
282
283 if(st->size < 10) {
284 errno = EINVAL;
285 return -1;
286 }
287
288 /*
289 * Decode first 10 bytes: "AAAAMMJJhh"
290 */
291 memset(&tm_s, 0, sizeof(tm_s));
292#undef B2F
293#undef B2T
294#define B2F(var) do { \
295 unsigned ch = *buf; \
296 if(ch < 0x30 && ch > 0x39) { \
297 errno = EINVAL; \
298 return -1; \
299 } else { \
300 var = var * 10 + (ch - 0x30); \
301 buf++; \
302 } \
303 } while(0)
304#define B2T(var) B2F(tm_s.var)
305
306 B2T(tm_year); /* 1: A */
307 B2T(tm_year); /* 2: A */
308 B2T(tm_year); /* 3: A */
309 B2T(tm_year); /* 4: A */
310 B2T(tm_mon); /* 5: M */
311 B2T(tm_mon); /* 6: M */
312 B2T(tm_mday); /* 7: J */
313 B2T(tm_mday); /* 8: J */
314 B2T(tm_hour); /* 9: h */
315 B2T(tm_hour); /* 0: h */
316
317 if(buf == end) goto local_finish;
318
319 /*
320 * Parse [mm[ss[(.|,)ffff]]]
321 * ^^
322 */
323 switch(*buf) {
324 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
325 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
326 tm_s.tm_min = (*buf++) - 0x30;
327 if(buf == end) { errno = EINVAL; return -1; }
328 B2T(tm_min);
329 break;
330 case 0x2B: case 0x2D: /* +, - */
331 goto offset;
332 case 0x5A: /* Z */
333 goto utc_finish;
334 default:
335 errno = EINVAL;
336 return -1;
337 }
338
339 if(buf == end) goto local_finish;
340
341 /*
342 * Parse [mm[ss[(.|,)ffff]]]
343 * ^^
344 */
345 switch(*buf) {
346 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
347 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
348 tm_s.tm_sec = (*buf++) - 0x30;
349 if(buf == end) { errno = EINVAL; return -1; }
350 B2T(tm_sec);
351 break;
352 case 0x2B: case 0x2D: /* +, - */
353 goto offset;
354 case 0x5A: /* Z */
355 goto utc_finish;
356 default:
357 errno = EINVAL;
358 return -1;
359 }
360
361 if(buf == end) goto local_finish;
362
363 /*
364 * Parse [mm[ss[(.|,)ffff]]]
365 * ^ ^
366 */
367 switch(*buf) {
368 case 0x2C: case 0x2E: /* (.|,) */
369 /* Fractions of seconds are not supported
370 * by time_t or struct tm. Skip them */
371 for(buf++; buf < end; buf++) {
372 switch(*buf) {
373 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
374 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
375 continue;
376 default:
377 break;
378 }
379 break;
380 }
381 }
382
383 if(buf == end) goto local_finish;
384
385 switch(*buf) {
386 case 0x2B: case 0x2D: /* +, - */
387 goto offset;
388 case 0x5A: /* Z */
389 goto utc_finish;
390 default:
391 errno = EINVAL;
392 return -1;
393 }
394
395
396offset:
397
398 if(end - buf < 3) {
399 errno = EINVAL;
400 return -1;
401 }
402 buf++;
Lev Walkin99006362004-08-07 03:52:26 +0000403 B2F(gmtoff_h);
404 B2F(gmtoff_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 if(buf[-3] == 0x2D) /* Negative */
Lev Walkin99006362004-08-07 03:52:26 +0000406 gmtoff = -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 else
Lev Walkin99006362004-08-07 03:52:26 +0000408 gmtoff = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000409
410 if((end - buf) == 2) {
Lev Walkin99006362004-08-07 03:52:26 +0000411 B2F(gmtoff_m);
412 B2F(gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 } else if(end != buf) {
414 errno = EINVAL;
415 return -1;
416 }
417
Lev Walkin99006362004-08-07 03:52:26 +0000418 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
Lev Walkinf15320b2004-06-03 03:38:44 +0000419
420 /* Fall through */
421utc_finish:
422
423 offset_specified = 1;
424
425 /* Fall through */
426local_finish:
427
428 /*
429 * Validation.
430 */
431 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
432 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
433 || (tm_s.tm_hour > 23)
434 || (tm_s.tm_sec > 60)
435 ) {
436 errno = EINVAL;
437 return -1;
438 }
439
440 /* Canonicalize */
441 tm_s.tm_mon -= 1; /* 0 - 11 */
442 tm_s.tm_year -= 1900;
443 tm_s.tm_isdst = -1;
444
Lev Walkin99006362004-08-07 03:52:26 +0000445 tm_s.tm_sec -= gmtoff;
446
447 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
448
Lev Walkin02a31552004-08-12 03:52:53 +0000449 if(offset_specified) {
Lev Walkin99006362004-08-07 03:52:26 +0000450 tloc = timegm(&tm_s);
Lev Walkin02a31552004-08-12 03:52:53 +0000451 } else {
Lev Walkin99006362004-08-07 03:52:26 +0000452 /*
Lev Walkin5e033762004-09-29 13:26:15 +0000453 * Without an offset (or "Z"),
Lev Walkin99006362004-08-07 03:52:26 +0000454 * we can only guess that it is a local zone.
455 * Interpret it in this fashion.
456 */
457 tloc = mktime(&tm_s);
458 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 if(tloc == -1) {
460 errno = EINVAL;
461 return -1;
462 }
463
Lev Walkin99006362004-08-07 03:52:26 +0000464 if(ret_tm) {
465 if(as_gmt) {
466 if(offset_specified) {
467 *ret_tm = tm_s;
468 } else {
469 if(gmtime_r(&tloc, ret_tm) == 0) {
470 errno = EINVAL;
471 return -1;
472 }
473 }
474 } else {
475 if(localtime_r(&tloc, ret_tm) == 0) {
476 errno = EINVAL;
477 return -1;
478 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 }
480 }
481
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 return tloc;
483}
484
Lev Walkin99006362004-08-07 03:52:26 +0000485
486GeneralizedTime_t *
487asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
488 struct tm tm_s;
489 long gmtoff;
490 const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */
491 char *buf;
492 char *p;
493 int size;
494
495 /* Check arguments */
496 if(!tm) {
497 errno = EINVAL;
498 return 0;
499 }
500
501 /* Pre-allocate a buffer of sufficient yet small length */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000502 buf = (char *)MALLOC(buf_size);
Lev Walkin99006362004-08-07 03:52:26 +0000503 if(!buf) return 0;
504
505 gmtoff = GMTOFF(*tm);
506
507 if(force_gmt && gmtoff) {
508 tm_s = *tm;
509 tm_s.tm_sec -= gmtoff;
510 timegm(&tm_s); /* Fix the time */
Lev Walkin99006362004-08-07 03:52:26 +0000511 tm = &tm_s;
Lev Walkin659a7512004-08-11 07:35:08 +0000512#ifdef HAVE_TM_GMTOFF
513 assert(!GMTOFF(tm_s)); /* Will fix itself */
514#else
515 gmtoff = 0; /* Intervention required */
516#endif
Lev Walkin99006362004-08-07 03:52:26 +0000517 }
518
519 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
520 tm->tm_year + 1900,
521 tm->tm_mon + 1,
522 tm->tm_mday,
523 tm->tm_hour,
524 tm->tm_min,
525 tm->tm_sec
526 );
527 assert(size == 14);
528
529 p = buf + size;
530 if(force_gmt) {
Lev Walkin5e033762004-09-29 13:26:15 +0000531 *p++ = 0x5a; /* "Z" */
Lev Walkin99006362004-08-07 03:52:26 +0000532 *p++ = 0;
533 size++;
534 } else {
535 int ret = snprintf(p, buf_size - size, "%+03ld%02ld",
536 gmtoff / 3600, gmtoff % 3600);
537 assert(ret >= 5 && ret <= 7);
538 size += ret;
539 }
540
541 if(opt_gt) {
542 if(opt_gt->buf)
543 FREEMEM(opt_gt->buf);
544 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000545 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
Lev Walkin99006362004-08-07 03:52:26 +0000546 if(!opt_gt) { free(buf); return 0; }
547 }
548
Lev Walkin4d9528c2004-08-11 08:10:13 +0000549 opt_gt->buf = (unsigned char *)buf;
Lev Walkin99006362004-08-07 03:52:26 +0000550 opt_gt->size = size;
551
552 return opt_gt;
553}
554
555