[PATCH] some time formatting fixes
- From: Enrico Weigelt <weigelt metux de>
- To: midnight commander devel <mc-devel gnome org>
- Subject: [PATCH] some time formatting fixes
- Date: Tue, 30 Dec 2008 02:03:53 +0100
Hi folks,
this patch goes a bit deeper into the strftime()+localtime() issue.
a) introduce some new shortcut macros and use them in some places
b) adds additional checks (where the macros dont fit)
cu
--
----------------------------------------------------------------------
Enrico Weigelt, metux IT service -- http://www.metux.de/
cellphone: +49 174 7066481 email: info metux de skype: nekrad666
----------------------------------------------------------------------
Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------
#
# Adds an safe(r) strftime()/localtime() handling, as replacement
# for the "mc-4.6.1-invalid-mtime.diff" patch
#
# Source: metux
# Reference: 4.6.1
# Submit-By: Enrico Weigelt, metux IT service <weigelt metux de>
# Submit-Date: 2008-12-30
# Obsoletes: mc-4.6.1-invalid-mtime.diff
#
diff -ruN mc-4.6.1.orig/edit/edit.c mc-4.6.1/edit/edit.c
--- mc-4.6.1.orig/edit/edit.c 2008-12-30 01:52:56.000000000 +0100
+++ mc-4.6.1/edit/edit.c 2008-12-30 01:53:41.000000000 +0100
@@ -29,6 +29,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 */
/*
what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL
@@ -2512,20 +2513,13 @@
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 -ruN mc-4.6.1.orig/src/Makefile.am mc-4.6.1/src/Makefile.am
--- mc-4.6.1.orig/src/Makefile.am 2008-12-30 01:52:56.000000000 +0100
+++ mc-4.6.1/src/Makefile.am 2008-12-30 01:53:41.000000000 +0100
@@ -57,9 +57,9 @@
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.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 \
+ tree.c tree.h treestore.c treestore.h timefmt.h 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 \
x11conn.h x11conn.c
if CHARSET
diff -ruN mc-4.6.1.orig/src/timefmt.h mc-4.6.1/src/timefmt.h
--- mc-4.6.1.orig/src/timefmt.h 1970-01-01 01:00:00.000000000 +0100
+++ mc-4.6.1/src/timefmt.h 2008-12-30 01:53:41.000000000 +0100
@@ -0,0 +1,43 @@
+#ifndef __UTIL_TIMEFMT_H
+#define __UTIL_TIMEFMT_H
+
+#include <sys/types.h>
+
+#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 -ruN mc-4.6.1.orig/src/util.c mc-4.6.1/src/util.c
--- mc-4.6.1.orig/src/util.c 2008-12-30 01:52:56.000000000 +0100
+++ mc-4.6.1/src/util.c 2008-12-30 01:53:41.000000000 +0100
@@ -39,6 +39,7 @@
#include "cmd.h" /* guess_message_value */
#include "mountlist.h"
#include "win.h" /* xterm_flag */
+#include "timefmt.h"
#ifdef HAVE_CHARSET
#include "charsets.h"
@@ -694,19 +695,28 @@
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);
-
+ 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;
-
+
return length;
}
@@ -740,7 +750,7 @@
else
fmt = fmttime;
- strftime (timebuf, i18n_timelength, fmt, localtime(&when));
+ FMT_LOCALTIME(timebuf, i18n_timelength, fmt, when);
return timebuf;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]