Time.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <stdint.h>
  18. #include <time.h>
  19. #include <folly/portability/Config.h>
  20. // OSX is a pain. The XCode 8 SDK always declares clock_gettime
  21. // even if the target OS version doesn't support it, so you get
  22. // an error at runtime because it can't resolve the symbol. We
  23. // solve that by pretending we have it here in the header and
  24. // then enable our implementation on the source side so that
  25. // gets linked in instead.
  26. #if __MACH__ && \
  27. (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 || \
  28. __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0)
  29. #ifdef FOLLY_HAVE_CLOCK_GETTIME
  30. #undef FOLLY_HAVE_CLOCK_GETTIME
  31. #endif
  32. #define FOLLY_HAVE_CLOCK_GETTIME 1
  33. #define FOLLY_FORCE_CLOCK_GETTIME_DEFINITION 1
  34. #endif
  35. // These aren't generic implementations, so we can only declare them on
  36. // platforms we support.
  37. #if !FOLLY_HAVE_CLOCK_GETTIME && (defined(__MACH__) || defined(_WIN32))
  38. #define CLOCK_REALTIME 0
  39. #define CLOCK_MONOTONIC 1
  40. #define CLOCK_PROCESS_CPUTIME_ID 2
  41. #define CLOCK_THREAD_CPUTIME_ID 3
  42. typedef uint8_t clockid_t;
  43. extern "C" int clock_gettime(clockid_t clk_id, struct timespec* ts);
  44. extern "C" int clock_getres(clockid_t clk_id, struct timespec* ts);
  45. #endif
  46. #ifdef _WIN32
  47. #define TM_YEAR_BASE (1900)
  48. extern "C" {
  49. char* asctime_r(const tm* tm, char* buf);
  50. char* ctime_r(const time_t* t, char* buf);
  51. tm* gmtime_r(const time_t* t, tm* res);
  52. tm* localtime_r(const time_t* t, tm* o);
  53. int nanosleep(const struct timespec* request, struct timespec* remain);
  54. char* strptime(
  55. const char* __restrict buf,
  56. const char* __restrict fmt,
  57. struct tm* __restrict tm);
  58. }
  59. #endif