Merge branch '2170_panel_header_color' into 4.7.5-stable
[midnight-commander.git] / lib / timefmt.c
blobbb4b57ef7a2a66a87ad137f17fe822247daca3dd
1 /* Time formatting functions
2 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
4 Written 1994, 1995, 1996 by:
5 Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
6 Jakub Jelinek, Mauricio Plaza.
8 The file_date routine is mostly from GNU's fileutils package,
9 written by Richard Stallman and David MacKenzie.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /** \file
26 * \brief Source: time formatting functions
29 #include <config.h>
31 #include <stdlib.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
36 #include "lib/timefmt.h"
38 /*** global variables ****************************************************************************/
40 char *user_recent_timeformat = NULL; /* time format string for recent dates */
41 char *user_old_timeformat = NULL; /* time format string for older dates */
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
50 * Cache variable for the i18n_checktimelength function,
51 * initially set to a clearly invalid value to show that
52 * it hasn't been initialized yet.
54 static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
56 /*** file scope functions ************************************************************************/
58 /*** public functions ****************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
61 /**
62 * Check strftime() results. Some systems (i.e. Solaris) have different
63 * short-month and month name sizes for different locales
65 size_t
66 i18n_checktimelength (void)
68 size_t length = 0;
69 const time_t testtime = time (NULL);
70 struct tm *lt = localtime (&testtime);
72 if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
73 return i18n_timelength_cache;
75 if (lt == NULL)
77 /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
78 length = str_term_width1 (_(INVALID_TIME_TEXT));
80 else
82 char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
84 /* We are interested in the longest possible date */
85 lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
87 /* Loop through all months to find out the longest one */
88 for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
90 strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
91 length = max ((size_t) str_term_width1 (buf), length);
92 strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
93 length = max ((size_t) str_term_width1 (buf), length);
96 length = max ((size_t) str_term_width1 (_(INVALID_TIME_TEXT)), length);
99 /* Don't handle big differences. Use standard value (email bug, please) */
100 if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
101 length = STD_I18NTIMELENGTH;
103 /* Save obtained value to the cache */
104 i18n_timelength_cache = length;
106 return i18n_timelength_cache;
109 /* --------------------------------------------------------------------------------------------- */
111 const char *
112 file_date (time_t when)
114 static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
115 time_t current_time = time ((time_t) 0);
116 const char *fmt;
118 if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
119 || current_time < when - 60L * 60L) /* In the future. */
120 /* The file is fairly old or in the future.
121 POSIX says the cutoff is 6 months old;
122 approximate this by 6*30 days.
123 Allow a 1 hour slop factor for what is considered "the future",
124 to allow for NFS server/client clock disagreement.
125 Show the year instead of the time of day. */
127 fmt = user_old_timeformat;
128 else
129 fmt = user_recent_timeformat;
131 FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
133 return timebuf;
136 /* --------------------------------------------------------------------------------------------- */