timefmt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Time formatting functions
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Miguel de Icaza, 1994, 1995, 1996
  7. Janne Kukonlehto, 1994, 1995, 1996
  8. Dugan Porter, 1994, 1995, 1996
  9. Jakub Jelinek, 1994, 1995, 1996
  10. Mauricio Plaza, 1994, 1995, 1996
  11. The file_date routine is mostly from GNU's fileutils package,
  12. written by Richard Stallman and David MacKenzie.
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file
  26. * \brief Source: time formatting functions
  27. */
  28. #include <config.h>
  29. #include <stdlib.h>
  30. #include <limits.h> /* MB_LEN_MAX */
  31. #include "lib/global.h"
  32. #include "lib/strutil.h"
  33. #include "lib/timefmt.h"
  34. /*** global variables ****************************************************************************/
  35. char *user_recent_timeformat = NULL; /* time format string for recent dates */
  36. char *user_old_timeformat = NULL; /* time format string for older dates */
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** forward declarations (file scope functions) *************************************************/
  40. /*** file scope variables ************************************************************************/
  41. /*
  42. * Cache variable for the i18n_checktimelength function,
  43. * initially set to a clearly invalid value to show that
  44. * it hasn't been initialized yet.
  45. */
  46. static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
  47. /* --------------------------------------------------------------------------------------------- */
  48. /*** file scope functions ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. /* --------------------------------------------------------------------------------------------- */
  51. /*** public functions ****************************************************************************/
  52. /* --------------------------------------------------------------------------------------------- */
  53. /**
  54. * Check strftime() results. Some systems (i.e. Solaris) have different
  55. * short-month and month name sizes for different locales
  56. */
  57. size_t
  58. i18n_checktimelength (void)
  59. {
  60. size_t length = 0;
  61. time_t testtime;
  62. struct tm *lt;
  63. if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
  64. return i18n_timelength_cache;
  65. testtime = time (NULL);
  66. lt = localtime (&testtime);
  67. if (lt == NULL)
  68. {
  69. /* huh, localtime() doesn't seem to work ... falling back to "(invalid)" */
  70. length = str_term_width1 (_(INVALID_TIME_TEXT));
  71. }
  72. else
  73. {
  74. char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  75. size_t tlen;
  76. /* We are interested in the longest possible date */
  77. lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
  78. /* Loop through all months to find out the longest one */
  79. for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
  80. {
  81. strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
  82. tlen = (size_t) str_term_width1 (buf);
  83. length = MAX (tlen, length);
  84. strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
  85. tlen = (size_t) str_term_width1 (buf);
  86. length = MAX (tlen, length);
  87. }
  88. tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT));
  89. length = MAX (tlen, length);
  90. }
  91. /* Don't handle big differences. Use standard value (email bug, please) */
  92. if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
  93. length = STD_I18NTIMELENGTH;
  94. /* Save obtained value to the cache */
  95. i18n_timelength_cache = length;
  96. return i18n_timelength_cache;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. const char *
  100. file_date (time_t when)
  101. {
  102. static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  103. time_t current_time = time (NULL);
  104. const char *fmt;
  105. if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
  106. || current_time < when - 60L * 60L) /* In the future. */
  107. /* The file is fairly old or in the future.
  108. POSIX says the cutoff is 6 months old;
  109. approximate this by 6*30 days.
  110. Allow a 1 hour slop factor for what is considered "the future",
  111. to allow for NFS server/client clock disagreement.
  112. Show the year instead of the time of day. */
  113. fmt = user_old_timeformat;
  114. else
  115. fmt = user_recent_timeformat;
  116. FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
  117. return timebuf;
  118. }
  119. /* --------------------------------------------------------------------------------------------- */