| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkOncePtr.h" | |
| 9 #include "SkString.h" | 8 #include "SkString.h" |
| 10 #include "SkTime.h" | 9 #include "SkTime.h" |
| 11 #include "SkTypes.h" | 10 #include "SkTypes.h" |
| 11 #include <chrono> |
| 12 | 12 |
| 13 void SkTime::DateTime::toISO8601(SkString* dst) const { | 13 void SkTime::DateTime::toISO8601(SkString* dst) const { |
| 14 if (dst) { | 14 if (dst) { |
| 15 int timeZoneMinutes = SkToInt(fTimeZoneMinutes); | 15 int timeZoneMinutes = SkToInt(fTimeZoneMinutes); |
| 16 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; | 16 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; |
| 17 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; | 17 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; |
| 18 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; | 18 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; |
| 19 dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", | 19 dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", |
| 20 static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), | 20 static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), |
| 21 static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), | 21 static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 dt->fMonth = SkToU8(tstruct->tm_mon + 1); | 58 dt->fMonth = SkToU8(tstruct->tm_mon + 1); |
| 59 dt->fDayOfWeek = SkToU8(tstruct->tm_wday); | 59 dt->fDayOfWeek = SkToU8(tstruct->tm_wday); |
| 60 dt->fDay = SkToU8(tstruct->tm_mday); | 60 dt->fDay = SkToU8(tstruct->tm_mday); |
| 61 dt->fHour = SkToU8(tstruct->tm_hour); | 61 dt->fHour = SkToU8(tstruct->tm_hour); |
| 62 dt->fMinute = SkToU8(tstruct->tm_min); | 62 dt->fMinute = SkToU8(tstruct->tm_min); |
| 63 dt->fSecond = SkToU8(tstruct->tm_sec); | 63 dt->fSecond = SkToU8(tstruct->tm_sec); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 #endif // SK_BUILD_FOR_WIN32 | 66 #endif // SK_BUILD_FOR_WIN32 |
| 67 | 67 |
| 68 #if defined(_MSC_VER) | 68 double SkTime::GetNSecs() { |
| 69 // TODO: try std::chrono again with MSVC 2015? | 69 auto now = std::chrono::high_resolution_clock::now(); |
| 70 #include <intrin.h> | 70 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); |
| 71 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); | 71 return ns.count(); |
| 72 double SkTime::GetNSecs() { | 72 } |
| 73 uint64_t ticks = __rdtsc(); | |
| 74 return ticks * *ns_per_tick.get([]{ | |
| 75 LARGE_INTEGER khz; // The docs say this returns Hz, but it returns
KHz. | |
| 76 QueryPerformanceFrequency(&khz); | |
| 77 return new double(1e6 / khz.QuadPart); | |
| 78 }); | |
| 79 } | |
| 80 #elif defined(__MACH__) | |
| 81 // TODO: fold into std::chrono when available? | |
| 82 #include <mach/mach_time.h> | |
| 83 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); | |
| 84 double SkTime::GetNSecs() { | |
| 85 uint64_t ticks = mach_absolute_time(); | |
| 86 return ticks * *ns_per_tick.get([]{ | |
| 87 mach_timebase_info_data_t timebase; | |
| 88 (void)mach_timebase_info(&timebase); | |
| 89 return new double(timebase.numer * 1.0 / timebase.denom); | |
| 90 }); | |
| 91 } | |
| 92 #else | |
| 93 // This std::chrono code looks great on Linux and Android, | |
| 94 // but MSVC 2013 returned mostly garbage (0ns times, etc). | |
| 95 #include <chrono> | |
| 96 double SkTime::GetNSecs() { | |
| 97 auto now = std::chrono::high_resolution_clock::now(); | |
| 98 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); | |
| 99 return ns.count(); | |
| 100 } | |
| 101 #endif | |
| OLD | NEW |