sys_time.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. // This file is a portable substitute for sys/time.h which does not exist on
  10. // Windows
  11. #pragma once
  12. #include "rocksdb/rocksdb_namespace.h"
  13. #if defined(OS_WIN) && (defined(_MSC_VER) || defined(__MINGW32__))
  14. #include <time.h>
  15. namespace ROCKSDB_NAMESPACE {
  16. namespace port {
  17. struct TimeVal {
  18. long tv_sec;
  19. long tv_usec;
  20. };
  21. void GetTimeOfDay(TimeVal* tv, struct timezone* tz);
  22. inline struct tm* LocalTimeR(const time_t* timep, struct tm* result) {
  23. errno_t ret = localtime_s(result, timep);
  24. return (ret == 0) ? result : NULL;
  25. }
  26. } // namespace port
  27. } // namespace ROCKSDB_NAMESPACE
  28. #else
  29. #include <sys/time.h>
  30. #include <time.h>
  31. namespace ROCKSDB_NAMESPACE {
  32. namespace port {
  33. using TimeVal = struct timeval;
  34. inline void GetTimeOfDay(TimeVal* tv, struct timezone* tz) {
  35. gettimeofday(tv, tz);
  36. }
  37. inline struct tm* LocalTimeR(const time_t* timep, struct tm* result) {
  38. return localtime_r(timep, result);
  39. }
  40. } // namespace port
  41. } // namespace ROCKSDB_NAMESPACE
  42. #endif