hysteria

ii wrapper script
git clone git://git.2f30.org/hysteria.git
Log | Files | Refs | README | LICENSE

commit 802a19eb46241f4bde95ff3b37577fc9c236c176
parent 1832a6e75dd6f1262ca164049f27c442e9f82f0d
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Wed Oct 29 22:39:56 +0000

convert unix timestamp prefix to localtime

Diffstat:
hysteria-highlight.c | 35+++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)
diff --git a/hysteria-highlight.c b/hysteria-highlight.c @@ -9,15 +9,16 @@ * - align nicknames. * - ignore words. * - don't print date-prefix (YYYY-mm-dd). + * - convert unix timestamp prefix to localtime. */ +#include <errno.h> +#include <regex.h> #include <stdio.h> -#include <string.h> #include <stdlib.h> -#include <errno.h> - +#include <string.h> #include <sys/types.h> -#include <regex.h> +#include <time.h> /* escape codes: colors, reset */ static const char *green = "\x1b[1;32m"; @@ -36,6 +37,10 @@ static const char *regexnickalign = "^[0-9]{2}:[0-9]{2} <([^>]+)>"; static const char *regexserver = "^([0-9]{2}:[0-9]{2} -!- .*)$"; /* skip this prefix for each line (date part) */ static const char *regextimestamp = "^[0-9]{4}-[0-9]{2}-[0-9]{2} "; +/* convert unix timestamp prefix */ +static const char *regexunixtimestamp = "^[0-9]{10} "; +static const char *timeformat = "%Y-%m-%d %H:%M "; +static char timebuf[18] = ""; /* ignore message on match */ static const char *regexignore = "^$"; /* highlight / color urls */ @@ -43,6 +48,7 @@ static const char *regexurl = "(((https?|ftp)|mailto):(//)?[^ <>\"[:blank:]]*|(www|ftp)[0-9]?\\.[-a-z0-9.]+)"; static regex_t nick, nickalign, server, timestamp, ignore, url, word; +static regex_t unixtimestamp; static void eprintf(const char *s) @@ -72,6 +78,8 @@ main(void) { regmatch_t match, matches[3]; regoff_t len; const char *p; + struct tm tm; + time_t t; int hasmatch, ret; /* compile regex */ @@ -83,6 +91,8 @@ main(void) { eprintf("invalid regex: server"); if(regcomp(&timestamp, regextimestamp, REG_EXTENDED)) eprintf("invalid regex: timestamp"); + if(regcomp(&unixtimestamp, regexunixtimestamp, REG_EXTENDED)) + eprintf("invalid regex: unix timestamp"); if(regcomp(&ignore, regexignore, REG_EXTENDED | REG_ICASE)) eprintf("invalid regex: ignore"); if(regcomp(&url, regexurl, REG_EXTENDED | REG_ICASE)) @@ -93,9 +103,21 @@ main(void) { while(fgets(buffer, sizeof(buffer), stdin)) { p = buffer; - /* skip date part (YYY-mm-dd) of timestamp */ - if((ret = regexec(&timestamp, p, 1, &match, 0)) != REG_NOMATCH) + /* convert unix timestamp to localtime */ + if((ret = regexec(&unixtimestamp, p, 1, &match, 0)) != REG_NOMATCH) { + errno = 0; + t = (time_t)strtol(p, NULL, 10); + if(errno != 0) + t = 0; + memset(&tm, 0, sizeof(tm)); + if(localtime_r(&t, &tm) != NULL && + (strftime(timebuf, sizeof(timebuf), timeformat, &tm) > 0)) + fputs(timebuf, stdout); p += match.rm_eo - match.rm_so; + } else if((ret = regexec(&timestamp, p, 1, &match, 0)) != REG_NOMATCH) { + /* skip date part (YYY-mm-dd) of timestamp */ + p += match.rm_eo - match.rm_so; + } /* highlight / color server messages */ if((ret = regexec(&server, p, 1, &match, 0)) != REG_NOMATCH) { @@ -167,6 +189,7 @@ main(void) { regfree(&nickalign); regfree(&server); regfree(&timestamp); + regfree(&unixtimestamp); regfree(&ignore); regfree(&url);