Add.
[shishi.git] / lib / gztime.c
blob14fe6a5c96f5a7b218a16dd5728583c01339bdb0
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
22 #include "internal.h"
24 /* Get xtime. */
25 #include "utils.h"
27 /* Get timegm. */
28 #include "timegm.h"
30 /**
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
36 * by caller.
38 * Return value: Return a KerberosTime time string corresponding to C time t.
39 **/
40 const char *
41 shishi_generalize_time (Shishi * handle, time_t t)
43 struct tm *tm;
45 tm = gmtime (&t);
46 strftime (handle->gztime_buf, sizeof (handle->gztime_buf),
47 "%Y%m%d%H%M%SZ", tm);
49 return handle->gztime_buf;
52 /**
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
60 * current time.
61 **/
62 const char *
63 shishi_generalize_now (Shishi * handle)
65 time_t t = xtime (NULL);
67 return shishi_generalize_time (handle, t);
70 /**
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.
78 **/
79 time_t
80 shishi_generalize_ctime (Shishi * handle, const char *t)
82 struct tm tm;
83 time_t ct;
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);
90 tm.tm_year -= 1900;
91 tm.tm_mon--;
93 ct = timegm (&tm);
95 return ct;
98 /**
99 * shishi_time:
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)
112 size_t len;
113 int res;
115 len = SHISHI_GENERALIZEDTIME_LENGTH + 1;
116 *t = xmalloc (len);
118 res = shishi_asn1_read_inline (handle, node, field, *t, &len);
119 if (res != SHISHI_OK)
120 return res;
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';
130 return SHISHI_OK;
134 * shishi_ctime:
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);
152 int rc;
154 rc = shishi_asn1_read_inline (handle, node, field, str, &strlen);
155 if (rc != SHISHI_OK)
156 return rc;
158 *t = shishi_generalize_ctime (handle, str);
160 return SHISHI_OK;