Ticket #3716: various checkboxes not responding.
[midnight-commander.git] / lib / timefmt.c
blob50a248ba6106645ddb5937f32e9c4d8207f01c16
1 /*
2 Time formatting functions
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1994, 1995, 1996
9 Janne Kukonlehto, 1994, 1995, 1996
10 Dugan Porter, 1994, 1995, 1996
11 Jakub Jelinek, 1994, 1995, 1996
12 Mauricio Plaza, 1994, 1995, 1996
14 The file_date routine is mostly from GNU's fileutils package,
15 written by Richard Stallman and David MacKenzie.
17 This file is part of the Midnight Commander.
19 The Midnight Commander is free software: you can redistribute it
20 and/or modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation, either version 3 of the License,
22 or (at your option) any later version.
24 The Midnight Commander is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 /** \file
34 * \brief Source: time formatting functions
37 #include <config.h>
39 #include <stdlib.h>
41 #include "lib/global.h"
42 #include "lib/strutil.h"
44 #include "lib/timefmt.h"
46 /*** global variables ****************************************************************************/
48 char *user_recent_timeformat = NULL; /* time format string for recent dates */
49 char *user_old_timeformat = NULL; /* time format string for older dates */
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
58 * Cache variable for the i18n_checktimelength function,
59 * initially set to a clearly invalid value to show that
60 * it hasn't been initialized yet.
62 static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
64 /*** file scope functions ************************************************************************/
66 /*** public functions ****************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
69 /**
70 * Check strftime() results. Some systems (i.e. Solaris) have different
71 * short-month and month name sizes for different locales
73 size_t
74 i18n_checktimelength (void)
76 size_t length = 0;
77 time_t testtime;
78 struct tm *lt;
80 if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
81 return i18n_timelength_cache;
83 testtime = time (NULL);
84 lt = localtime (&testtime);
86 if (lt == NULL)
88 /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
89 length = str_term_width1 (_(INVALID_TIME_TEXT));
91 else
93 char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
94 size_t tlen;
96 /* We are interested in the longest possible date */
97 lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
99 /* Loop through all months to find out the longest one */
100 for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
102 strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
103 tlen = (size_t) str_term_width1 (buf);
104 length = MAX (tlen, length);
105 strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
106 tlen = (size_t) str_term_width1 (buf);
107 length = MAX (tlen, length);
110 tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT));
111 length = MAX (tlen, length);
114 /* Don't handle big differences. Use standard value (email bug, please) */
115 if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
116 length = STD_I18NTIMELENGTH;
118 /* Save obtained value to the cache */
119 i18n_timelength_cache = length;
121 return i18n_timelength_cache;
124 /* --------------------------------------------------------------------------------------------- */
126 const char *
127 file_date (time_t when)
129 static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
130 time_t current_time = time (NULL);
131 const char *fmt;
133 if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
134 || current_time < when - 60L * 60L) /* In the future. */
135 /* The file is fairly old or in the future.
136 POSIX says the cutoff is 6 months old;
137 approximate this by 6*30 days.
138 Allow a 1 hour slop factor for what is considered "the future",
139 to allow for NFS server/client clock disagreement.
140 Show the year instead of the time of day. */
142 fmt = user_old_timeformat;
143 else
144 fmt = user_recent_timeformat;
146 FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
148 return timebuf;
151 /* --------------------------------------------------------------------------------------------- */