The Linux Programming Interface

(nextflipdebug5) #1
Time 195

char
currTime(const char
format)
{
static char buf[BUF_SIZE]; / Nonreentrant /
time_t t;
size_t s;
struct tm *tm;


t = time(NULL);
tm = localtime(&t);
if (tm == NULL)
return NULL;


s = strftime(buf, BUF_SIZE, (format != NULL)? format : "%c", tm);


return (s == 0)? NULL : buf;
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––– time/curr_time.c


Converting from printable form to broken-down time


The strptime() function is the converse of strftime(). It converts a date-plus-time
string to a broken-down time.


The strptime() function uses the specification given in format to parse the date-plus-
time string given in str, and places the converted broken-down time in the structure
pointed to by timeptr.
On success, strptime() returns a pointer to the next unprocessed character in str.
(This is useful if the string contains further information to be processed by the call-
ing program.) If the complete format string could not be matched, strptime()
returns NULL to indicate the error.
The format specification given to strptime() is akin to that given to scanf(3). It
contains the following types of characters:


z conversion specifications beginning with a percent character (%);


z white-space characters, which match zero or more white spaces in the input
string; and


z non-white-space characters (other than %), which must match exactly the same
characters in the input string.


#define _XOPEN_SOURCE
#include <time.h>

char *strptime(const char *str, const char *format, struct tm *timeptr);
Returns pointer to next unprocessed character in
str on success, or NULL on error
Free download pdf