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