timefmt.h 855 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /** \file timefmt.h
  2. * \brief Header: time formating macroses
  3. */
  4. #ifndef __UTIL_TIMEFMT_H
  5. #define __UTIL_TIMEFMT_H
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #define INVALID_TIME_TEXT "(invalid)"
  9. /* safe localtime formatting - strftime()-using version */
  10. #define FMT_LOCALTIME(buffer, bufsize, fmt, when) \
  11. { \
  12. struct tm *whentm; \
  13. whentm = localtime(&when); \
  14. if (whentm == NULL) \
  15. { \
  16. strncpy(buffer, INVALID_TIME_TEXT, bufsize); \
  17. buffer[bufsize-1] = 0; \
  18. } \
  19. else \
  20. { \
  21. strftime(buffer, bufsize, fmt, whentm); \
  22. } \
  23. } \
  24. #define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt) \
  25. { \
  26. time_t __current_time; \
  27. time(&__current_time); \
  28. FMT_LOCALTIME(buffer,bufsize,fmt,__current_time); \
  29. }
  30. #endif /* !__UTIL_H */