diff --git a/edit/edit.c b/edit/edit.c index 4bce1196c..8f2bca76e 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -44,6 +44,7 @@ #include "../src/cmd.h" /* view_other_cmd() */ #include "../src/user.h" /* user_menu_cmd() */ #include "../src/wtools.h" /* query_dialog() */ +#include "../src/timefmt.h" /* time formatting */ /* @@ -2517,20 +2518,13 @@ edit_execute_cmd (WEdit *edit, int command, int char_for_insertion) break; case CK_Date:{ - time_t t; -#ifdef HAVE_STRFTIME char s[1024]; /* fool gcc to prevent a Y2K warning */ char time_format[] = "_c"; time_format[0] = '%'; -#endif - time (&t); -#ifdef HAVE_STRFTIME - strftime (s, sizeof (s), time_format, localtime (&t)); + + FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format); edit_print_string (edit, s); -#else - edit_print_string (edit, ctime (&t)); -#endif edit->force |= REDRAW_PAGE; break; } diff --git a/src/Makefile.am b/src/Makefile.am index ccf0f8bba..a4713a625 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -60,8 +60,8 @@ SRCS = achown.c achown.h background.c background.h boxes.c boxes.h \ popt.c poptconfig.c popt.h popthelp.c poptint.h poptparse.c \ profile.c profile.h regex.c rxvt.c screen.c setup.c setup.h \ slint.c subshell.c subshell.h textconf.c textconf.h \ - tree.c tree.h treestore.c treestore.h tty.c tty.h user.c user.h \ - util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c \ + tree.c tree.h treestore.c treestore.h timefmt.h tty.c tty.h user.c \ + user.h util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c \ widget.h win.c win.h wtools.c wtools.h unixcompat.h \ x11conn.h x11conn.c ecs.h ecs.c diff --git a/src/timefmt.h b/src/timefmt.h new file mode 100644 index 000000000..2b8d52fd8 --- /dev/null +++ b/src/timefmt.h @@ -0,0 +1,43 @@ +#ifndef __UTIL_TIMEFMT_H +#define __UTIL_TIMEFMT_H + +#include + +#define INVALID_TIME_TEXT "(invalid)" + +#ifdef HAVE_STRFTIME + +/* safe localtime formatting - strftime()-using version */ +#define FMT_LOCALTIME(buffer, bufsize, fmt, when) \ + { \ + struct tm *whentm; \ + whentm = localtime(&when); \ + if (whentm == NULL) \ + { \ + strncpy(buffer, INVALID_TIME_TEXT, bufsize); \ + buffer[bufsize-1] = 0; \ + } \ + else \ + { \ + strftime(buffer, bufsize, fmt, whentm); \ + } \ + } \ + +#else + +/* fallback when strftime/localtime not available */ +#define FMT_LOCALTIME(buffer,bufsize,fmt,when) \ + { \ + ctime_r(when,buffer); \ + } \ + +#endif + +#define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt) \ + { \ + time_t __current_time; \ + time(&__current_time); \ + FMT_LOCALTIME(buffer,bufsize,fmt,__current_time); \ + } + +#endif /* !__UTIL_H */ diff --git a/src/util.c b/src/util.c index da6d1b2b8..2e4eabbca 100644 --- a/src/util.c +++ b/src/util.c @@ -41,6 +41,7 @@ #include "cmd.h" /* guess_message_value */ #include "mountlist.h" #include "win.h" /* xterm_flag */ +#include "timefmt.h" #ifdef HAVE_CHARSET #include "charsets.h" @@ -724,19 +725,24 @@ short-month-name sizes for different locales */ size_t i18n_checktimelength (void) { - size_t length, a, b; - char buf [MAX_I18NTIMELENGTH + 1]; time_t testtime = time (NULL); - - a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), localtime(&testtime)); - b = strftime (buf, sizeof(buf)-1, _("%b %e %Y"), localtime(&testtime)); - - length = max (a, b); - length = max (strlen (_("(invalid)")), length); - + struct tm* lt = localtime(&testtime); + size_t length; + + if (lt == NULL) { + // huh, localtime() doesnt seem to work ... falling back to "(invalid)" + length = strlen(INVALID_TIME_TEXT); + } else { + char buf [MAX_I18NTIMELENGTH + 1]; + size_t a, b; + a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), lt); + b = strftime (buf, sizeof(buf)-1, _("%b %e %Y"), lt); + length = max (a, b); + } + /* Don't handle big differences. Use standard value (email bug, please) */ if ( length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH ) - length = STD_I18NTIMELENGTH; + length = STD_I18NTIMELENGTH; return length; } @@ -773,11 +779,8 @@ file_date (time_t when) else fmt = fmttime; - whentm = localtime(&when); - if (whentm == NULL) - g_snprintf (timebuf, i18n_timelength, "%s", _("(invalid)")); - else - strftime (timebuf, i18n_timelength, fmt, whentm); + FMT_LOCALTIME(timebuf, i18n_timelength, fmt, when); + return timebuf; }