### Question 13.13

I know that the library routine `localtime` will convert a `time_t` into a broken-down `struct tm`, and that `ctime` will convert a `time_t` to a printable string. How can I perform the inverse operations of converting a `struct tm` or a string into a `time_t`?

ANSI C specifies a library routine, `mktime`, which converts a `struct tm` to a `time_t`.

Converting a string to a `time_t` is harder, because of the wide variety of date and time formats which might be encountered. Some systems provide a `strptime` function, which is basically the inverse of `strftime`. Other popular routines are `partime` (widely distributed with the RCS package) and `getdate` (and a few others, from the C news distribution). See question 18.16.

References: K&R2 Sec. B10 p. 256
ANSI Sec. 4.12.2.3
ISO Sec. 7.12.2.3
H&S Sec. 18.4 pp. 401-2

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback