1 /* gztime.c --- Convertion functions for GeneralizedTime.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
29 * shishi_generalize_time:
30 * @handle: shishi handle as allocated by shishi_init().
31 * @t: C time to convert.
33 * Convert C time to KerberosTime. The string must not be deallocate
36 * Return value: Return a KerberosTime time string corresponding to C time t.
39 shishi_generalize_time (Shishi
* handle
, time_t t
)
44 strftime (handle
->gztime_buf
, sizeof (handle
->gztime_buf
),
47 return handle
->gztime_buf
;
51 * shishi_generalize_now:
52 * @handle: shishi handle as allocated by shishi_init().
54 * Convert current time to KerberosTime. The string must not be
55 * deallocate by caller.
57 * Return value: Return a KerberosTime time string corresponding to
61 shishi_generalize_now (Shishi
* handle
)
63 time_t t
= xtime (NULL
);
65 return shishi_generalize_time (handle
, t
);
69 * shishi_generalize_ctime:
70 * @handle: shishi handle as allocated by shishi_init().
71 * @t: KerberosTime to convert.
73 * Convert KerberosTime to C time.
75 * Return value: Returns C time corresponding to KerberosTime t.
78 shishi_generalize_ctime (Shishi
* handle
, const char *t
)
83 memset (&tm
, 0, sizeof (tm
));
85 sscanf (t
, "%4u%2u%2u%2u%2u%2uZ",
86 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
87 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
);
98 * @handle: shishi handle as allocated by shishi_init().
99 * @node: ASN.1 node to get time from.
100 * @field: Name of field in ASN.1 node to get time from.
101 * @t: newly allocated output array with zero terminated time string.
103 * Extract time from ASN.1 structure.
105 * Return value: Returns SHISHI_OK iff successful.
108 shishi_time (Shishi
* handle
, Shishi_asn1 node
, const char *field
, char **t
)
113 len
= SHISHI_GENERALIZEDTIME_LENGTH
+ 1;
116 res
= shishi_asn1_read_inline (handle
, node
, field
, *t
, &len
);
117 if (res
!= SHISHI_OK
)
120 if (len
<= SHISHI_GENERALIZEDTIME_LENGTH
)
122 shishi_error_printf (handle
, "Read time too short (%s)", *t
);
123 return SHISHI_ASN1_ERROR
;
126 (*t
)[SHISHI_GENERALIZEDTIME_LENGTH
] = '\0';
133 * @handle: shishi handle as allocated by shishi_init().
134 * @node: ASN.1 variable to read field from.
135 * @field: name of field in @node to read.
136 * @t: pointer to time field to set.
138 * Extract time from ASN.1 structure.
140 * Return value: Returns SHISHI_OK if successful,
141 * SHISHI_ASN1_NO_ELEMENT if the element do not exist,
142 * SHISHI_ASN1_NO_VALUE if the field has no value, ot
143 * SHISHI_ASN1_ERROR otherwise.
146 shishi_ctime (Shishi
* handle
, Shishi_asn1 node
, const char *field
, time_t *t
)
148 char str
[SHISHI_GENERALIZEDTIME_LENGTH
+ 1];
149 size_t strlen
= sizeof (str
);
152 rc
= shishi_asn1_read_inline (handle
, node
, field
, str
, &strlen
);
156 *t
= shishi_generalize_ctime (handle
, str
);