1 /* time.c - UTCTime and GeneralizedTime helper
2 * Copyright (C) 2001, 2003, 2005, 2012 g10 Code GmbH
4 * This file is part of KSBA.
6 * KSBA is free software; you can redistribute it and/or modify
7 * it under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * KSBA is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * You should have received a copies of the GNU General Public License
27 * and the GNU Lesser General Public License along with this program;
28 * if not, see <http://www.gnu.org/licenses/>.
42 /* Converts an UTCTime or GeneralizedTime to ISO format. Sets the
43 returns string to empty on error and returns the error code. The
44 function figures automagically the right format. fixme: Currently
45 we only zupport Zulu time and no timezone which is sufficient for
46 DER encoding. It IS_UTCTIME is true, the function assumes that the
47 time is in UTCTime and thus allows to parse UTCTimes without
48 seconds (which is legal ASN.1; however Gutmann claims that the
49 rules changed in 1996 to always require seconds; OTOH, Dubuisson's
50 reference book from 2001 doesn't say so). */
52 _ksba_asntime_to_iso (const char *buffer, size_t length, int is_utctime,
53 ksba_isotime_t timebuf)
60 for (s=buffer, n=0; n < length && digitp (s); n++, s++)
64 if ((n != 10 && n != 12) || *s != 'Z')
65 return gpg_error (GPG_ERR_INV_TIME);
67 else if ((n != 12 && n != 14) || *s != 'Z')
68 return gpg_error (GPG_ERR_INV_TIME);
71 if (n==12 || n == 10 ) /* UTCTime with or without seconds. */
74 timebuf[0] = year < 50? '2': '1';
75 timebuf[1] = year < 50? '0': '9';
76 memcpy (timebuf+2, s, 6);
81 memcpy (timebuf, s, 8);
85 if (n == 10) /* UTCTime w/0 seconds. */
87 memcpy (timebuf+9, s, 4);
88 timebuf[13] = timebuf[14] = '0';
92 memcpy (timebuf+9, s, 6);
100 /* Return 0 if ATIME has the proper format (e.g. "19660205T131415"). */
102 _ksba_assert_time_format (const ksba_isotime_t atime)
108 return gpg_error (GPG_ERR_NO_VALUE);
110 for (s=atime, i=0; i < 8; i++, s++)
112 return gpg_error (GPG_ERR_BUG);
114 return gpg_error (GPG_ERR_BUG);
115 for (s++, i=9; i < 15; i++, s++)
117 return gpg_error (GPG_ERR_BUG);
119 return gpg_error (GPG_ERR_BUG);
124 /* Copy ISO time S to D. This is a function so that we can detect
125 faulty time formats. */
127 _ksba_copy_time (ksba_isotime_t d, const ksba_isotime_t s)
131 else if ( _ksba_assert_time_format (s) )
133 fprintf (stderr, "BUG: invalid isotime buffer\n");
141 /* Compare the time strings A and B. Return 0 if they show the very
142 same time, return 1 if A is newer than B and -1 if A is older than
145 _ksba_cmp_time (const ksba_isotime_t a, const ksba_isotime_t b)
147 return strcmp (a, b);
150 /* Fill the TIMEBUF with the current time (UTC of course). */
152 _ksba_current_time (ksba_isotime_t timebuf)
154 time_t epoch = time (NULL);
159 tp = gmtime_r ( &epoch, &tmbuf);
161 tp = gmtime ( &epoch );
163 return snprintf (timebuf, sizeof (ksba_isotime_t),
164 "%04d%02d%02dT%02d%02d%02d",
165 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
166 tp->tm_hour, tp->tm_min, tp->tm_sec);