time.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_UTIL_MISC_TIME_H_
  15. #define CRASHPAD_UTIL_MISC_TIME_H_
  16. #include <stdint.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include "build/build_config.h"
  20. #if defined(OS_WIN)
  21. #include <windows.h>
  22. #endif
  23. namespace crashpad {
  24. constexpr uint64_t kNanosecondsPerSecond = static_cast<uint64_t>(1E9);
  25. //! \brief Add `timespec` \a ts1 and \a ts2 and return the result in \a result.
  26. void AddTimespec(const timespec& ts1, const timespec& ts2, timespec* result);
  27. //! \brief Subtract `timespec` \a ts2 from \a ts1 and return the result in \a
  28. //! result.
  29. void SubtractTimespec(const timespec& ts1,
  30. const timespec& ts2,
  31. timespec* result);
  32. //! \brief Convert the timespec \a ts to a timeval \a tv.
  33. //! \return `true` if the assignment is possible without truncation.
  34. bool TimespecToTimeval(const timespec& ts, timeval* tv);
  35. //! \brief Convert the timeval \a tv to a timespec \a ts.
  36. void TimevalToTimespec(const timeval& tv, timespec* ts);
  37. #if defined(OS_WIN) || DOXYGEN
  38. //! \brief Convert a `timespec` to a Windows `FILETIME`, converting from POSIX
  39. //! epoch to Windows epoch.
  40. FILETIME TimespecToFiletimeEpoch(const timespec& ts);
  41. //! \brief Convert a Windows `FILETIME` to `timespec`, converting from Windows
  42. //! epoch to POSIX epoch.
  43. timespec FiletimeToTimespecEpoch(const FILETIME& filetime);
  44. //! \brief Convert Windows `FILETIME` to `timeval`, converting from Windows
  45. //! epoch to POSIX epoch.
  46. timeval FiletimeToTimevalEpoch(const FILETIME& filetime);
  47. //! \brief Convert Windows `FILETIME` to `timeval`, treating the values as
  48. //! an interval of elapsed time.
  49. timeval FiletimeToTimevalInterval(const FILETIME& filetime);
  50. //! \brief Similar to POSIX `gettimeofday()`, gets the current system time in
  51. //! UTC.
  52. void GetTimeOfDay(timeval* tv);
  53. #endif // OS_WIN
  54. } // namespace crashpad
  55. #endif // CRASHPAD_UTIL_MISC_TIME_H_