From 0479ee0f89ec8cad0c3fc147e043e2060f34d067 Mon Sep 17 00:00:00 2001 From: Felicity Tarnell Date: Sat, 8 Mar 2014 13:49:19 +0000 Subject: [PATCH] Configurable time format. --- functions.c | 2 +- str.c | 30 +++++++++++++++++++++++++----- str.h | 7 ++++++- tts.c | 2 ++ tts.h | 2 ++ ttsrc.sample | 9 +++++++++ ui.c | 40 ++++++++++++++++++---------------------- 7 files changed, 63 insertions(+), 29 deletions(-) diff --git a/functions.c b/functions.c index eafa4ba..61007a6 100644 --- a/functions.c +++ b/functions.c @@ -481,7 +481,7 @@ int s = 0; return; } - ct = maketime(s); + ct = maketime(s, time_format); swprintf(pr, wsizeof(pr), L"Merge %d marked entries [%ls] into current entry?", nmarked, ct); free(ct); diff --git a/str.c b/str.c index 99be83b..5813606 100644 --- a/str.c +++ b/str.c @@ -174,6 +174,9 @@ time_t i = 0, r = 0; r += i; i = 0; continue; + + case L' ': + continue; } if (wcschr(L"0123456789", *tm) == NULL) @@ -187,29 +190,46 @@ time_t i = 0, r = 0; } wchar_t * -maketime(tm) +maketime(tm, fmt) time_t tm; { wchar_t res[64] = {}; wchar_t t[16]; + if (fmt == TIME_HMS) { + int h, m, s; + time_to_hms(tm, h, m, s); + swprintf(t, wsizeof(t), L"%02d:%02d:%02d", + h, m, s); + return wcsdup(t); + } + + if (fmt == TIME_HM) { + int h, m, s; + time_to_hms(tm, h, m, s); + swprintf(t, wsizeof(t), L"%02d:%02d", h, m); + return wcsdup(t); + } + if (tm >= (60 * 60)) { - swprintf(t, wsizeof(t), L"%dh", tm / (60 * 60)); + swprintf(t, wsizeof(t), L"%2dh ", tm / (60 * 60)); wcslcat(res, t, wsizeof(res)); tm %= (60 * 60); } if (tm >= 60) { - swprintf(t, wsizeof(t), L"%dm", tm / 60); + swprintf(t, wsizeof(t), L"%2dm ", tm / 60); wcslcat(res, t, wsizeof(res)); tm %= 60; } - if (tm) { - swprintf(t, wsizeof(t), L"%ds", tm); + if (fmt == TIME_AHMS && tm) { + swprintf(t, wsizeof(t), L"%2ds ", tm); wcslcat(res, t, wsizeof(res)); } + res[wcslen(res) - 1] = '\0'; + return wcsdup(res); } diff --git a/str.h b/str.h index 47d65c2..575a72f 100644 --- a/str.h +++ b/str.h @@ -15,10 +15,15 @@ #include "wide.h" +#define TIME_HMS 0 /* HH:MM:SS */ +#define TIME_HM 1 /* HH:MM */ +#define TIME_AHMS 2 /* 1h10m37s */ +#define TIME_AHM 3 /* 1h10w */ + size_t tokenise (const wchar_t *, wchar_t ***result); void tokfree (wchar_t ***); time_t parsetime (const wchar_t *); -wchar_t *maketime (time_t); +wchar_t *maketime (time_t, int format); wchar_t *escstr (const wchar_t *); #endif /* !TTS_STR_H */ diff --git a/tts.c b/tts.c index cc36bea..1a2b9ad 100644 --- a/tts.c +++ b/tts.c @@ -92,6 +92,7 @@ int mark_advance = 1; int bill_advance = 0; int bill_increment = 0; wchar_t *auto_nonbillable; +int time_format; variable_t variables[] = { { L"delete_advance", VTYPE_BOOL, &delete_advance }, @@ -100,6 +101,7 @@ variable_t variables[] = { { L"show_billable", VTYPE_BOOL, &show_billable }, { L"auto_non_billable", VTYPE_STRING, &auto_nonbillable }, { L"bill_increment", VTYPE_INT, &bill_increment }, + { L"time_format", VTYPE_INT, &time_format }, { } }; diff --git a/tts.h b/tts.h index d3eab40..21db178 100644 --- a/tts.h +++ b/tts.h @@ -73,4 +73,6 @@ size_t wcslcat(wchar_t *s1, const wchar_t *s2, size_t n); size_t wcslcpy(wchar_t *s1, const wchar_t *s2, size_t n); #endif +extern int time_format; + #endif /* !TTS_H */ diff --git a/ttsrc.sample b/ttsrc.sample index b4a860c..da11815 100644 --- a/ttsrc.sample +++ b/ttsrc.sample @@ -11,6 +11,15 @@ #set mark_advance 1 #set delete_advance 1 +# Select the time format used in the display; value can be from 0 to 3. +# +# 0: 10:03:37 (default) +# 1: 10:03 +# 2: 10h 3m 37s +# 3: 10h 3m +# +#set time_format 0 + #### Billing options # # If set, show billable time in each daily summary. diff --git a/ui.c b/ui.c index d8a6d54..48c6ef7 100644 --- a/ui.c +++ b/ui.c @@ -119,13 +119,12 @@ wchar_t title[64]; waddwstr(titwin, title); if (itime > 0) { - wchar_t str[128]; - int h, m, s; + wchar_t str[128], *tm; time_t passed = time(NULL) - itime; - time_to_hms(passed, h, m, s); - swprintf(str, wsizeof(str), L" *** MARK INTERRUPT: %02d:%02d:%02d ***", - h, m, s); + tm = maketime(passed, time_format); + swprintf(str, wsizeof(str), L" *** MARK INTERRUPT: %ls ***", tm); + free(tm); wattron(titwin, A_BOLD); waddwstr(titwin, str); @@ -431,9 +430,9 @@ chtype oldbg; for (; en; en = TTS_TAILQ_NEXT(en, en_entries)) { time_t n; - int h, s, m; wchar_t flags[10], stime[16], *p; attr_t attrs = 0; + wchar_t *etime; if (!showinv && en->en_flags.efl_invoiced) continue; @@ -443,11 +442,11 @@ chtype oldbg; if (lastday != entry_day(en)) { struct tm *lt; wchar_t lbl[128]; - wchar_t *itime = maketime(entry_time_for_day(entry_day(en), 1, 0)), - *ntime = maketime(entry_time_for_day(entry_day(en), 0, 0)), - *btime = maketime(entry_time_for_day(entry_day(en), 2, bill_increment)), + wchar_t *itime = maketime(entry_time_for_day(entry_day(en), 1, 0), time_format), + *ntime = maketime(entry_time_for_day(entry_day(en), 0, 0), time_format), + *btime = maketime(entry_time_for_day(entry_day(en), 2, bill_increment), time_format), *ttime = maketime(entry_time_for_day(entry_day(en), 1, 0) + - entry_time_for_day(entry_day(en), 0, 0)); + entry_time_for_day(entry_day(en), 0, 0), time_format); wchar_t hdrtext[256]; int n = 0; @@ -473,33 +472,33 @@ chtype oldbg; if (*itime) { wcslcat(hdrtext, L"I:", wsizeof(hdrtext)); wcslcat(hdrtext, itime, wsizeof(hdrtext)); - free(itime); n++; } + free(itime); if (*ntime) { if (n++) wcslcat(hdrtext, L" ", wsizeof(hdrtext)); wcslcat(hdrtext, L"N:", wsizeof(hdrtext)); wcslcat(hdrtext, ntime, wsizeof(hdrtext)); - free(ntime); } + free(ntime); if (*ttime) { if (n++) wcslcat(hdrtext, L" ", wsizeof(hdrtext)); wcslcat(hdrtext, L"T:", wsizeof(hdrtext)); wcslcat(hdrtext, ttime, wsizeof(hdrtext)); - free(ttime); } + free(ttime); if (*btime) { if (n++) wcslcat(hdrtext, L" ", wsizeof(hdrtext)); wcslcat(hdrtext, L"B:", wsizeof(hdrtext)); wcslcat(hdrtext, btime, wsizeof(hdrtext)); - free(btime); } + free(btime); wcslcat(hdrtext, L"]", wsizeof(hdrtext)); @@ -558,14 +557,11 @@ chtype oldbg; n = en->en_secs; if (en->en_started) n += time(NULL) - en->en_started; - h = n / (60 * 60); - n %= (60 * 60); - m = n / 60; - n %= 60; - s = n; - swprintf(stime, wsizeof(stime), L"%02d:%02d:%02d%c ", - h, m, s, (itime && (en == running)) ? '*' : ' '); + etime = maketime(n, time_format); + swprintf(stime, wsizeof(stime), L"%8ls%c ", + *etime ? etime : L"0m", (itime && (en == running)) ? '*' : ' '); + free(etime); waddwstr(listwin, stime); memset(flags, 0, sizeof(flags)); @@ -627,7 +623,7 @@ wchar_t *defstr = NULL; wchar_t *tstr; time_t ret; - defstr = maketime(def); + defstr = maketime(def, TIME_AHMS); if ((tstr = prompt(pr, defstr, NULL)) == NULL) { free(defstr);