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