timefmt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Time formatting functions
  3. Copyright (C) 1994-2021
  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. /*** file scope variables ************************************************************************/
  40. /*
  41. * Cache variable for the i18n_checktimelength function,
  42. * initially set to a clearly invalid value to show that
  43. * it hasn't been initialized yet.
  44. */
  45. static size_t i18n_timelength_cache = MAX_I18NTIMELENGTH + 1;
  46. /*** file scope functions ************************************************************************/
  47. /*** public functions ****************************************************************************/
  48. /* --------------------------------------------------------------------------------------------- */
  49. /**
  50. * Check strftime() results. Some systems (i.e. Solaris) have different
  51. * short-month and month name sizes for different locales
  52. */
  53. size_t
  54. i18n_checktimelength (void)
  55. {
  56. size_t length = 0;
  57. time_t testtime;
  58. struct tm *lt;
  59. if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
  60. return i18n_timelength_cache;
  61. testtime = time (NULL);
  62. lt = localtime (&testtime);
  63. if (lt == NULL)
  64. {
  65. /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
  66. length = str_term_width1 (_(INVALID_TIME_TEXT));
  67. }
  68. else
  69. {
  70. char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  71. size_t tlen;
  72. /* We are interested in the longest possible date */
  73. lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
  74. /* Loop through all months to find out the longest one */
  75. for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
  76. {
  77. strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
  78. tlen = (size_t) str_term_width1 (buf);
  79. length = MAX (tlen, length);
  80. strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
  81. tlen = (size_t) str_term_width1 (buf);
  82. length = MAX (tlen, length);
  83. }
  84. tlen = (size_t) str_term_width1 (_(INVALID_TIME_TEXT));
  85. length = MAX (tlen, length);
  86. }
  87. /* Don't handle big differences. Use standard value (email bug, please) */
  88. if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
  89. length = STD_I18NTIMELENGTH;
  90. /* Save obtained value to the cache */
  91. i18n_timelength_cache = length;
  92. return i18n_timelength_cache;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. const char *
  96. file_date (time_t when)
  97. {
  98. static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  99. time_t current_time = time (NULL);
  100. const char *fmt;
  101. if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
  102. || current_time < when - 60L * 60L) /* In the future. */
  103. /* The file is fairly old or in the future.
  104. POSIX says the cutoff is 6 months old;
  105. approximate this by 6*30 days.
  106. Allow a 1 hour slop factor for what is considered "the future",
  107. to allow for NFS server/client clock disagreement.
  108. Show the year instead of the time of day. */
  109. fmt = user_old_timeformat;
  110. else
  111. fmt = user_recent_timeformat;
  112. FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
  113. return timebuf;
  114. }
  115. /* --------------------------------------------------------------------------------------------- */