# HG changeset patch # User Matti Hamalainen # Date 1288779970 -7200 # Node ID 0a9fe14882dcebb480842d5ccb6b4a7a836e126e # Parent 0720ca51673e27e65c63ec5d5f293ef78b376f93 Introduce utility function getTimeStamp() and use it to lessen code duplication. diff -r 0720ca51673e -r 0a9fe14882dc nnchat.c --- a/nnchat.c Wed Nov 03 01:33:39 2010 +0200 +++ b/nnchat.c Wed Nov 03 12:26:10 2010 +0200 @@ -149,19 +149,26 @@ return TRUE; } +BOOL getTimeStamp(char *str, size_t len, const char *fmt) +{ + time_t stamp = time(NULL); + struct tm *stamp_tm; + if ((stamp_tm = localtime(&stamp)) != NULL) { + strftime(str, len, fmt, stamp_tm); + return TRUE; + } else { + str[0] = 0; + return FALSE; + } +} void updateStatus(BOOL insertMode) { - char tmpStr[128] = ""; - time_t timeStamp; - struct tm *tmpTime;; + char tmpStr[128]; if (statusWin == NULL) return; - timeStamp = time(NULL); - if ((tmpTime = localtime(&timeStamp)) != NULL) { - strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime); - } + getTimeStamp(tmpStr, sizeof(tmpStr), "%H:%M:%S"); wbkgdset(statusWin, COLOR_PAIR(10)); werase(statusWin); @@ -285,14 +292,9 @@ void printMsgV(BOOL addStamp, BOOL logOnly, const char *fmt, va_list ap) { - char tmpStr[128] = "", buf[8192]; - time_t timeStamp; - struct tm *tmpTime;; + char tmpStr[128], buf[8192]; - timeStamp = time(NULL); - if ((tmpTime = localtime(&timeStamp)) != NULL) { - strftime(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ ", tmpTime); - } + getTimeStamp(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ "); vsnprintf(buf, sizeof(buf), fmt, ap); @@ -421,14 +423,9 @@ int handleLogin(int sock, const char *str) { - char tmpStr[256] = ""; - time_t timeStamp; - struct tm *tmpTime;; + char tmpStr[256]; - timeStamp = time(NULL); - if ((tmpTime = localtime(&timeStamp)) != NULL) { - strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime); - } + getTimeStamp(tmpStr, sizeof(tmpStr), "%c"); if (!strncmp(str, "FAILURE", 7)) { printMsg("˝1˝Login failure˝0˝ - ˝3˝%s˝0˝\n", tmpStr);