diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-03-29 14:50:40 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-03-29 19:42:25 +0000 |
commit | 0cf642f763b1ab8cdb3c52db80cf38e380c82a19 (patch) | |
tree | 7ae693182eb52787bf7357d4b36d882811904453 /xfa/fgas/localization/cfx_datetime.cpp | |
parent | efcae5d85f829618749461617521a076881cc493 (diff) | |
download | pdfium-0cf642f763b1ab8cdb3c52db80cf38e380c82a19.tar.xz |
Rename CFX_Unitime to CFX_DateTime
The name Unitime did not give any indication of what the class
contained. This Cl renames to DateTime to more accurately refect the
class holds a date and time.
Change-Id: I95f96224822f46a7da46ae39c71d2e23fc16f7d5
Reviewed-on: https://pdfium-review.googlesource.com/3255
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Nicolás Peña <npm@chromium.org>
Diffstat (limited to 'xfa/fgas/localization/cfx_datetime.cpp')
-rw-r--r-- | xfa/fgas/localization/cfx_datetime.cpp | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/xfa/fgas/localization/cfx_datetime.cpp b/xfa/fgas/localization/cfx_datetime.cpp new file mode 100644 index 0000000000..eb804eed7a --- /dev/null +++ b/xfa/fgas/localization/cfx_datetime.cpp @@ -0,0 +1,129 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/fx_system.h" +#include "xfa/fgas/localization/cfx_datetime.h" + +#if _FX_OS_ == _FX_LINUX_DESKTOP_ || _FX_OS_ == _FX_ANDROID_ || \ + _FX_OS_ == _FX_MACOSX_ || _FX_OS_ == _FX_IOS_ +#include <sys/time.h> +#include <time.h> +#endif + +namespace { + +const uint8_t g_FXDaysPerMonth[12] = {31, 28, 31, 30, 31, 30, + 31, 31, 30, 31, 30, 31}; +const uint8_t g_FXDaysPerLeapMonth[12] = {31, 29, 31, 30, 31, 30, + 31, 31, 30, 31, 30, 31}; +const int32_t g_FXDaysBeforeMonth[12] = {0, 31, 59, 90, 120, 151, + 181, 212, 243, 273, 304, 334}; +const int32_t g_FXDaysBeforeLeapMonth[12] = {0, 31, 60, 91, 121, 152, + 182, 213, 244, 274, 305, 335}; +const int32_t g_FXDaysPerYear = 365; +const int32_t g_FXDaysPerLeapYear = 366; + +int32_t DaysBeforeMonthInYear(int32_t iYear, uint8_t iMonth) { + ASSERT(iYear != 0); + ASSERT(iMonth >= 1 && iMonth <= 12); + + const int32_t* p = + FX_IsLeapYear(iYear) ? g_FXDaysBeforeLeapMonth : g_FXDaysBeforeMonth; + return p[iMonth - 1]; +} + +int32_t DaysInYear(int32_t iYear) { + ASSERT(iYear != 0); + return FX_IsLeapYear(iYear) ? g_FXDaysPerLeapYear : g_FXDaysPerYear; +} + +int64_t DateToDays(int32_t iYear, + uint8_t iMonth, + uint8_t iDay, + bool bIncludeThisDay) { + ASSERT(iYear != 0); + ASSERT(iMonth >= 1 && iMonth <= 12); + ASSERT(iDay >= 1 && iDay <= FX_DaysInMonth(iYear, iMonth)); + + int64_t iDays = DaysBeforeMonthInYear(iYear, iMonth); + iDays += iDay; + if (!bIncludeThisDay) + iDays--; + + if (iYear > 0) { + iYear--; + } else { + iDays -= DaysInYear(iYear); + iYear++; + } + return iDays + static_cast<int64_t>(iYear) * 365 + iYear / 4 - iYear / 100 + + iYear / 400; +} + +struct FXUT_SYSTEMTIME { + uint16_t wYear; + uint16_t wMonth; + uint16_t wDayOfWeek; + uint16_t wDay; + uint16_t wHour; + uint16_t wMinute; + uint16_t wSecond; + uint16_t wMillisecond; +}; + +} // namespace + +uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth) { + ASSERT(iYear != 0); + ASSERT(iMonth >= 1 && iMonth <= 12); + + const uint8_t* p = + FX_IsLeapYear(iYear) ? g_FXDaysPerLeapMonth : g_FXDaysPerMonth; + return p[iMonth - 1]; +} + +bool FX_IsLeapYear(int32_t iYear) { + ASSERT(iYear != 0); + return ((iYear % 4) == 0 && (iYear % 100) != 0) || (iYear % 400) == 0; +} + +void CFX_DateTime::Now() { + FXUT_SYSTEMTIME utLocal; +#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ + _FX_OS_ == _FX_WIN64_ + ::GetLocalTime((LPSYSTEMTIME)&utLocal); +#elif _FX_OS_ != _FX_EMBEDDED_ + timeval curTime; + gettimeofday(&curTime, nullptr); + + struct tm st; + localtime_r(&curTime.tv_sec, &st); + utLocal.wYear = st.tm_year + 1900; + utLocal.wMonth = st.tm_mon + 1; + utLocal.wDayOfWeek = st.tm_wday; + utLocal.wDay = st.tm_mday; + utLocal.wHour = st.tm_hour; + utLocal.wMinute = st.tm_min; + utLocal.wSecond = st.tm_sec; + utLocal.wMillisecond = curTime.tv_usec / 1000; +#endif // _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ + // _FX_OS_ == _FX_WIN64_ + + year_ = utLocal.wYear; + month_ = static_cast<uint8_t>(utLocal.wMonth); + day_ = static_cast<uint8_t>(utLocal.wDay); + hour_ = static_cast<uint8_t>(utLocal.wHour); + minute_ = static_cast<uint8_t>(utLocal.wMinute); + second_ = static_cast<uint8_t>(utLocal.wSecond); + millisecond_ = static_cast<uint16_t>(utLocal.wMillisecond); +} + +int32_t CFX_DateTime::GetDayOfWeek() const { + int32_t v = static_cast<int32_t>(DateToDays(year_, month_, day_, true) % 7); + if (v < 0) + v += 7; + return v; +} |