From ce76896eb3a48419cc37a51bee844e5c4c2b8e73 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 12 Mar 2014 03:08:13 -0400 Subject: [PATCH] easier way to format timestamps --- src/log.c | 14 ++++++-------- src/misc_tools.c | 5 +++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/log.c b/src/log.c index de1a244..6d93aa6 100644 --- a/src/log.c +++ b/src/log.c @@ -51,10 +51,9 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) sprintf(&ident[2], "%02X", key[2] & 0xff); ident[KEY_IDENT_DIGITS*2+1] = '\0'; } else { - struct tm *tminfo = get_time(); - snprintf(ident, sizeof(ident), - "%04d-%02d-%02d[%d:%02d:%02d]", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, - tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); + uint8_t s[MAX_STR_SIZE]; + strftime(s, MAX_STR_SIZE, "%Y-%m-%d[%H:%M:%S]", get_time()); + snprintf(ident, sizeof(ident), "%s", s); path_len += strlen(ident) + 1; } @@ -96,10 +95,9 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) else snprintf(name_frmt, sizeof(name_frmt), "%s:", name); - struct tm *tminfo = get_time(); - fprintf(log->file,"%04d/%02d/%02d [%d:%02d:%02d] %s %s\n", tminfo->tm_year+1900, - tminfo->tm_mon+1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min, - tminfo->tm_sec, name_frmt, msg); + uint8_t s[MAX_STR_SIZE]; + strftime(s, MAX_STR_SIZE, "%Y/%m/%d [%H:%M:%S]", get_time()); + fprintf(log->file,"%s %s %s\n", s, name_frmt, msg); uint64_t curtime = (uint64_t) time(NULL); diff --git a/src/misc_tools.c b/src/misc_tools.c index 0128ac4..3256e8c 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -67,10 +67,11 @@ struct tm *get_time(void) /* Prints the time to given window */ void print_time(WINDOW *window) { - struct tm *timeinfo = get_time(); + uint8_t s[MAX_STR_SIZE]; + strftime(s, MAX_STR_SIZE, "[%H:%M] ", get_time()); wattron(window, COLOR_PAIR(BLUE)); - wprintw(window, "[%d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + wprintw(window, "%s", s); wattroff(window,COLOR_PAIR(BLUE)); }