diff options
author | halcanary <halcanary@google.com> | 2015-05-01 07:06:23 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-01 07:06:23 -0700 |
commit | 0b9d4118ba4f3f1190c063492894e324c63e8fd8 (patch) | |
tree | d7b7faaa33d11420c02097b1653d08871f845f98 | |
parent | d309e7aa0efa2d5dd7e7b1af97026fcd3a047e98 (diff) |
SkTime: return timezone information; format in ISO-8601
Motivation: PDF/A metadata will need the creation date embedded in it.
Also, GetDateTime returns local time in Win32. This now behaves the
same as on Unix systems.
BUG=skia:3110
Review URL: https://codereview.chromium.org/1109593002
-rw-r--r-- | gyp/core.gypi | 1 | ||||
-rw-r--r-- | include/core/SkTime.h | 6 | ||||
-rw-r--r-- | src/core/SkTime.cpp | 24 | ||||
-rw-r--r-- | src/ports/SkTime_Unix.cpp | 4 | ||||
-rw-r--r-- | src/ports/SkTime_win.cpp | 18 | ||||
-rw-r--r-- | tests/Time.cpp | 12 |
6 files changed, 63 insertions, 2 deletions
diff --git a/gyp/core.gypi b/gyp/core.gypi index 1f062524ae..fef6fe45b3 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -208,6 +208,7 @@ '<(skia_src_path)/core/SkTextBlob.cpp', '<(skia_src_path)/core/SkTextFormatParams.h', '<(skia_src_path)/core/SkTextMapStateProc.h', + '<(skia_src_path)/core/SkTime.cpp', '<(skia_src_path)/core/SkTDPQueue.h', '<(skia_src_path)/core/SkTLList.h', '<(skia_src_path)/core/SkTLS.cpp', diff --git a/include/core/SkTime.h b/include/core/SkTime.h index 51616d41c7..a8656347aa 100644 --- a/include/core/SkTime.h +++ b/include/core/SkTime.h @@ -12,12 +12,16 @@ #include "SkTypes.h" +class SkString; + /** \class SkTime Platform-implemented utilities to return time of day, and millisecond counter. */ class SkTime { public: struct DateTime { + int16_t fTimeZoneMinutes; // The number of minutes that GetDateTime() + // is ahead of or behind UTC. uint16_t fYear; //!< e.g. 2005 uint8_t fMonth; //!< 1..12 uint8_t fDayOfWeek; //!< 0..6, 0==Sunday @@ -25,6 +29,8 @@ public: uint8_t fHour; //!< 0..23 uint8_t fMinute; //!< 0..59 uint8_t fSecond; //!< 0..59 + + void toISO8601(SkString* dst) const; }; static void GetDateTime(DateTime*); diff --git a/src/core/SkTime.cpp b/src/core/SkTime.cpp new file mode 100644 index 0000000000..b8e42365ef --- /dev/null +++ b/src/core/SkTime.cpp @@ -0,0 +1,24 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkString.h" +#include "SkTime.h" + +void SkTime::DateTime::toISO8601(SkString* dst) const { + if (dst) { + int timeZoneMinutes = SkToInt(fTimeZoneMinutes); + char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; + int timeZoneHours = abs(timeZoneMinutes) / 60; + timeZoneMinutes = abs(timeZoneMinutes) % 60; + dst->printf("%04u-%02u-%02uT%02u:%02u:%02u%c%02d:%02d", + static_cast<unsigned>(fYear), static_cast<unsigned>(fMonth), + static_cast<unsigned>(fDay), static_cast<unsigned>(fHour), + static_cast<unsigned>(fMinute), + static_cast<unsigned>(fSecond), timezoneSign, timeZoneHours, + timeZoneMinutes); + } +} diff --git a/src/ports/SkTime_Unix.cpp b/src/ports/SkTime_Unix.cpp index 6e305a16f6..2d5fa279b4 100644 --- a/src/ports/SkTime_Unix.cpp +++ b/src/ports/SkTime_Unix.cpp @@ -16,11 +16,15 @@ void SkTime::GetDateTime(DateTime* dt) { if (dt) { + tzset(); // initialize timezone variable; time_t m_time; time(&m_time); struct tm* tstruct; tstruct = localtime(&m_time); + int offset = tstruct->tm_isdst == 1 ? 60 : 0; + // http://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html + dt->fTimeZoneMinutes = SkToS16(offset - timezone / 60); dt->fYear = tstruct->tm_year + 1900; dt->fMonth = SkToU8(tstruct->tm_mon + 1); dt->fDayOfWeek = SkToU8(tstruct->tm_wday); diff --git a/src/ports/SkTime_win.cpp b/src/ports/SkTime_win.cpp index bc0f8f0084..19f4695a86 100644 --- a/src/ports/SkTime_win.cpp +++ b/src/ports/SkTime_win.cpp @@ -14,8 +14,22 @@ void SkTime::GetDateTime(DateTime* dt) if (dt) { SYSTEMTIME st; - GetSystemTime(&st); - + TIME_ZONE_INFORMATION timeZoneInfo; + int tz_bias; + GetLocalTime(&st); + // https://gist.github.com/wrl/8924636 + switch (GetTimeZoneInformation(&timeZoneInfo)) { + case TIME_ZONE_ID_STANDARD: + tz_bias = -timeZoneInfo.Bias - timeZoneInfo.StandardBias; + break; + case TIME_ZONE_ID_DAYLIGHT: + tz_bias = -timeZoneInfo.Bias - timeZoneInfo.DaylightBias; + break; + default: + tz_bias = -timeZoneInfo.Bias; + break; + } + dt->fTimeZoneMinutes = SkToS16(tz_bias); dt->fYear = st.wYear; dt->fMonth = SkToU8(st.wMonth); dt->fDayOfWeek = SkToU8(st.wDayOfWeek); diff --git a/tests/Time.cpp b/tests/Time.cpp index 0139654e19..60dcd5527c 100644 --- a/tests/Time.cpp +++ b/tests/Time.cpp @@ -41,4 +41,16 @@ DEF_TEST(Time_GetDateTime, r) { REPORTER_ASSERT(r, dateTime.fMinute <= 59); REPORTER_ASSERT(r, dateTime.fSecond <= 60); // leap seconds are 23:59:60 + + // The westernmost timezone is -12:00. + // The easternmost timezone is +14:00. + REPORTER_ASSERT(r, abs(SkToInt(dateTime.fTimeZoneMinutes)) <= 14 * 60); + + SkString timeStamp; + dateTime.toISO8601(&timeStamp); + REPORTER_ASSERT(r, timeStamp.size() > 0); + if (r->verbose()) { // `dm --veryVerbose` + SkDebugf("\nCurrent Time (ISO-8601 format): \"%s\"\n", + timeStamp.c_str()); + } } |