1 /* gztime.c --- Convertion functions for GeneralizedTime.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31 * shishi_generalize_time:
32 * @handle: shishi handle as allocated by shishi_init().
33 * @t: C time to convert.
35 * Convert C time to KerberosTime. The string must not be deallocate
38 * Return value: Return a KerberosTime time string corresponding to C time t.
41 shishi_generalize_time (Shishi
* handle
, time_t t
)
46 strftime (handle
->gztime_buf
, sizeof (handle
->gztime_buf
),
49 return handle
->gztime_buf
;
53 * shishi_generalize_now:
54 * @handle: shishi handle as allocated by shishi_init().
56 * Convert current time to KerberosTime. The string must not be
57 * deallocate by caller.
59 * Return value: Return a KerberosTime time string corresponding to
63 shishi_generalize_now (Shishi
* handle
)
65 time_t t
= xtime (NULL
);
67 return shishi_generalize_time (handle
, t
);
71 * shishi_generalize_ctime:
72 * @handle: shishi handle as allocated by shishi_init().
73 * @t: KerberosTime to convert.
75 * Convert KerberosTime to C time.
77 * Return value: Returns C time corresponding to KerberosTime t.
80 shishi_generalize_ctime (Shishi
* handle
, const char *t
)
85 memset (&tm
, 0, sizeof (tm
));
87 sscanf (t
, "%4u%2u%2u%2u%2u%2uZ",
88 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
89 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
);
100 * @handle: shishi handle as allocated by shishi_init().
101 * @node: ASN.1 node to get time from.
102 * @field: Name of field in ASN.1 node to get time from.
103 * @t: newly allocated output array with zero terminated time string.
105 * Extract time from ASN.1 structure.
107 * Return value: Returns SHISHI_OK iff successful.
110 shishi_time (Shishi
* handle
, Shishi_asn1 node
, const char *field
, char **t
)
115 len
= SHISHI_GENERALIZEDTIME_LENGTH
+ 1;
118 res
= shishi_asn1_read_inline (handle
, node
, field
, *t
, &len
);
119 if (res
!= SHISHI_OK
)
122 if (len
<= SHISHI_GENERALIZEDTIME_LENGTH
)
124 shishi_error_printf (handle
, "Read time too short (%s)", *t
);
125 return SHISHI_ASN1_ERROR
;
128 (*t
)[SHISHI_GENERALIZEDTIME_LENGTH
] = '\0';
135 * @handle: shishi handle as allocated by shishi_init().
136 * @node: ASN.1 variable to read field from.
137 * @field: name of field in @node to read.
138 * @t: pointer to time field to set.
140 * Extract time from ASN.1 structure.
142 * Return value: Returns SHISHI_OK if successful,
143 * SHISHI_ASN1_NO_ELEMENT if the element do not exist,
144 * SHISHI_ASN1_NO_VALUE if the field has no value, ot
145 * SHISHI_ASN1_ERROR otherwise.
148 shishi_ctime (Shishi
* handle
, Shishi_asn1 node
, const char *field
, time_t *t
)
150 char str
[SHISHI_GENERALIZEDTIME_LENGTH
+ 1];
151 size_t strlen
= sizeof (str
);
154 rc
= shishi_asn1_read_inline (handle
, node
, field
, str
, &strlen
);
158 *t
= shishi_generalize_ctime (handle
, str
);