summaryrefslogtreecommitdiff
path: root/xfa/fgas
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-03-20 15:04:16 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-03-20 20:21:31 +0000
commitcf2d0ca3d5ba890dca4e2735915739430a43cfa0 (patch)
tree3045ca50e2305ea348eb743576e04bdf8ce144f1 /xfa/fgas
parent2a6482c19c5577924f73d100431acceb8c874e04 (diff)
downloadpdfium-cf2d0ca3d5ba890dca4e2735915739430a43cfa0.tar.xz
Simplify date time code
This CL removes the CFX_DateTime class and moves the one used method into the CFX_Unitime class. Change-Id: I89f6d099b2c436d8f12b9dc2c5f10ac236ad56ef Reviewed-on: https://pdfium-review.googlesource.com/3104 Reviewed-by: Nicolás Peña <npm@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'xfa/fgas')
-rw-r--r--xfa/fgas/localization/cfx_formatstring.h2
-rw-r--r--xfa/fgas/localization/fgas_datetime.cpp182
-rw-r--r--xfa/fgas/localization/fgas_datetime.h130
-rw-r--r--xfa/fgas/localization/fgas_locale.cpp49
-rw-r--r--xfa/fgas/localization/fgas_locale.h4
5 files changed, 101 insertions, 266 deletions
diff --git a/xfa/fgas/localization/cfx_formatstring.h b/xfa/fgas/localization/cfx_formatstring.h
index 19764da85c..b93771d919 100644
--- a/xfa/fgas/localization/cfx_formatstring.h
+++ b/xfa/fgas/localization/cfx_formatstring.h
@@ -30,7 +30,7 @@ class CFX_FormatString {
bool ParseDateTime(const CFX_WideString& wsSrcDateTime,
const CFX_WideString& wsPattern,
FX_DATETIMETYPE eDateTimeType,
- CFX_Unitime& dtValue);
+ CFX_Unitime* dtValue);
bool ParseZero(const CFX_WideString& wsSrcText,
const CFX_WideString& wsPattern);
bool ParseNull(const CFX_WideString& wsSrcText,
diff --git a/xfa/fgas/localization/fgas_datetime.cpp b/xfa/fgas/localization/fgas_datetime.cpp
index 0d5b2b9b45..973fb3b5e3 100644
--- a/xfa/fgas/localization/fgas_datetime.cpp
+++ b/xfa/fgas/localization/fgas_datetime.cpp
@@ -25,22 +25,6 @@ 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;
-const int32_t g_FXDaysPer4Years = 1461;
-const int32_t g_FXDaysPer100Years = 36524;
-const int32_t g_FXDaysPer400Years = 146097;
-const int64_t g_FXMillisecondsPerSecond = 1000;
-const int64_t g_FXMillisecondsPerMinute = 60000;
-const int64_t g_FXMillisecondsPerHour = 3600000;
-const int64_t g_FXMillisecondsPerDay = 86400000;
-
-int64_t GetDayOfAD(int64_t unitime) {
- bool bBC = unitime < 0;
- int64_t iDays = unitime / g_FXMillisecondsPerDay;
- iDays += bBC ? -1 : 0;
- if (bBC && (unitime % g_FXMillisecondsPerDay) == 0)
- iDays++;
- return iDays;
-}
int32_t DaysBeforeMonthInYear(int32_t iYear, uint8_t iMonth) {
ASSERT(iYear != 0);
@@ -79,64 +63,6 @@ int64_t DateToDays(int32_t iYear,
iYear / 400;
}
-void DaysToDate(int64_t iDays,
- int32_t* retYear,
- uint8_t* retMonth,
- uint8_t* retDay) {
- bool bBC = iDays < 0;
- if (bBC)
- iDays = -iDays;
-
- int32_t iYear = 1;
- uint8_t iMonth = 1;
- uint8_t iDay = 1;
- if (iDays >= g_FXDaysPer400Years) {
- iYear += static_cast<int32_t>(iDays / g_FXDaysPer400Years * 400);
- iDays %= g_FXDaysPer400Years;
- }
-
- if (iDays >= g_FXDaysPer100Years) {
- if (iDays == g_FXDaysPer100Years * 4) {
- iYear += 300;
- iDays -= g_FXDaysPer100Years * 3;
- } else {
- iYear += static_cast<int32_t>(iDays / g_FXDaysPer100Years * 100);
- iDays %= g_FXDaysPer100Years;
- }
- }
-
- if (iDays >= g_FXDaysPer4Years) {
- iYear += static_cast<int32_t>(iDays / g_FXDaysPer4Years * 4);
- iDays %= g_FXDaysPer4Years;
- }
-
- while (true) {
- int32_t iYearDays = DaysInYear(iYear);
- if (iDays < iYearDays) {
- if (bBC) {
- iYear = -iYear;
- iDays = iYearDays - iDays;
- }
- break;
- }
- iYear++;
- iDays -= iYearDays;
- }
- while (true) {
- int32_t iMonthDays = FX_DaysInMonth(iYear, iMonth);
- if (iDays < iMonthDays)
- break;
-
- iMonth++;
- iDays -= iMonthDays;
- }
- iDay += static_cast<uint8_t>(iDays);
-
- *retYear = iYear;
- *retMonth = iMonth;
- *retDay = iDay;
-}
-
struct FXUT_SYSTEMTIME {
uint16_t wYear;
uint16_t wMonth;
@@ -145,7 +71,7 @@ struct FXUT_SYSTEMTIME {
uint16_t wHour;
uint16_t wMinute;
uint16_t wSecond;
- uint16_t wMilliseconds;
+ uint16_t wMillisecond;
};
} // namespace
@@ -182,107 +108,21 @@ void CFX_Unitime::Now() {
utLocal.wHour = st.tm_hour;
utLocal.wMinute = st.tm_min;
utLocal.wSecond = st.tm_sec;
- utLocal.wMilliseconds = curTime.tv_usec / 1000;
+ utLocal.wMillisecond = curTime.tv_usec / 1000;
#endif // _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
// _FX_OS_ == _FX_WIN64_
- Set(utLocal.wYear, static_cast<uint8_t>(utLocal.wMonth),
- static_cast<uint8_t>(utLocal.wDay), static_cast<uint8_t>(utLocal.wHour),
- static_cast<uint8_t>(utLocal.wMinute),
- static_cast<uint8_t>(utLocal.wSecond),
- static_cast<uint16_t>(utLocal.wMilliseconds));
-}
-
-void CFX_Unitime::Set(int32_t year,
- uint8_t month,
- uint8_t day,
- uint8_t hour,
- uint8_t minute,
- uint8_t second,
- uint16_t millisecond) {
- ASSERT(hour <= 23);
- ASSERT(minute <= 59);
- ASSERT(second <= 59);
- ASSERT(millisecond <= 999);
-
- m_iUnitime = static_cast<int64_t>(hour) * g_FXMillisecondsPerHour +
- static_cast<int64_t>(minute) * g_FXMillisecondsPerMinute +
- static_cast<int64_t>(second) * g_FXMillisecondsPerSecond +
- millisecond;
- if (year <= 0)
- return;
-
- m_iUnitime += DateToDays(year, month, day, false) * g_FXMillisecondsPerDay;
-}
-
-int32_t CFX_Unitime::GetYear() const {
- int32_t iYear;
- uint8_t iMonth, iDay;
- DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
- return iYear;
-}
-
-uint8_t CFX_Unitime::GetMonth() const {
- int32_t iYear;
- uint8_t iMonth, iDay;
- DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
- return iMonth;
-}
-
-uint8_t CFX_Unitime::GetDay() const {
- int32_t iYear;
- uint8_t iMonth, iDay;
- DaysToDate(GetDayOfAD(m_iUnitime), &iYear, &iMonth, &iDay);
- return iDay;
-}
-
-uint8_t CFX_Unitime::GetHour() const {
- int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerDay);
- if (v < 0)
- v += g_FXMillisecondsPerDay;
- return static_cast<uint8_t>(v / g_FXMillisecondsPerHour);
-}
-
-uint8_t CFX_Unitime::GetMinute() const {
- int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerHour);
- if (v < 0)
- v += g_FXMillisecondsPerHour;
- return static_cast<uint8_t>(v / g_FXMillisecondsPerMinute);
-}
-
-uint8_t CFX_Unitime::GetSecond() const {
- int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerMinute);
- if (v < 0)
- v += g_FXMillisecondsPerMinute;
- return static_cast<uint8_t>(v / g_FXMillisecondsPerSecond);
-}
-
-uint16_t CFX_Unitime::GetMillisecond() const {
- int32_t v = static_cast<int32_t>(m_iUnitime % g_FXMillisecondsPerSecond);
- if (v < 0)
- v += g_FXMillisecondsPerSecond;
- return static_cast<uint16_t>(v);
-}
-
-bool CFX_DateTime::Set(int32_t year, uint8_t month, uint8_t day) {
- ASSERT(year != 0);
- ASSERT(month >= 1 && month <= 12);
- ASSERT(day >= 1 && day <= FX_DaysInMonth(year, month));
- m_DateTime.Date.sDate.year = year;
- m_DateTime.Date.sDate.month = month;
- m_DateTime.Date.sDate.day = day;
- m_DateTime.Time.sTime.hour = 0;
- m_DateTime.Time.sTime.minute = 0;
- m_DateTime.Time.sTime.second = 0;
- m_DateTime.Time.sTime.millisecond = 0;
- return true;
+ 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(m_DateTime.Date.sDate.year,
- m_DateTime.Date.sDate.month,
- m_DateTime.Date.sDate.day, true) %
- 7);
+int32_t CFX_Unitime::GetDayOfWeek() const {
+ int32_t v = static_cast<int32_t>(DateToDays(year_, month_, day_, true) % 7);
if (v < 0)
v += 7;
return v;
diff --git a/xfa/fgas/localization/fgas_datetime.h b/xfa/fgas/localization/fgas_datetime.h
index 1f9f19d2c1..9225dccaaa 100644
--- a/xfa/fgas/localization/fgas_datetime.h
+++ b/xfa/fgas/localization/fgas_datetime.h
@@ -17,35 +17,79 @@ uint8_t FX_DaysInMonth(int32_t iYear, uint8_t iMonth);
class CFX_Unitime {
public:
- CFX_Unitime() { m_iUnitime = 0; }
- explicit CFX_Unitime(int64_t iUnitime) { m_iUnitime = iUnitime; }
-
- int64_t ToInt64() const { return m_iUnitime; }
+ CFX_Unitime()
+ : year_(0),
+ month_(0),
+ day_(0),
+ hour_(0),
+ minute_(0),
+ second_(0),
+ millisecond_(0) {}
+ CFX_Unitime(int32_t year,
+ uint8_t month,
+ uint8_t day,
+ uint8_t hour,
+ uint8_t minute,
+ uint8_t second,
+ uint16_t millisecond)
+ : year_(year),
+ month_(month),
+ day_(day),
+ hour_(hour),
+ minute_(minute),
+ second_(second),
+ millisecond_(millisecond) {}
void Now();
- void Set(int32_t year,
- uint8_t month,
- uint8_t day,
- uint8_t hour,
- uint8_t minute,
- uint8_t second,
- uint16_t millisecond);
- void Set(int64_t t) { m_iUnitime = t; }
- int32_t GetYear() const;
- uint8_t GetMonth() const;
- uint8_t GetDay() const;
- uint8_t GetHour() const;
- uint8_t GetMinute() const;
- uint8_t GetSecond() const;
- uint16_t GetMillisecond() const;
+ void Reset() {
+ year_ = 0;
+ month_ = 0;
+ day_ = 0;
+ hour_ = 0;
+ minute_ = 0;
+ second_ = 0;
+ millisecond_ = 0;
+ }
+
+ bool IsSet() const {
+ return year_ != 0 || month_ != 0 || day_ != 0 || hour_ != 0 ||
+ minute_ != 0 || second_ != 0 || millisecond_ != 0;
+ }
+
+ void SetDate(int32_t year, uint8_t month, uint8_t day) {
+ year_ = year;
+ month_ = month;
+ day_ = day;
+ }
- CFX_Unitime operator+(const CFX_Unitime& t2) const {
- return CFX_Unitime(m_iUnitime + t2.m_iUnitime);
+ void SetTime(uint8_t hour,
+ uint8_t minute,
+ uint8_t second,
+ uint16_t millisecond) {
+ hour_ = hour;
+ minute_ = minute;
+ second_ = second;
+ millisecond_ = millisecond;
}
+ int32_t GetYear() const { return year_; }
+ uint8_t GetMonth() const { return month_; }
+ uint8_t GetDay() const { return day_; }
+ uint8_t GetHour() const { return hour_; }
+ uint8_t GetMinute() const { return minute_; }
+ uint8_t GetSecond() const { return second_; }
+ uint16_t GetMillisecond() const { return millisecond_; }
+ int32_t GetDayOfWeek() const;
+
private:
- int64_t m_iUnitime;
+ int32_t year_;
+ uint8_t month_;
+ uint8_t day_;
+ uint8_t hour_;
+ uint8_t minute_;
+ uint8_t second_;
+ uint16_t millisecond_;
};
#if _FX_OS_ != _FX_ANDROID_
@@ -59,46 +103,4 @@ struct FX_TIMEZONE {
#pragma pack(pop)
#endif
-class CFX_DateTime {
- public:
- CFX_DateTime() {}
- ~CFX_DateTime() {}
-
- bool Set(int32_t year, uint8_t month, uint8_t day);
- int32_t GetDayOfWeek() const;
-
- private:
-#if _FX_OS_ != _FX_ANDROID_
-#pragma pack(push, 1)
-#endif
- struct FX_DATE {
- int32_t year;
- uint8_t month;
- uint8_t day;
- };
-
- struct FX_TIME {
- uint8_t hour;
- uint8_t minute;
- uint8_t second;
- uint16_t millisecond;
- };
-
- struct FX_DATETIME {
- union {
- FX_DATE sDate;
- FX_DATE aDate;
- } Date;
- union {
- FX_TIME sTime;
- FX_TIME aTime;
- } Time;
- };
-
-#if _FX_OS_ != _FX_ANDROID_
-#pragma pack(pop)
-#endif
- FX_DATETIME m_DateTime;
-};
-
#endif // XFA_FGAS_LOCALIZATION_FGAS_DATETIME_H_
diff --git a/xfa/fgas/localization/fgas_locale.cpp b/xfa/fgas/localization/fgas_locale.cpp
index 253e8512e1..7fddbb4496 100644
--- a/xfa/fgas/localization/fgas_locale.cpp
+++ b/xfa/fgas/localization/fgas_locale.cpp
@@ -205,7 +205,7 @@ bool GetNumericDotIndex(const CFX_WideString& wsNum,
bool ParseLocaleDate(const CFX_WideString& wsDate,
const CFX_WideString& wsDatePattern,
IFX_Locale* pLocale,
- CFX_Unitime& datetime,
+ CFX_Unitime* datetime,
int32_t& cc) {
int32_t year = 1900;
int32_t month = 1;
@@ -393,9 +393,7 @@ bool ParseLocaleDate(const CFX_WideString& wsDate,
if (cc < len)
return false;
- CFX_Unitime ut;
- ut.Set(year, month, day, 0, 0, 0, 0);
- datetime = datetime + ut;
+ datetime->SetDate(year, month, day);
return !!cc;
}
@@ -423,7 +421,7 @@ void ResolveZone(uint8_t& wHour,
bool ParseLocaleTime(const CFX_WideString& wsTime,
const CFX_WideString& wsTimePattern,
IFX_Locale* pLocale,
- CFX_Unitime& datetime,
+ CFX_Unitime* datetime,
int32_t& cc) {
uint8_t hour = 0;
uint8_t minute = 0;
@@ -611,9 +609,7 @@ bool ParseLocaleTime(const CFX_WideString& wsTime,
}
}
}
- CFX_Unitime ut;
- ut.Set(0, 0, 0, hour, minute, second, millisecond);
- datetime = datetime + ut;
+ datetime->SetTime(hour, minute, second, millisecond);
return !!cc;
}
@@ -953,7 +949,7 @@ bool FormatDateTimeInternal(const CFX_Unitime& dt,
} // namespace
-bool FX_DateFromCanonical(const CFX_WideString& wsDate, CFX_Unitime& datetime) {
+bool FX_DateFromCanonical(const CFX_WideString& wsDate, CFX_Unitime* datetime) {
int32_t year = 1900;
int32_t month = 1;
int32_t day = 1;
@@ -1026,14 +1022,12 @@ bool FX_DateFromCanonical(const CFX_WideString& wsDate, CFX_Unitime& datetime) {
}
}
}
- CFX_Unitime ut;
- ut.Set(year, month, day, 0, 0, 0, 0);
- datetime = datetime + ut;
+ datetime->SetDate(year, month, day);
return true;
}
bool FX_TimeFromCanonical(const CFX_WideStringC& wsTime,
- CFX_Unitime& datetime,
+ CFX_Unitime* datetime,
IFX_Locale* pLocale) {
if (wsTime.GetLength() == 0)
return false;
@@ -1099,17 +1093,14 @@ bool FX_TimeFromCanonical(const CFX_WideStringC& wsTime,
FX_TIMEZONE tzDiff;
tzDiff.tzHour = 0;
tzDiff.tzMinute = 0;
- if (str[cc] != 'Z') {
+ if (str[cc] != 'Z')
cc += ParseTimeZone(str + cc, len - cc, tzDiff);
- }
ResolveZone(hour, minute, tzDiff, pLocale);
}
}
}
}
- CFX_Unitime ut;
- ut.Set(0, 0, 0, hour, minute, second, millisecond);
- datetime = datetime + ut;
+ datetime->SetTime(hour, minute, second, millisecond);
return true;
}
@@ -2077,8 +2068,9 @@ FX_DATETIMETYPE CFX_FormatString::GetDateTimeFormat(
bool CFX_FormatString::ParseDateTime(const CFX_WideString& wsSrcDateTime,
const CFX_WideString& wsPattern,
FX_DATETIMETYPE eDateTimeType,
- CFX_Unitime& dtValue) {
- dtValue.Set(0);
+ CFX_Unitime* dtValue) {
+ dtValue->Reset();
+
if (wsSrcDateTime.IsEmpty() || wsPattern.IsEmpty()) {
return false;
}
@@ -2711,15 +2703,16 @@ bool CFX_FormatString::FormatDateTime(const CFX_WideString& wsSrcDateTime,
if (eCategory == FX_DATETIMETYPE_Unknown) {
return false;
}
- CFX_Unitime dt(0);
+ CFX_Unitime dt;
int32_t iT = wsSrcDateTime.Find(L"T");
if (iT < 0) {
if (eCategory == FX_DATETIMETYPE_Date &&
- FX_DateFromCanonical(wsSrcDateTime, dt)) {
+ FX_DateFromCanonical(wsSrcDateTime, &dt)) {
return FormatDateTimeInternal(dt, wsDatePattern, wsTimePattern, true,
pLocale, wsOutput);
- } else if (eCategory == FX_DATETIMETYPE_Time &&
- FX_TimeFromCanonical(wsSrcDateTime.AsStringC(), dt, pLocale)) {
+ }
+ if (eCategory == FX_DATETIMETYPE_Time &&
+ FX_TimeFromCanonical(wsSrcDateTime.AsStringC(), &dt, pLocale)) {
return FormatDateTimeInternal(dt, wsDatePattern, wsTimePattern, true,
pLocale, wsOutput);
}
@@ -2727,11 +2720,11 @@ bool CFX_FormatString::FormatDateTime(const CFX_WideString& wsSrcDateTime,
CFX_WideString wsSrcDate(wsSrcDateTime.c_str(), iT);
CFX_WideStringC wsSrcTime(wsSrcDateTime.c_str() + iT + 1,
wsSrcDateTime.GetLength() - iT - 1);
- if (wsSrcDate.IsEmpty() || wsSrcTime.IsEmpty()) {
+ if (wsSrcDate.IsEmpty() || wsSrcTime.IsEmpty())
return false;
- }
- if (FX_DateFromCanonical(wsSrcDate, dt) &&
- FX_TimeFromCanonical(wsSrcTime, dt, pLocale)) {
+
+ if (FX_DateFromCanonical(wsSrcDate, &dt) &&
+ FX_TimeFromCanonical(wsSrcTime, &dt, pLocale)) {
return FormatDateTimeInternal(dt, wsDatePattern, wsTimePattern,
eCategory != FX_DATETIMETYPE_TimeDate,
pLocale, wsOutput);
diff --git a/xfa/fgas/localization/fgas_locale.h b/xfa/fgas/localization/fgas_locale.h
index ffcbb36b8d..449430b5c2 100644
--- a/xfa/fgas/localization/fgas_locale.h
+++ b/xfa/fgas/localization/fgas_locale.h
@@ -80,9 +80,9 @@ class IFX_Locale {
CFX_WideString& wsPattern) const = 0;
};
-bool FX_DateFromCanonical(const CFX_WideString& wsDate, CFX_Unitime& datetime);
+bool FX_DateFromCanonical(const CFX_WideString& wsDate, CFX_Unitime* datetime);
bool FX_TimeFromCanonical(const CFX_WideStringC& wsTime,
- CFX_Unitime& datetime,
+ CFX_Unitime* datetime,
IFX_Locale* pLocale);
class CFX_Decimal {
public: