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 8 #include "SkTime.h" 9 10 #include "SkLeanWindows.h" 11 #include "SkString.h" 12 #include "SkTo.h" 13 #include "SkTypes.h" 14 15 #include <chrono> 16 17 void SkTime::DateTime::toISO8601(SkString* dst) const { 18 if (dst) { 19 int timeZoneMinutes = SkToInt(fTimeZoneMinutes); 20 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; 21 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; 22 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; 23 dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", 24 static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), 25 static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), 26 static_cast<unsigned>(fMinute), 27 static_cast<unsigned>(fSecond), timezoneSign, timeZoneHours, 28 timeZoneMinutes); 29 } 30 } 31 32 #ifdef SK_BUILD_FOR_WIN 33 34 void SkTime::GetDateTime(DateTime* dt) { 35 if (dt) { 36 SYSTEMTIME st; 37 GetSystemTime(&st); 38 dt->fTimeZoneMinutes = 0; 39 dt->fYear = st.wYear; 40 dt->fMonth = SkToU8(st.wMonth); 41 dt->fDayOfWeek = SkToU8(st.wDayOfWeek); 42 dt->fDay = SkToU8(st.wDay); 43 dt->fHour = SkToU8(st.wHour); 44 dt->fMinute = SkToU8(st.wMinute); 45 dt->fSecond = SkToU8(st.wSecond); 46 } 47 } 48 49 #else // SK_BUILD_FOR_WIN 50 51 #include <time.h> 52 void SkTime::GetDateTime(DateTime* dt) { 53 if (dt) { 54 time_t m_time; 55 time(&m_time); 56 struct tm tstruct; 57 gmtime_r(&m_time, &tstruct); 58 dt->fTimeZoneMinutes = 0; 59 dt->fYear = tstruct.tm_year + 1900; 60 dt->fMonth = SkToU8(tstruct.tm_mon + 1); 61 dt->fDayOfWeek = SkToU8(tstruct.tm_wday); 62 dt->fDay = SkToU8(tstruct.tm_mday); 63 dt->fHour = SkToU8(tstruct.tm_hour); 64 dt->fMinute = SkToU8(tstruct.tm_min); 65 dt->fSecond = SkToU8(tstruct.tm_sec); 66 } 67 } 68 #endif // SK_BUILD_FOR_WIN 69 70 #if !defined(__has_feature) 71 #define __has_feature(x) 0 72 #endif 73 74 double SkTime::GetNSecs() { 75 #if __has_feature(memory_sanitizer) 76 // See skia:6504 77 struct timespec tp; 78 clock_gettime(CLOCK_MONOTONIC, &tp); 79 return tp.tv_sec * 1e9 + tp.tv_nsec; 80 #else 81 auto now = std::chrono::high_resolution_clock::now(); 82 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); 83 return ns.count(); 84 #endif 85 } 86