Unify widget and dialog message handling.
[midnight-commander.git] / lib / timefmt.c
blob7a270191afb6551c87fb886ec84204b57a70a45b
1 /*
2 Time formatting functions
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1994, 1995, 1996
10 Janne Kukonlehto, 1994, 1995, 1996
11 Dugan Porter, 1994, 1995, 1996
12 Jakub Jelinek, 1994, 1995, 1996
13 Mauricio Plaza, 1994, 1995, 1996
15 The file_date routine is mostly from GNU's fileutils package,
16 written by Richard Stallman and David MacKenzie.
18 This file is part of the Midnight Commander.
20 The Midnight Commander is free software: you can redistribute it
21 and/or modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation, either version 3 of the License,
23 or (at your option) any later version.
25 The Midnight Commander is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 /** \file
35 * \brief Source: time formatting functions
38 #include <config.h>
40 #include <stdlib.h>
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 const time_t testtime = time (NULL);
79 struct tm *lt = localtime (&testtime);
81 if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
82 return i18n_timelength_cache;
84 if (lt == NULL)
86 /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
87 length = str_term_width1 (_(INVALID_TIME_TEXT));
89 else
91 char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
93 /* We are interested in the longest possible date */
94 lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
96 /* Loop through all months to find out the longest one */
97 for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
99 strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
100 length = max ((size_t) str_term_width1 (buf), length);
101 strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
102 length = max ((size_t) str_term_width1 (buf), length);
105 length = max ((size_t) str_term_width1 (_(INVALID_TIME_TEXT)), length);
108 /* Don't handle big differences. Use standard value (email bug, please) */
109 if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
110 length = STD_I18NTIMELENGTH;
112 /* Save obtained value to the cache */
113 i18n_timelength_cache = length;
115 return i18n_timelength_cache;
118 /* --------------------------------------------------------------------------------------------- */
120 const char *
121 file_date (time_t when)
123 static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
124 time_t current_time = time ((time_t) 0);
125 const char *fmt;
127 if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
128 || current_time < when - 60L * 60L) /* In the future. */
129 /* The file is fairly old or in the future.
130 POSIX says the cutoff is 6 months old;
131 approximate this by 6*30 days.
132 Allow a 1 hour slop factor for what is considered "the future",
133 to allow for NFS server/client clock disagreement.
134 Show the year instead of the time of day. */
136 fmt = user_old_timeformat;
137 else
138 fmt = user_recent_timeformat;
140 FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
142 return timebuf;
145 /* --------------------------------------------------------------------------------------------- */