localtime-buffer.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Provide access to the last buffer returned by localtime() or gmtime().
  2. Copyright (C) 2001-2003, 2005-2007, 2009-2017 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "localtime-buffer.h"
  17. #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
  18. static struct tm tm_zero_buffer;
  19. struct tm *localtime_buffer_addr = &tm_zero_buffer;
  20. /* This is a wrapper for localtime.
  21. On the first call, record the address of the static buffer that
  22. localtime uses for its result. */
  23. struct tm *
  24. rpl_localtime (time_t const *timep)
  25. {
  26. struct tm *tm = localtime (timep);
  27. if (localtime_buffer_addr == &tm_zero_buffer)
  28. localtime_buffer_addr = tm;
  29. return tm;
  30. }
  31. /* Same as above, since gmtime and localtime use the same buffer. */
  32. struct tm *
  33. rpl_gmtime (time_t const *timep)
  34. {
  35. struct tm *tm = gmtime (timep);
  36. if (localtime_buffer_addr == &tm_zero_buffer)
  37. localtime_buffer_addr = tm;
  38. return tm;
  39. }
  40. #endif