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

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
« no previous file with comments | « fpdfsdk/src/javascript/JS_Value.cpp ('k') | fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // 1969-12-31
112 formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy");
113 EXPECT_STREQ(L"311269", formatted_date);
114 formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd");
115 EXPECT_STREQ(L"69!1231", formatted_date);
116
117 // 1970-01-01
118 formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy");
119 EXPECT_STREQ(L"010170", formatted_date);
120 formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd");
121 EXPECT_STREQ(L"01-1970-01", formatted_date);
122
123 // 1985-12-31
124 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy");
125 EXPECT_STREQ(L"311285", formatted_date);
126 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd");
127 EXPECT_STREQ(L"851231", formatted_date);
128
129 // 1995-02-01
130 formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy");
131 EXPECT_STREQ(L"010295", formatted_date);
132 formatted_date =
133 CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd");
134 EXPECT_STREQ(L"19950201", formatted_date);
135
136 // 2005-02-01
137 formatted_date =
138 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy");
139 EXPECT_STREQ(L"010205", formatted_date);
140 formatted_date =
141 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm");
142 EXPECT_STREQ(L"20050102", formatted_date);
143
144 // 2085-12-31
145 formatted_date =
146 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy");
147 EXPECT_STREQ(L"311285", formatted_date);
148 formatted_date =
149 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd");
150 EXPECT_STREQ(L"208531", formatted_date);
151
152 // 2095-02-01
153 formatted_date =
154 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy");
155 EXPECT_STREQ(L"010295", formatted_date);
156 formatted_date =
157 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy");
158 EXPECT_STREQ(L"02012095", formatted_date);
159 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/JS_Value.cpp ('k') | fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698