systime.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <util/system/platform.h>
  3. #include <util/generic/string.h>
  4. #include <ctime>
  5. // timegm and gmtime_r versions that don't need access to filesystem or a big stack
  6. time_t TimeGM(const struct tm* t);
  7. struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf);
  8. // safe version of ctime, convinient version of ctime_r
  9. TString CTimeR(const time_t* timer);
  10. #ifdef _win_
  11. #include <util/system/winint.h>
  12. #include <winsock2.h>
  13. // Convert FILETIME to timeval - seconds and microseconds.
  14. void FileTimeToTimeval(const FILETIME* ft, struct timeval* tv);
  15. // Convert FILETIME to timespec - seconds and nanoseconds.
  16. void FileTimeToTimespec(const FILETIME& ft, struct timespec* ts);
  17. // obtains the current time, expressed as seconds and microseconds since 00:00 UTC, January 1, 1970
  18. int gettimeofday(struct timeval* tp, void*);
  19. // thou should not mix these with non-_r functions
  20. tm* localtime_r(const time_t* clock, tm* result);
  21. tm* gmtime_r(const time_t* clock, tm* result);
  22. char* ctime_r(const time_t* clock, char* buf);
  23. inline time_t timegm(struct tm* t) {
  24. return TimeGM(t);
  25. }
  26. char* strptime(const char* buf, const char* fmt, struct tm* tm); // strptime.cpp
  27. #else
  28. #include <sys/time.h>
  29. #endif
  30. #ifndef timersub
  31. #define timersub(tvp, uvp, vvp) \
  32. do { \
  33. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  34. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  35. if ((vvp)->tv_usec < 0) { \
  36. (vvp)->tv_sec--; \
  37. (vvp)->tv_usec += 1000000; \
  38. } \
  39. } while (0)
  40. #endif