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" |
8 #include "SkString.h" | 9 #include "SkString.h" |
9 #include "SkTime.h" | 10 #include "SkTime.h" |
10 | 11 |
11 void SkTime::DateTime::toISO8601(SkString* dst) const { | 12 void SkTime::DateTime::toISO8601(SkString* dst) const { |
12 if (dst) { | 13 if (dst) { |
13 int timeZoneMinutes = SkToInt(fTimeZoneMinutes); | 14 int timeZoneMinutes = SkToInt(fTimeZoneMinutes); |
14 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; | 15 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; |
15 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; | 16 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; |
16 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; | 17 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; |
17 dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", | 18 dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", |
18 static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), | 19 static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), |
19 static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), | 20 static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), |
20 static_cast<unsigned>(fMinute), | 21 static_cast<unsigned>(fMinute), |
21 static_cast<unsigned>(fSecond), timezoneSign, timeZoneHours, | 22 static_cast<unsigned>(fSecond), timezoneSign, timeZoneHours, |
22 timeZoneMinutes); | 23 timeZoneMinutes); |
23 } | 24 } |
24 } | 25 } |
| 26 |
| 27 #if defined(_MSC_VER) |
| 28 // TODO: try std::chrono again with MSVC 2015? |
| 29 #include <intrin.h> |
| 30 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); |
| 31 double SkTime::GetNSecs() { |
| 32 uint64_t ticks = __rdtsc(); |
| 33 return ticks * *ns_per_tick.get([]{ |
| 34 LARGE_INTEGER khz; // The docs say this returns Hz, but it returns
KHz. |
| 35 QueryPerformanceFrequency(&khz); |
| 36 return new double(1e6 / khz.QuadPart); |
| 37 }); |
| 38 } |
| 39 #elif defined(__MACH__) |
| 40 // TODO: fold into std::chrono when available? |
| 41 #include <mach/mach_time.h> |
| 42 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); |
| 43 double SkTime::GetNSecs() { |
| 44 uint64_t ticks = mach_absolute_time(); |
| 45 return ticks * *ns_per_tick.get([]{ |
| 46 mach_timebase_info_data_t timebase; |
| 47 (void)mach_timebase_info(&timebase); |
| 48 return new double(timebase.numer * 1.0 / timebase.denom); |
| 49 }); |
| 50 } |
| 51 #else |
| 52 // This std::chrono code looks great on Linux and Android, |
| 53 // but MSVC 2013 returned mostly garbage (0ns times, etc). |
| 54 #include <chrono> |
| 55 double SkTime::GetNSecs() { |
| 56 auto now = std::chrono::high_resolution_clock::now(); |
| 57 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); |
| 58 return ns.count(); |
| 59 } |
| 60 #endif |
OLD | NEW |