time_win.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "util/misc/time.h"
  15. #include <stdint.h>
  16. #include "base/logging.h"
  17. namespace crashpad {
  18. namespace {
  19. constexpr uint64_t kMicrosecondsPerSecond = static_cast<uint64_t>(1E6);
  20. constexpr uint64_t kNanosecondsPerFiletimeInterval = static_cast<uint64_t>(100);
  21. constexpr uint64_t kFiletimeIntervalsPerSecond =
  22. kNanosecondsPerSecond / kNanosecondsPerFiletimeInterval;
  23. constexpr uint64_t kFiletimeIntervalsPerMicrosecond =
  24. kFiletimeIntervalsPerSecond / kMicrosecondsPerSecond;
  25. // Windows epoch is 1601-01-01, and FILETIME ticks are 100 nanoseconds.
  26. // 1601 to 1970 is 369 years + 89 leap days = 134774 days * 86400 seconds per
  27. // day. It's not entirely clear, but it appears that these are solar seconds,
  28. // not SI seconds, so there are no leap seconds to be considered.
  29. constexpr uint64_t kNumSecondsFrom1601To1970 = (369 * 365 + 89) * 86400ULL;
  30. uint64_t FiletimeToMicroseconds(const FILETIME& filetime) {
  31. uint64_t t = (static_cast<uint64_t>(filetime.dwHighDateTime) << 32) |
  32. filetime.dwLowDateTime;
  33. return t / kFiletimeIntervalsPerMicrosecond;
  34. }
  35. timeval MicrosecondsToTimeval(uint64_t microseconds) {
  36. timeval tv;
  37. tv.tv_sec = static_cast<long>(microseconds / kMicrosecondsPerSecond);
  38. tv.tv_usec = static_cast<long>(microseconds % kMicrosecondsPerSecond);
  39. return tv;
  40. }
  41. } // namespace
  42. FILETIME TimespecToFiletimeEpoch(const timespec& ts) {
  43. uint64_t intervals =
  44. (kNumSecondsFrom1601To1970 + ts.tv_sec) * kFiletimeIntervalsPerSecond +
  45. ts.tv_nsec / kNanosecondsPerFiletimeInterval;
  46. FILETIME filetime;
  47. filetime.dwLowDateTime = intervals & 0xffffffff;
  48. filetime.dwHighDateTime = intervals >> 32;
  49. return filetime;
  50. }
  51. timespec FiletimeToTimespecEpoch(const FILETIME& filetime) {
  52. uint64_t intervals =
  53. (uint64_t{filetime.dwHighDateTime} << 32) | filetime.dwLowDateTime;
  54. timespec result;
  55. result.tv_sec =
  56. (intervals / kFiletimeIntervalsPerSecond) - kNumSecondsFrom1601To1970;
  57. result.tv_nsec =
  58. static_cast<long>(intervals % kFiletimeIntervalsPerSecond) *
  59. kNanosecondsPerFiletimeInterval;
  60. return result;
  61. }
  62. timeval FiletimeToTimevalEpoch(const FILETIME& filetime) {
  63. uint64_t microseconds = FiletimeToMicroseconds(filetime);
  64. DCHECK_GE(microseconds, kNumSecondsFrom1601To1970 * kMicrosecondsPerSecond);
  65. microseconds -= kNumSecondsFrom1601To1970 * kMicrosecondsPerSecond;
  66. return MicrosecondsToTimeval(microseconds);
  67. }
  68. timeval FiletimeToTimevalInterval(const FILETIME& filetime) {
  69. return MicrosecondsToTimeval(FiletimeToMicroseconds(filetime));
  70. }
  71. void GetTimeOfDay(timeval* tv) {
  72. FILETIME filetime;
  73. GetSystemTimeAsFileTime(&filetime);
  74. *tv = FiletimeToTimevalEpoch(filetime);
  75. }
  76. } // namespace crashpad