diff options
-rw-r--r-- | fpdfsdk/javascript/JS_Value.cpp | 73 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_Value.h | 19 | ||||
-rw-r--r-- | fpdfsdk/javascript/util.cpp | 61 |
3 files changed, 33 insertions, 120 deletions
diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index 4ef194b8f2..1ef5599e9e 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -19,12 +19,6 @@ namespace { -double -MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) { - return JS_MakeDate(JS_MakeDay(year, mon, day), - JS_MakeTime(hour, min, sec, ms)); -} - double GetLocalTZA() { if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) return 0; @@ -172,10 +166,6 @@ int DateFromTime(double t) { } } -double JS_LocalTime(double d) { - return d + GetLocalTZA() + GetDaylightSavingTA(d); -} - } // namespace CJS_Return::CJS_Return(bool result) : is_error_(!result) {} @@ -219,71 +209,12 @@ int CJS_Array::GetLength(CJS_Runtime* pRuntime) const { return pRuntime->GetArrayLength(m_pArray); } -CJS_Date::CJS_Date() {} - CJS_Date::CJS_Date(v8::Local<v8::Date> pDate) : m_pDate(pDate) {} -CJS_Date::CJS_Date(CJS_Runtime* pRuntime, double dMsecTime) - : m_pDate(pRuntime->NewDate(dMsecTime)) {} - -CJS_Date::CJS_Date(CJS_Runtime* pRuntime, - int year, - int mon, - int day, - int hour, - int min, - int sec) - : m_pDate(pRuntime->NewDate(MakeDate(year, mon, day, hour, min, sec, 0))) {} - CJS_Date::CJS_Date(const CJS_Date& other) = default; CJS_Date::~CJS_Date() {} -bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const { - return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate)); -} - -int CJS_Date::GetYear(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - - return JS_GetYearFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - -int CJS_Date::GetMonth(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - return JS_GetMonthFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - -int CJS_Date::GetDay(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - - return JS_GetDayFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - -int CJS_Date::GetHours(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - - return JS_GetHourFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - -int CJS_Date::GetMinutes(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - - return JS_GetMinFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - -int CJS_Date::GetSeconds(CJS_Runtime* pRuntime) const { - if (!IsValidDate(pRuntime)) - return 0; - - return JS_GetSecFromTime(JS_LocalTime(pRuntime->ToDouble(m_pDate))); -} - double JS_GetDateTime() { if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) return 0; @@ -321,6 +252,10 @@ int JS_GetSecFromTime(double dt) { return (int)Mod(floor(dt / 1000), 60); } +double JS_LocalTime(double d) { + return d + GetLocalTZA() + GetDaylightSavingTA(d); +} + double JS_DateParse(const WideString& str) { v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); v8::Isolate::Scope isolate_scope(pIsolate); diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h index e3947eb66b..6136119e51 100644 --- a/fpdfsdk/javascript/JS_Value.h +++ b/fpdfsdk/javascript/JS_Value.h @@ -62,28 +62,10 @@ class CJS_Array { class CJS_Date { public: - CJS_Date(); explicit CJS_Date(v8::Local<v8::Date> pDate); - CJS_Date(CJS_Runtime* pRuntime, double dMsec_time); - CJS_Date(CJS_Runtime* pRuntime, - int year, - int mon, - int day, - int hour, - int min, - int sec); CJS_Date(const CJS_Date&); virtual ~CJS_Date(); - bool IsValidDate(CJS_Runtime* pRuntime) const; - - int GetYear(CJS_Runtime* pRuntime) const; - int GetMonth(CJS_Runtime* pRuntime) const; - int GetDay(CJS_Runtime* pRuntime) const; - int GetHours(CJS_Runtime* pRuntime) const; - int GetMinutes(CJS_Runtime* pRuntime) const; - int GetSeconds(CJS_Runtime* pRuntime) const; - v8::Local<v8::Value> ToV8Value() const { return m_pDate; } private: @@ -97,6 +79,7 @@ int JS_GetDayFromTime(double dt); int JS_GetHourFromTime(double dt); int JS_GetMinFromTime(double dt); int JS_GetSecFromTime(double dt); +double JS_LocalTime(double d); double JS_DateParse(const WideString& str); double JS_MakeDay(int nYear, int nMonth, int nDay); double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp index 3accaa265c..26e44496da 100644 --- a/fpdfsdk/javascript/util.cpp +++ b/fpdfsdk/javascript/util.cpp @@ -144,31 +144,33 @@ CJS_Return util::printd(CJS_Runtime* pRuntime, return CJS_Return(JSGetStringFromID(IDS_STRING_JSPRINT1)); CJS_Date jsDate(params[1].As<v8::Date>()); - if (!jsDate.IsValidDate(pRuntime)) + if (jsDate.ToV8Value().IsEmpty() || + std::isnan(pRuntime->ToDouble(jsDate.ToV8Value()))) { return CJS_Return(JSGetStringFromID(IDS_STRING_JSPRINT2)); + } + + double date = JS_LocalTime(pRuntime->ToDouble(jsDate.ToV8Value())); + int year = JS_GetYearFromTime(date); + int month = JS_GetMonthFromTime(date) + 1; // One-based. + int day = JS_GetDayFromTime(date); + int hour = JS_GetHourFromTime(date); + int min = JS_GetMinFromTime(date); + int sec = JS_GetSecFromTime(date); if (params[0]->IsNumber()) { WideString swResult; switch (pRuntime->ToInt32(params[0])) { case 0: - swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", jsDate.GetYear(pRuntime), - jsDate.GetMonth(pRuntime) + 1, jsDate.GetDay(pRuntime), - jsDate.GetHours(pRuntime), jsDate.GetMinutes(pRuntime), - jsDate.GetSeconds(pRuntime)); + swResult.Format(L"D:%04d%02d%02d%02d%02d%02d", year, month, day, hour, + min, sec); break; case 1: - swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", - jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, - jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), - jsDate.GetMinutes(pRuntime), - jsDate.GetSeconds(pRuntime)); + swResult.Format(L"%04d.%02d.%02d %02d:%02d:%02d", year, month, day, + hour, min, sec); break; case 2: - swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", - jsDate.GetYear(pRuntime), jsDate.GetMonth(pRuntime) + 1, - jsDate.GetDay(pRuntime), jsDate.GetHours(pRuntime), - jsDate.GetMinutes(pRuntime), - jsDate.GetSeconds(pRuntime)); + swResult.Format(L"%04d/%02d/%02d %02d:%02d:%02d", year, month, day, + hour, min, sec); break; default: return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); @@ -200,20 +202,13 @@ CJS_Return util::printd(CJS_Runtime* pRuntime, } } - int iYear = jsDate.GetYear(pRuntime); - if (iYear < 0) + if (year < 0) return CJS_Return(JSGetStringFromID(IDS_STRING_JSVALUEERROR)); - int iMonth = jsDate.GetMonth(pRuntime); - int iDay = jsDate.GetDay(pRuntime); - int iHour = jsDate.GetHours(pRuntime); - int iMin = jsDate.GetMinutes(pRuntime); - int iSec = jsDate.GetSeconds(pRuntime); - static const TbConvertAdditional cTableAd[] = { - {L"m", iMonth + 1}, {L"d", iDay}, - {L"H", iHour}, {L"h", iHour > 12 ? iHour - 12 : iHour}, - {L"M", iMin}, {L"s", iSec}, + {L"m", month}, {L"d", day}, + {L"H", hour}, {L"h", hour > 12 ? hour - 12 : hour}, + {L"M", min}, {L"s", sec}, }; for (size_t i = 0; i < FX_ArraySize(cTableAd); ++i) { @@ -235,12 +230,12 @@ CJS_Return util::printd(CJS_Runtime* pRuntime, } struct tm time = {}; - time.tm_year = iYear - 1900; - time.tm_mon = iMonth; - time.tm_mday = iDay; - time.tm_hour = iHour; - time.tm_min = iMin; - time.tm_sec = iSec; + time.tm_year = year - 1900; + time.tm_mon = month - 1; + time.tm_mday = day; + time.tm_hour = hour; + time.tm_min = min; + time.tm_sec = sec; wchar_t buf[64] = {}; FXSYS_wcsftime(buf, 64, cFormat.c_str(), &time); @@ -373,7 +368,7 @@ CJS_Return util::scand(CJS_Runtime* pRuntime, if (std::isnan(dDate)) return CJS_Return(pRuntime->NewUndefined()); - return CJS_Return(CJS_Date(pRuntime, dDate).ToV8Value()); + return CJS_Return(pRuntime->NewDate(dDate)); } CJS_Return util::byteToChar(CJS_Runtime* pRuntime, |