timefmt.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /** \file timefmt.h
  2. * \brief Header: time formating macroses
  3. */
  4. #ifndef MC__UTIL_TIMEFMT_H
  5. #define MC__UTIL_TIMEFMT_H
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. /*** typedefs(not structures) and defined constants **********************************************/
  9. #define INVALID_TIME_TEXT "(invalid)"
  10. /* safe localtime formatting - strftime()-using version */
  11. #define FMT_LOCALTIME(buffer, bufsize, fmt, when) \
  12. { \
  13. struct tm *whentm; \
  14. whentm = localtime(&when); \
  15. if (whentm == NULL) \
  16. { \
  17. strncpy(buffer, INVALID_TIME_TEXT, bufsize); \
  18. buffer[bufsize-1] = 0; \
  19. } \
  20. else \
  21. { \
  22. strftime(buffer, bufsize, fmt, whentm); \
  23. } \
  24. } \
  25. #define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt) \
  26. { \
  27. time_t __current_time; \
  28. time(&__current_time); \
  29. FMT_LOCALTIME(buffer,bufsize,fmt,__current_time); \
  30. }
  31. /*** enums ***************************************************************************************/
  32. /*** structures declarations (and typedefs of structures)*****************************************/
  33. /*** global variables defined in .c file *********************************************************/
  34. /*** declarations of public functions ************************************************************/
  35. /*** inline functions ****************************************************************************/
  36. #endif /* !__UTIL_H */