Merge to XFA: Fix JS seconds since epoch to date conversions.

BUG=515137,564736
TBR=ochang@chromium.org, tsepez@chromium.org

Review URL: https://codereview.chromium.org/1533233002 .
Review URL: https://codereview.chromium.org/1544493003 .

(cherry picked from commit b426e3edde040089b70d1a223c83b90957aa571d)
(cherry picked from commit bd35d484a4027775f19ff93e9143e0b270133d42)

Review URL: https://codereview.chromium.org/1546443002 .
diff --git a/BUILD.gn b/BUILD.gn
index 81d874b..8047660 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1517,7 +1517,12 @@
   ]
   include_dirs = []
   if (pdf_enable_v8) {
-    sources += [ "fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp" ]
+    sources += [
+      "fpdfsdk/src/javascript/public_methods_embeddertest.cpp",
+      "fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp",
+      "testing/js_embedder_test.cpp",
+      "testing/js_embedder_test.h",
+    ]
     deps += [
       "//v8",
       "//v8:v8_libplatform",
diff --git a/fpdfsdk/src/javascript/JS_Value.cpp b/fpdfsdk/src/javascript/JS_Value.cpp
index ea7bfda..bd00adc 100644
--- a/fpdfsdk/src/javascript/JS_Value.cpp
+++ b/fpdfsdk/src/javascript/JS_Value.cpp
@@ -649,7 +649,7 @@
 }
 
 double _TimeFromYear(int y) {
-  return ((double)86400000) * _DayFromYear(y);
+  return 86400000.0 * _DayFromYear(y);
 }
 
 double _TimeFromYearMonth(int y, int m) {
@@ -669,12 +669,12 @@
 
 int _YearFromTime(double t) {
   // estimate the time.
-  int y = 1970 + (int)(t / (365.0 * 86400000));
+  int y = 1970 + static_cast<int>(t / (365.2425 * 86400000));
   if (_TimeFromYear(y) <= t) {
     while (_TimeFromYear(y + 1) <= t)
       y++;
   } else
-    while (_TimeFromYear(y - 1) > t)
+    while (_TimeFromYear(y) > t)
       y--;
   return y;
 }
diff --git a/fpdfsdk/src/javascript/public_methods_embeddertest.cpp b/fpdfsdk/src/javascript/public_methods_embeddertest.cpp
new file mode 100644
index 0000000..eac5264
--- /dev/null
+++ b/fpdfsdk/src/javascript/public_methods_embeddertest.cpp
@@ -0,0 +1,165 @@
+// Copyright 2015 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.
+
+#include <cmath>
+
+#include "core/include/fxcrt/fx_string.h"
+#include "fpdfsdk/src/javascript/PublicMethods.h"
+#include "testing/js_embedder_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+double RoundDownDate(double date) {
+  return date - fmod(date, 86400000);
+}
+
+}  // namespace
+
+class PublicMethodsEmbedderTest : public JSEmbedderTest {};
+
+TEST_F(PublicMethodsEmbedderTest, MakeRegularDate) {
+  v8::Isolate::Scope isolate_scope(isolate());
+#ifdef PDF_ENABLE_XFA
+  v8::Locker locker(isolate());
+#endif  // PDF_ENABLE_XFA
+  v8::HandleScope handle_scope(isolate());
+  v8::Context::Scope context_scope(GetV8Context());
+  FX_BOOL bWrongFormat;
+  double date;
+
+  // 1968
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"06/25/1968", L"mm/dd/yyyy",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(-47865600000, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 1968
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"25061968", L"ddmmyyyy",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(-47865600000, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 1968
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"19680625", L"yyyymmdd",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(-47865600000, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 1985
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"31121985", L"ddmmyyyy",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(504835200000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 2085, the other '85.
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(3660595200000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 1995
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(791596800000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 2095, the other '95.
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(3947356800000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 2005
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy",
+                                            bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(1107216000000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+
+  // 2005
+  bWrongFormat = false;
+  date = CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", bWrongFormat);
+  date = RoundDownDate(date);
+  EXPECT_DOUBLE_EQ(1107216000000.0, date);
+  EXPECT_FALSE(bWrongFormat);
+}
+
+TEST_F(PublicMethodsEmbedderTest, MakeFormatDate) {
+  v8::Isolate::Scope isolate_scope(isolate());
+#ifdef PDF_ENABLE_XFA
+  v8::Locker locker(isolate());
+#endif  // PDF_ENABLE_XFA
+  v8::HandleScope handle_scope(isolate());
+  v8::Context::Scope context_scope(GetV8Context());
+  CFX_WideString formatted_date;
+
+  // 1968-06-25
+  formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy");
+  EXPECT_STREQ(L"250668", formatted_date);
+  formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd");
+  EXPECT_STREQ(L"68/06/25", formatted_date);
+
+  // 1969-12-31
+  formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy");
+  EXPECT_STREQ(L"311269", formatted_date);
+  formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd");
+  EXPECT_STREQ(L"69!1231", formatted_date);
+
+  // 1970-01-01
+  formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy");
+  EXPECT_STREQ(L"010170", formatted_date);
+  formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd");
+  EXPECT_STREQ(L"01-1970-01", formatted_date);
+
+  // 1985-12-31
+  formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy");
+  EXPECT_STREQ(L"311285", formatted_date);
+  formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd");
+  EXPECT_STREQ(L"851231", formatted_date);
+
+  // 1995-02-01
+  formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy");
+  EXPECT_STREQ(L"010295", formatted_date);
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd");
+  EXPECT_STREQ(L"19950201", formatted_date);
+
+  // 2005-02-01
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy");
+  EXPECT_STREQ(L"010205", formatted_date);
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm");
+  EXPECT_STREQ(L"20050102", formatted_date);
+
+  // 2085-12-31
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy");
+  EXPECT_STREQ(L"311285", formatted_date);
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd");
+  EXPECT_STREQ(L"208531", formatted_date);
+
+  // 2095-02-01
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy");
+  EXPECT_STREQ(L"010295", formatted_date);
+  formatted_date =
+      CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy");
+  EXPECT_STREQ(L"02012095", formatted_date);
+}
diff --git a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp
index 8ff1339..8fce27b 100644
--- a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp
+++ b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp
@@ -2,11 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "core/include/fpdfapi/fpdf_parser.h"
-#include "fpdfsdk/include/jsapi/fxjs_v8.h"
-#include "testing/embedder_test.h"
+#include "testing/js_embedder_test.h"
 #include "testing/gtest/include/gtest/gtest.h"
-#include "third_party/base/nonstd_unique_ptr.h"
 
 namespace {
 
@@ -14,51 +11,9 @@
 
 }  // namespace
 
-class FXJSV8Embeddertest : public EmbedderTest {
- public:
-  FXJSV8Embeddertest()
-      : m_pArrayBufferAllocator(new FXJS_ArrayBufferAllocator) {
-    v8::Isolate::CreateParams params;
-    params.array_buffer_allocator = m_pArrayBufferAllocator.get();
-    m_pIsolate = v8::Isolate::New(params);
-  }
+class FXJSV8EmbedderTest : public JSEmbedderTest {};
 
-  ~FXJSV8Embeddertest() override { m_pIsolate->Dispose(); }
-
-  void SetUp() override {
-    EmbedderTest::SetExternalIsolate(m_pIsolate);
-    EmbedderTest::SetUp();
-
-    v8::Isolate::Scope isolate_scope(m_pIsolate);
-#ifdef PDF_ENABLE_XFA
-    v8::Locker locker(m_pIsolate);
-#endif  // PDF_ENABLE_XFA
-    v8::HandleScope handle_scope(m_pIsolate);
-    FXJS_PerIsolateData::SetUp(m_pIsolate);
-    FXJS_InitializeRuntime(m_pIsolate, nullptr, &m_pPersistentContext,
-                           &m_StaticObjects);
-  }
-
-  void TearDown() override {
-    FXJS_ReleaseRuntime(m_pIsolate, &m_pPersistentContext, &m_StaticObjects);
-    m_pPersistentContext.Reset();
-    FXJS_Release();
-    EmbedderTest::TearDown();
-  }
-
-  v8::Isolate* isolate() { return m_pIsolate; }
-  v8::Local<v8::Context> GetV8Context() {
-    return m_pPersistentContext.Get(m_pIsolate);
-  }
-
- private:
-  nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
-  v8::Isolate* m_pIsolate;
-  v8::Global<v8::Context> m_pPersistentContext;
-  std::vector<v8::Global<v8::Object>*> m_StaticObjects;
-};
-
-TEST_F(FXJSV8Embeddertest, Getters) {
+TEST_F(FXJSV8EmbedderTest, Getters) {
   v8::Isolate::Scope isolate_scope(isolate());
 #ifdef PDF_ENABLE_XFA
   v8::Locker locker(isolate());
diff --git a/pdfium.gyp b/pdfium.gyp
index ed24910..6dfd42f 100644
--- a/pdfium.gyp
+++ b/pdfium.gyp
@@ -825,7 +825,10 @@
             '<(DEPTH)/v8/tools/gyp/v8.gyp:v8_libplatform',
           ],
           'sources': [
+            'fpdfsdk/src/javascript/public_methods_embeddertest.cpp',
             'fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp',
+            'testing/js_embedder_test.cpp',
+            'testing/js_embedder_test.h',
           ],
         }],
       ],
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index d3ac09b..23b15a4 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 PDFium Authors. All rights reserved.
+// Copyright 2015 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.
 
diff --git a/testing/embedder_test.h b/testing/embedder_test.h
index f68cb9d..df2e40a 100644
--- a/testing/embedder_test.h
+++ b/testing/embedder_test.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 PDFium Authors. All rights reserved.
+// Copyright 2015 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.
 
diff --git a/testing/js_embedder_test.cpp b/testing/js_embedder_test.cpp
new file mode 100644
index 0000000..ffa647d
--- /dev/null
+++ b/testing/js_embedder_test.cpp
@@ -0,0 +1,45 @@
+// Copyright 2015 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.
+
+#include "testing/js_embedder_test.h"
+
+JSEmbedderTest::JSEmbedderTest()
+    : m_pArrayBufferAllocator(new FXJS_ArrayBufferAllocator) {
+  v8::Isolate::CreateParams params;
+  params.array_buffer_allocator = m_pArrayBufferAllocator.get();
+  m_pIsolate = v8::Isolate::New(params);
+}
+
+JSEmbedderTest::~JSEmbedderTest() {
+  m_pIsolate->Dispose();
+}
+
+void JSEmbedderTest::SetUp() {
+  EmbedderTest::SetExternalIsolate(m_pIsolate);
+  EmbedderTest::SetUp();
+
+  v8::Isolate::Scope isolate_scope(m_pIsolate);
+#ifdef PDF_ENABLE_XFA
+  v8::Locker locker(m_pIsolate);
+#endif  // PDF_ENABLE_XFA
+  v8::HandleScope handle_scope(m_pIsolate);
+  FXJS_PerIsolateData::SetUp(m_pIsolate);
+  FXJS_InitializeRuntime(m_pIsolate, nullptr, &m_pPersistentContext,
+                         &m_StaticObjects);
+}
+
+void JSEmbedderTest::TearDown() {
+  FXJS_ReleaseRuntime(m_pIsolate, &m_pPersistentContext, &m_StaticObjects);
+  m_pPersistentContext.Reset();
+  FXJS_Release();
+  EmbedderTest::TearDown();
+}
+
+v8::Isolate* JSEmbedderTest::isolate() {
+  return m_pIsolate;
+}
+
+v8::Local<v8::Context> JSEmbedderTest::GetV8Context() {
+  return m_pPersistentContext.Get(m_pIsolate);
+}
diff --git a/testing/js_embedder_test.h b/testing/js_embedder_test.h
new file mode 100644
index 0000000..167aea7
--- /dev/null
+++ b/testing/js_embedder_test.h
@@ -0,0 +1,32 @@
+// Copyright 2015 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.
+
+#ifndef TESTING_JS_EMBEDDER_TEST_H_
+#define TESTING_JS_EMBEDDER_TEST_H_
+
+#include <vector>
+
+#include "fpdfsdk/include/jsapi/fxjs_v8.h"
+#include "testing/embedder_test.h"
+#include "third_party/base/nonstd_unique_ptr.h"
+
+class JSEmbedderTest : public EmbedderTest {
+ public:
+  JSEmbedderTest();
+  ~JSEmbedderTest() override;
+
+  void SetUp() override;
+  void TearDown() override;
+
+  v8::Isolate* isolate();
+  v8::Local<v8::Context> GetV8Context();
+
+ private:
+  nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
+  v8::Isolate* m_pIsolate;
+  v8::Global<v8::Context> m_pPersistentContext;
+  std::vector<v8::Global<v8::Object>*> m_StaticObjects;
+};
+
+#endif  // TESTING_JS_EMBEDDER_TEST_H_