2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Time formatting functions
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 1994-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2011-10-15 14:56:47 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Miguel de Icaza, 1994, 1995, 1996
|
|
|
|
Janne Kukonlehto, 1994, 1995, 1996
|
|
|
|
Dugan Porter, 1994, 1995, 1996
|
|
|
|
Jakub Jelinek, 1994, 1995, 1996
|
|
|
|
Mauricio Plaza, 1994, 1995, 1996
|
2010-11-09 21:50:25 +03:00
|
|
|
|
|
|
|
The file_date routine is mostly from GNU's fileutils package,
|
|
|
|
written by Richard Stallman and David MacKenzie.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2010-11-09 21:50:25 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2010-11-09 21:50:25 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2010-11-09 21:50:25 +03:00
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \brief Source: time formatting functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-04-16 07:56:22 +03:00
|
|
|
#include <limits.h> /* MB_LEN_MAX */
|
2010-11-09 21:50:25 +03:00
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
|
|
|
|
#include "lib/timefmt.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
char *user_recent_timeformat = NULL; /* time format string for recent dates */
|
|
|
|
char *user_old_timeformat = NULL; /* time format string for older dates */
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-09 21:50:25 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache variable for the i18n_checktimelength function,
|
|
|
|
* initially set to a clearly invalid value to show that
|
|
|
|
* it hasn't been initialized yet.
|
|
|
|
*/
|
|
|
|
static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-09 21:50:25 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-09 21:50:25 +03:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-09 21:50:25 +03:00
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Check strftime() results. Some systems (i.e. Solaris) have different
|
|
|
|
* short-month and month name sizes for different locales
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
i18n_checktimelength (void)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
2015-06-25 16:50:21 +03:00
|
|
|
time_t testtime;
|
|
|
|
struct tm *lt;
|
2010-11-09 21:50:25 +03:00
|
|
|
|
|
|
|
if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
|
|
|
|
return i18n_timelength_cache;
|
|
|
|
|
2015-06-25 16:50:21 +03:00
|
|
|
testtime = time (NULL);
|
|
|
|
lt = localtime (&testtime);
|
|
|
|
|
2010-11-09 21:50:25 +03:00
|
|
|
if (lt == NULL)
|
|
|
|
{
|
Fix various typos in the source code (closes MidnightCommander/mc#177).
Found via `codespell -S
po,doc,./misc/syntax,./src/vfs/extfs/helpers/README.it -L
parm,rouge,sav,ect,vie,te,dum,clen,wee,dynamc,childs,ths,fo,nin,unx,nd,iif,iterm,ser,makrs,wil`
Co-authored-by: Yury V. Zaytsev <yury@shurup.com>
Signed-off-by: Kian-Meng Ang <kianmeng@cpan.org>
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
2023-01-10 06:02:52 +03:00
|
|
|
/* huh, localtime() doesn't seem to work ... falling back to "(invalid)" */
|
2010-11-09 21:50:25 +03:00
|
|
|
length = str_term_width1 (_(INVALID_TIME_TEXT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
|
2015-06-25 16:50:21 +03:00
|
|
|
size_t tlen;
|
2010-11-09 21:50:25 +03:00
|
|
|
|
|
|
|
/* We are interested in the longest possible date */
|
|
|
|
lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
|
|
|
|
|
|
|
|
/* Loop through all months to find out the longest one */
|
|
|
|
for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
|
|
|
|
{
|
|
|
|
strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
|
2015-06-25 16:50:21 +03:00
|
|
|
tlen = (size_t) str_term_width1 (buf);
|
2016-04-07 10:52:04 +03:00
|
|
|
length = MAX (tlen, length);
|
2010-11-09 21:50:25 +03:00
|
|
|
strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
|
2015-06-25 16:50:21 +03:00
|
|
|
tlen = (size_t) str_term_width1 (buf);
|
2016-04-07 10:52:04 +03:00
|
|
|
length = MAX (tlen, length);
|
2010-11-09 21:50:25 +03:00
|
|
|
}
|
|
|
|
|
2015-06-25 16:50:21 +03:00
|
|
|
tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT));
|
2016-04-07 10:52:04 +03:00
|
|
|
length = MAX (tlen, length);
|
2010-11-09 21:50:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't handle big differences. Use standard value (email bug, please) */
|
|
|
|
if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
|
|
|
|
length = STD_I18NTIMELENGTH;
|
|
|
|
|
|
|
|
/* Save obtained value to the cache */
|
|
|
|
i18n_timelength_cache = length;
|
|
|
|
|
|
|
|
return i18n_timelength_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
file_date (time_t when)
|
|
|
|
{
|
|
|
|
static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
|
2015-04-11 12:39:06 +03:00
|
|
|
time_t current_time = time (NULL);
|
2010-11-09 21:50:25 +03:00
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
|
|
|
|
|| current_time < when - 60L * 60L) /* In the future. */
|
|
|
|
/* The file is fairly old or in the future.
|
|
|
|
POSIX says the cutoff is 6 months old;
|
|
|
|
approximate this by 6*30 days.
|
|
|
|
Allow a 1 hour slop factor for what is considered "the future",
|
|
|
|
to allow for NFS server/client clock disagreement.
|
|
|
|
Show the year instead of the time of day. */
|
|
|
|
|
|
|
|
fmt = user_old_timeformat;
|
|
|
|
else
|
|
|
|
fmt = user_recent_timeformat;
|
|
|
|
|
|
|
|
FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
|
|
|
|
|
|
|
|
return timebuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|