1 /* 2 Time formatting functions 3 4 Copyright (C) 1994-2024 5 Free Software Foundation, Inc. 6 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 13 14 The file_date routine is mostly from GNU's fileutils package, 15 written by Richard Stallman and David MacKenzie. 16 17 This file is part of the Midnight Commander. 18 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. 23 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. 28 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/>. 31 */ 32 33 /** \file 34 * \brief Source: time formatting functions 35 */ 36 37 #include <config.h> 38 39 #include <stdlib.h> 40 #include <limits.h> /* MB_LEN_MAX */ 41 42 #include "lib/global.h" 43 #include "lib/strutil.h" 44 45 #include "lib/timefmt.h" 46 47 /*** global variables ****************************************************************************/ 48 49 char *user_recent_timeformat = NULL; /* time format string for recent dates */ 50 char *user_old_timeformat = NULL; /* time format string for older dates */ 51 52 /*** file scope macro definitions ****************************************************************/ 53 54 /*** file scope type declarations ****************************************************************/ 55 56 /*** forward declarations (file scope functions) *************************************************/ 57 58 /*** file scope variables ************************************************************************/ 59 60 /* 61 * Cache variable for the i18n_checktimelength function, 62 * initially set to a clearly invalid value to show that 63 * it hasn't been initialized yet. 64 */ 65 static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1; 66 67 /* --------------------------------------------------------------------------------------------- */ 68 /*** file scope functions ************************************************************************/ 69 /* --------------------------------------------------------------------------------------------- */ 70 71 /* --------------------------------------------------------------------------------------------- */ 72 /*** public functions ****************************************************************************/ 73 /* --------------------------------------------------------------------------------------------- */ 74 /** 75 * Check strftime() results. Some systems (i.e. Solaris) have different 76 * short-month and month name sizes for different locales 77 */ 78 size_t 79 i18n_checktimelength (void) /* */ 80 { 81 size_t length = 0; 82 time_t testtime; 83 struct tm *lt; 84 85 if (i18n_timelength_cache <= MAX_I18NTIMELENGTH) 86 return i18n_timelength_cache; 87 88 testtime = time (NULL); 89 lt = localtime (&testtime); 90 91 if (lt == NULL) 92 { 93 /* huh, localtime() doesn't seem to work ... falling back to "(invalid)" */ 94 length = str_term_width1 (_(INVALID_TIME_TEXT)); 95 } 96 else 97 { 98 char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1]; 99 size_t tlen; 100 101 /* We are interested in the longest possible date */ 102 lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10; 103 104 /* Loop through all months to find out the longest one */ 105 for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++) 106 { 107 strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt); 108 tlen = (size_t) str_term_width1 (buf); 109 length = MAX (tlen, length); 110 strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt); 111 tlen = (size_t) str_term_width1 (buf); 112 length = MAX (tlen, length); 113 } 114 115 tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT)); 116 length = MAX (tlen, length); 117 } 118 119 /* Don't handle big differences. Use standard value (email bug, please) */ 120 if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH) 121 length = STD_I18NTIMELENGTH; 122 123 /* Save obtained value to the cache */ 124 i18n_timelength_cache = length; 125 126 return i18n_timelength_cache; 127 } 128 129 /* --------------------------------------------------------------------------------------------- */ 130 131 const char * 132 file_date (time_t when) /* */ 133 { 134 static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1]; 135 time_t current_time = time (NULL); 136 const char *fmt; 137 138 if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */ 139 || current_time < when - 60L * 60L) /* In the future. */ 140 /* The file is fairly old or in the future. 141 POSIX says the cutoff is 6 months old; 142 approximate this by 6*30 days. 143 Allow a 1 hour slop factor for what is considered "the future", 144 to allow for NFS server/client clock disagreement. 145 Show the year instead of the time of day. */ 146 147 fmt = user_old_timeformat; 148 else 149 fmt = user_recent_timeformat; 150 151 FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when); 152 153 return timebuf; 154 } 155 156 /* --------------------------------------------------------------------------------------------- */