diff options
author | Pavel Reichl <preichl@redhat.com> | 2015-01-20 16:27:41 -0500 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2015-03-05 20:28:35 +0100 |
commit | 5a5c5cdeb92f4012fc75fd717bfea06598f68f12 (patch) | |
tree | 21388284d228d651f9835db9d6cb5ea0cbb2ae2d /src/util/util.c | |
parent | 01f78f755fde63997ccfded71fb8395569b11430 (diff) | |
download | sssd-5a5c5cdeb92f4012fc75fd717bfea06598f68f12.tar.gz sssd-5a5c5cdeb92f4012fc75fd717bfea06598f68f12.tar.xz sssd-5a5c5cdeb92f4012fc75fd717bfea06598f68f12.zip |
UTIL: convert GeneralizedTime to unix time
New utility function *sss_utc_to_time_t* to convert GeneralizedTime to
unix time.
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Diffstat (limited to 'src/util/util.c')
-rw-r--r-- | src/util/util.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/util/util.c b/src/util/util.c index 613c559bb..cfd26a58b 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "config.h" #include <ctype.h> #include <netdb.h> #include <poll.h> @@ -26,6 +27,7 @@ #include <arpa/inet.h> #include <talloc.h> #include <dhash.h> +#include <time.h> #include "util/util.h" #include "util/sss_utf8.h" @@ -904,3 +906,54 @@ errno_t sss_fd_nonblocking(int fd) return EOK; } + +/* Convert GeneralizedTime (http://en.wikipedia.org/wiki/GeneralizedTime) + * to unix time (seconds since epoch). Use UTC time zone. + */ +errno_t sss_utc_to_time_t(const char *str, const char *format, time_t *_unix_time) +{ + char *end; + struct tm tm; + size_t len; + time_t ut; + + if (str == NULL) { + return EINVAL; + } + + len = strlen(str); + if (str[len-1] != 'Z') { + DEBUG(SSSDBG_TRACE_INTERNAL, + "%s does not seem to be in UTZ time zone.\n", str); + return ERR_TIMESPEC_NOT_SUPPORTED; + } + + memset(&tm, 0, sizeof(tm)); + + end = strptime(str, format, &tm); + /* not all characters from format were matched */ + if (end == NULL) { + DEBUG(SSSDBG_TRACE_INTERNAL, + "String [%s] failed to match format [%s].\n", str, format); + return EINVAL; + } + + /* str is 'longer' than format */ + if (*end != '\0') { + DEBUG(SSSDBG_TRACE_INTERNAL, + "String [%s] is longer than format [%s].\n", str, format); + return EINVAL; + } + + ut = mktime(&tm); + if (ut == -1) { + DEBUG(SSSDBG_TRACE_INTERNAL, + "mktime failed to convert [%s].\n", str); + return EINVAL; + } + + tzset(); + ut -= timezone; + *_unix_time = ut; + return EOK; +} |