Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: fpdfsdk/src/javascript/public_methods_embeddertest.cpp

Issue 1533233002: Fix JS seconds since epoch to date conversions. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: more test cases Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <cmath>
6
7 #include "core/include/fxcrt/fx_string.h"
8 #include "fpdfsdk/src/javascript/PublicMethods.h"
9 #include "testing/js_embedder_test.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 double RoundDownDate(double date) {
15 return date - fmod(date, 86400000);
16 }
17
18 } // namespace
19
20 class PublicMethodsEmbedderTest : public JSEmbedderTest {};
21
22 TEST_F(PublicMethodsEmbedderTest, MakeRegularDate) {
23 v8::Isolate::Scope isolate_scope(isolate());
24 v8::HandleScope handle_scope(isolate());
25 v8::Context::Scope context_scope(GetV8Context());
26 FX_BOOL bWrongFormat;
27 double date;
28
29 // 1968
30 bWrongFormat = false;
31 date = CJS_PublicMethods::MakeRegularDate(L"06/25/1968", L"mm/dd/yyyy",
32 bWrongFormat);
33 date = RoundDownDate(date);
34 EXPECT_DOUBLE_EQ(-47865600000, date);
35 EXPECT_FALSE(bWrongFormat);
36
37 // 1968
38 bWrongFormat = false;
39 date = CJS_PublicMethods::MakeRegularDate(L"25061968", L"ddmmyyyy",
40 bWrongFormat);
41 date = RoundDownDate(date);
42 EXPECT_DOUBLE_EQ(-47865600000, date);
43 EXPECT_FALSE(bWrongFormat);
44
45 // 1968
46 bWrongFormat = false;
47 date = CJS_PublicMethods::MakeRegularDate(L"19680625", L"yyyymmdd",
48 bWrongFormat);
49 date = RoundDownDate(date);
50 EXPECT_DOUBLE_EQ(-47865600000, date);
51 EXPECT_FALSE(bWrongFormat);
52
53 // 1985
54 bWrongFormat = false;
55 date = CJS_PublicMethods::MakeRegularDate(L"31121985", L"ddmmyyyy",
56 bWrongFormat);
57 date = RoundDownDate(date);
58 EXPECT_DOUBLE_EQ(504835200000.0, date);
59 EXPECT_FALSE(bWrongFormat);
60
61 // 2085, the other '85.
62 bWrongFormat = false;
63 date = CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", bWrongFormat);
64 date = RoundDownDate(date);
65 EXPECT_DOUBLE_EQ(3660595200000.0, date);
66 EXPECT_FALSE(bWrongFormat);
67
68 // 1995
69 bWrongFormat = false;
70 date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy",
71 bWrongFormat);
72 date = RoundDownDate(date);
73 EXPECT_DOUBLE_EQ(791596800000.0, date);
74 EXPECT_FALSE(bWrongFormat);
75
76 // 2095, the other '95.
77 bWrongFormat = false;
78 date = CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", bWrongFormat);
79 date = RoundDownDate(date);
80 EXPECT_DOUBLE_EQ(3947356800000.0, date);
81 EXPECT_FALSE(bWrongFormat);
82
83 // 2005
84 bWrongFormat = false;
85 date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy",
86 bWrongFormat);
87 date = RoundDownDate(date);
88 EXPECT_DOUBLE_EQ(1107216000000.0, date);
89 EXPECT_FALSE(bWrongFormat);
90
91 // 2005
92 bWrongFormat = false;
93 date = CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", bWrongFormat);
94 date = RoundDownDate(date);
95 EXPECT_DOUBLE_EQ(1107216000000.0, date);
96 EXPECT_FALSE(bWrongFormat);
97 }
98
99 TEST_F(PublicMethodsEmbedderTest, MakeFormatDate) {
100 v8::Isolate::Scope isolate_scope(isolate());
101 v8::HandleScope handle_scope(isolate());
102 v8::Context::Scope context_scope(GetV8Context());
103 CFX_WideString formatted_date;
104
105 // 1968-06-25
106 formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy");
107 EXPECT_STREQ(L"250668", formatted_date);
108 formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd");
109 EXPECT_STREQ(L"68/06/25", formatted_date);
110
111 // 1985-12-31
112 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy");
113 EXPECT_STREQ(L"311285", formatted_date);
114 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd");
115 EXPECT_STREQ(L"851231", formatted_date);
116
117 // 1995-02-01
118 formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy");
119 EXPECT_STREQ(L"010295", formatted_date);
120 formatted_date =
121 CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd");
122 EXPECT_STREQ(L"19950201", formatted_date);
123
124 // 2005-02-01
125 formatted_date =
126 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy");
127 EXPECT_STREQ(L"010205", formatted_date);
128 formatted_date =
129 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm");
130 EXPECT_STREQ(L"20050102", formatted_date);
131
132 // 2085-12-31
133 formatted_date =
134 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy");
135 EXPECT_STREQ(L"311285", formatted_date);
136 formatted_date =
137 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd");
138 EXPECT_STREQ(L"208531", formatted_date);
139
140 // 2095-02-01
141 formatted_date =
142 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy");
143 EXPECT_STREQ(L"010295", formatted_date);
144 formatted_date =
145 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy");
146 EXPECT_STREQ(L"02012095", formatted_date);
147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698