timefmt.c 4.8 KB

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