Merge branch '4401_viewer_segfault'
[midnight-commander.git] / lib / timefmt.c
blob0866d2b7b416a8b168b416745e2948f102d87924
1 /*
2 Time formatting functions
4 Copyright (C) 1994-2022
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>
40 #include <limits.h> /* MB_LEN_MAX */
42 #include "lib/global.h"
43 #include "lib/strutil.h"
45 #include "lib/timefmt.h"
47 /*** global variables ****************************************************************************/
49 char *user_recent_timeformat = NULL; /* time format string for recent dates */
50 char *user_old_timeformat = NULL; /* time format string for older dates */
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
59 * Cache variable for the i18n_checktimelength function,
60 * initially set to a clearly invalid value to show that
61 * it hasn't been initialized yet.
63 static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
65 /*** file scope functions ************************************************************************/
67 /*** public functions ****************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
70 /**
71 * Check strftime() results. Some systems (i.e. Solaris) have different
72 * short-month and month name sizes for different locales
74 size_t
75 i18n_checktimelength (void)
77 size_t length = 0;
78 time_t testtime;
79 struct tm *lt;
81 if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
82 return i18n_timelength_cache;
84 testtime = time (NULL);
85 lt = localtime (&testtime);
87 if (lt == NULL)
89 /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
90 length = str_term_width1 (_(INVALID_TIME_TEXT));
92 else
94 char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
95 size_t tlen;
97 /* We are interested in the longest possible date */
98 lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
100 /* Loop through all months to find out the longest one */
101 for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
103 strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
104 tlen = (size_t) str_term_width1 (buf);
105 length = MAX (tlen, length);
106 strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
107 tlen = (size_t) str_term_width1 (buf);
108 length = MAX (tlen, length);
111 tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT));
112 length = MAX (tlen, length);
115 /* Don't handle big differences. Use standard value (email bug, please) */
116 if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
117 length = STD_I18NTIMELENGTH;
119 /* Save obtained value to the cache */
120 i18n_timelength_cache = length;
122 return i18n_timelength_cache;
125 /* --------------------------------------------------------------------------------------------- */
127 const char *
128 file_date (time_t when)
130 static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
131 time_t current_time = time (NULL);
132 const char *fmt;
134 if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
135 || current_time < when - 60L * 60L) /* In the future. */
136 /* The file is fairly old or in the future.
137 POSIX says the cutoff is 6 months old;
138 approximate this by 6*30 days.
139 Allow a 1 hour slop factor for what is considered "the future",
140 to allow for NFS server/client clock disagreement.
141 Show the year instead of the time of day. */
143 fmt = user_old_timeformat;
144 else
145 fmt = user_recent_timeformat;
147 FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
149 return timebuf;
152 /* --------------------------------------------------------------------------------------------- */