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