mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-16 07:47:00 +00:00
other: use strftime() to generate date-time string
It is better to use library function to create date-time string than sprintf various struct tm members. In same go retire time() call that is obsoleted by clock_gettime(). Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
4befdeeb47
commit
48e46e8698
1 changed files with 9 additions and 17 deletions
24
src/other.c
24
src/other.c
|
|
@ -551,31 +551,23 @@ void clean_up(struct conf_t *state)
|
|||
}
|
||||
|
||||
/*! \brief Print a time stamp of a path or now to output file. */
|
||||
void dp_time_tool(FILE *file, const char *path, int epoch)
|
||||
void dp_time_tool(FILE *file, const char *path, const int epoch)
|
||||
{
|
||||
time_t t;
|
||||
|
||||
/* a file or now */
|
||||
if (path) {
|
||||
struct stat st;
|
||||
|
||||
if (path)
|
||||
stat(path, &st);
|
||||
t = st.st_mtime;
|
||||
} else
|
||||
t = time(NULL);
|
||||
/* epoc or iso time stamp */
|
||||
else
|
||||
clock_gettime(CLOCK_REALTIME, &st.st_mtim);
|
||||
if (epoch)
|
||||
fprintf(file, "%ld", t);
|
||||
fprintf(file, "%ld", st.st_mtim.tv_sec);
|
||||
else {
|
||||
char time_stamp[64];
|
||||
const time_t mtime = st.st_mtim.tv_sec;
|
||||
struct tm tm;
|
||||
int len;
|
||||
|
||||
localtime_r(&t, &tm);
|
||||
len = snprintf(time_stamp, sizeof(time_stamp), "%4d-%.2d-%.2dT%02d:%02d:%02d",
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
strftime(time_stamp + len, sizeof(time_stamp) - len, "%z", &tm);
|
||||
localtime_r(&mtime, &tm);
|
||||
strftime(time_stamp, sizeof(time_stamp), "%FT%T%z", &tm);
|
||||
fprintf(file, "%s", time_stamp);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue