SkTime.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkTime.h"
  8. #include "include/core/SkString.h"
  9. #include "include/core/SkTypes.h"
  10. #include "include/private/SkTo.h"
  11. #include "src/core/SkLeanWindows.h"
  12. #include <chrono>
  13. void SkTime::DateTime::toISO8601(SkString* dst) const {
  14. if (dst) {
  15. int timeZoneMinutes = SkToInt(fTimeZoneMinutes);
  16. char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-';
  17. int timeZoneHours = SkTAbs(timeZoneMinutes) / 60;
  18. timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60;
  19. dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d",
  20. static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth),
  21. static_cast<unsigned>(fDay), static_cast<unsigned>(fHour),
  22. static_cast<unsigned>(fMinute),
  23. static_cast<unsigned>(fSecond), timezoneSign, timeZoneHours,
  24. timeZoneMinutes);
  25. }
  26. }
  27. #ifdef SK_BUILD_FOR_WIN
  28. void SkTime::GetDateTime(DateTime* dt) {
  29. if (dt) {
  30. SYSTEMTIME st;
  31. GetSystemTime(&st);
  32. dt->fTimeZoneMinutes = 0;
  33. dt->fYear = st.wYear;
  34. dt->fMonth = SkToU8(st.wMonth);
  35. dt->fDayOfWeek = SkToU8(st.wDayOfWeek);
  36. dt->fDay = SkToU8(st.wDay);
  37. dt->fHour = SkToU8(st.wHour);
  38. dt->fMinute = SkToU8(st.wMinute);
  39. dt->fSecond = SkToU8(st.wSecond);
  40. }
  41. }
  42. #else // SK_BUILD_FOR_WIN
  43. #include <time.h>
  44. void SkTime::GetDateTime(DateTime* dt) {
  45. if (dt) {
  46. time_t m_time;
  47. time(&m_time);
  48. struct tm tstruct;
  49. gmtime_r(&m_time, &tstruct);
  50. dt->fTimeZoneMinutes = 0;
  51. dt->fYear = tstruct.tm_year + 1900;
  52. dt->fMonth = SkToU8(tstruct.tm_mon + 1);
  53. dt->fDayOfWeek = SkToU8(tstruct.tm_wday);
  54. dt->fDay = SkToU8(tstruct.tm_mday);
  55. dt->fHour = SkToU8(tstruct.tm_hour);
  56. dt->fMinute = SkToU8(tstruct.tm_min);
  57. dt->fSecond = SkToU8(tstruct.tm_sec);
  58. }
  59. }
  60. #endif // SK_BUILD_FOR_WIN
  61. #if !defined(__has_feature)
  62. #define __has_feature(x) 0
  63. #endif
  64. double SkTime::GetNSecs() {
  65. #if __has_feature(memory_sanitizer)
  66. // See skia:6504
  67. struct timespec tp;
  68. clock_gettime(CLOCK_MONOTONIC, &tp);
  69. return tp.tv_sec * 1e9 + tp.tv_nsec;
  70. #else
  71. auto now = std::chrono::steady_clock::now();
  72. std::chrono::duration<double, std::nano> ns = now.time_since_epoch();
  73. return ns.count();
  74. #endif
  75. }