timefmt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Time formatting functions
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2007, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Miguel de Icaza, 1994, 1995, 1996
  8. Janne Kukonlehto, 1994, 1995, 1996
  9. Dugan Porter, 1994, 1995, 1996
  10. Jakub Jelinek, 1994, 1995, 1996
  11. Mauricio Plaza, 1994, 1995, 1996
  12. The file_date routine is mostly from GNU's fileutils package,
  13. written by Richard Stallman and David MacKenzie.
  14. This file is part of the Midnight Commander.
  15. The Midnight Commander is free software: you can redistribute it
  16. and/or modify it under the terms of the GNU General Public License as
  17. published by the Free Software Foundation, either version 3 of the License,
  18. or (at your option) any later version.
  19. The Midnight Commander is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /** \file
  27. * \brief Source: time formatting functions
  28. */
  29. #include <config.h>
  30. #include <stdlib.h>
  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. const time_t testtime = time (NULL);
  58. struct tm *lt = localtime (&testtime);
  59. if (i18n_timelength_cache <= MAX_I18NTIMELENGTH)
  60. return i18n_timelength_cache;
  61. if (lt == NULL)
  62. {
  63. /* huh, localtime() doesnt seem to work ... falling back to "(invalid)" */
  64. length = str_term_width1 (_(INVALID_TIME_TEXT));
  65. }
  66. else
  67. {
  68. char buf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  69. /* We are interested in the longest possible date */
  70. lt->tm_sec = lt->tm_min = lt->tm_hour = lt->tm_mday = 10;
  71. /* Loop through all months to find out the longest one */
  72. for (lt->tm_mon = 0; lt->tm_mon < 12; lt->tm_mon++)
  73. {
  74. strftime (buf, sizeof (buf) - 1, user_recent_timeformat, lt);
  75. length = max ((size_t) str_term_width1 (buf), length);
  76. strftime (buf, sizeof (buf) - 1, user_old_timeformat, lt);
  77. length = max ((size_t) str_term_width1 (buf), length);
  78. }
  79. length = max ((size_t) str_term_width1 (_(INVALID_TIME_TEXT)), length);
  80. }
  81. /* Don't handle big differences. Use standard value (email bug, please) */
  82. if (length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH)
  83. length = STD_I18NTIMELENGTH;
  84. /* Save obtained value to the cache */
  85. i18n_timelength_cache = length;
  86. return i18n_timelength_cache;
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. const char *
  90. file_date (time_t when)
  91. {
  92. static char timebuf[MB_LEN_MAX * MAX_I18NTIMELENGTH + 1];
  93. time_t current_time = time ((time_t) 0);
  94. const char *fmt;
  95. if (current_time > when + 6L * 30L * 24L * 60L * 60L /* Old. */
  96. || current_time < when - 60L * 60L) /* In the future. */
  97. /* The file is fairly old or in the future.
  98. POSIX says the cutoff is 6 months old;
  99. approximate this by 6*30 days.
  100. Allow a 1 hour slop factor for what is considered "the future",
  101. to allow for NFS server/client clock disagreement.
  102. Show the year instead of the time of day. */
  103. fmt = user_old_timeformat;
  104. else
  105. fmt = user_recent_timeformat;
  106. FMT_LOCALTIME (timebuf, sizeof (timebuf), fmt, when);
  107. return timebuf;
  108. }
  109. /* --------------------------------------------------------------------------------------------- */