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