1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/ecma_vm.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_date.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/tests/test_helper.h"
22
23 using namespace panda::ecmascript;
24
25 namespace panda::test {
26 class JSDateTest : public testing::Test {
27 public:
SetUpTestCase()28 static void SetUpTestCase()
29 {
30 GTEST_LOG_(INFO) << "SetUpTestCase";
31 }
32
TearDownTestCase()33 static void TearDownTestCase()
34 {
35 GTEST_LOG_(INFO) << "TearDownCase";
36 }
37
SetUp()38 void SetUp() override
39 {
40 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41 }
42
TearDown()43 void TearDown() override
44 {
45 TestHelper::DestroyEcmaVMWithScope(instance, scope);
46 }
47
48 EcmaVM *instance {nullptr};
49 ecmascript::EcmaHandleScope *scope {nullptr};
50 JSThread *thread {nullptr};
51 };
52
JSDateCreate(JSThread * thread)53 JSDate *JSDateCreate(JSThread *thread)
54 {
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 EcmaVM *ecmaVM = thread->GetEcmaVM();
57 auto globalEnv = ecmaVM->GetGlobalEnv();
58 JSHandle<JSTaggedValue> dateFunction = globalEnv->GetDateFunction();
59 JSHandle<JSDate> dateObject =
60 JSHandle<JSDate>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(dateFunction), dateFunction));
61 return *dateObject;
62 }
63
HWTEST_F_L0(JSDateTest,Create)64 HWTEST_F_L0(JSDateTest, Create)
65 {
66 double tm = 0.0;
67 JSHandle<JSDate> jsDate(thread, JSDateCreate(thread));
68 EXPECT_EQ(jsDate->GetTimeValue(), JSTaggedValue(tm));
69 EXPECT_EQ(jsDate->GetLocalOffset(), JSTaggedValue(JSDate::MAX_DOUBLE));
70 tm = 28 * 60 * 60 * 1000;
71 jsDate->SetTimeValue(thread, JSTaggedValue(tm));
72
73 [[maybe_unused]] double temp = jsDate->GetTimeValue().GetDouble();
74 EXPECT_EQ(jsDate->GetTimeValue(), JSTaggedValue(tm));
75 }
76
HWTEST_F_L0(JSDateTest,MakeTime)77 HWTEST_F_L0(JSDateTest, MakeTime)
78 {
79 double const day1 = ecmascript::JSDate::MakeDay(0, 11, 31);
80 double const time1 = ecmascript::JSDate::MakeTime(0, 0, 0, 0);
81 double ms1 = ecmascript::JSDate::TimeClip(ecmascript::JSDate::MakeDate(day1, time1));
82 EXPECT_EQ(ms1, -62135683200000.0);
83
84 double const day = ecmascript::JSDate::MakeDay(-1, 11, 31);
85 double const time = ecmascript::JSDate::MakeTime(0, 0, 0, 0);
86 double ms = ecmascript::JSDate::TimeClip(ecmascript::JSDate::MakeDate(day, time));
87 EXPECT_EQ(ms, -62167305600000.0);
88 }
89
HWTEST_F_L0(JSDateTest,GetTimeFromString)90 HWTEST_F_L0(JSDateTest, GetTimeFromString)
91 {
92 // test ISO 8601
93 CString str = "2020-11-19T12:18:18.132Z";
94 JSTaggedValue ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
95 EXPECT_EQ(ms.GetDouble(), 1605788298132.0);
96
97 str = "1880-12-30T23:59:59";
98 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
99 EXPECT_EQ(ms.GetDouble(), -2808633901000.0);
100
101 str = "2020-11-19Z";
102 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
103 EXPECT_EQ(ms.GetDouble(), 1605744000000.0);
104
105 str = "2020-11";
106 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
107 EXPECT_EQ(ms.GetDouble(), 1604188800000.0);
108
109 str = "2023-1-11";
110 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
111 EXPECT_EQ(ms.GetDouble(), 1673366400000.0);
112
113 str = "+275760-09-13T00:00:00.000Z";
114 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
115 EXPECT_EQ(ms.GetDouble(), 8640000000000000.0);
116
117 str = "-271821-04-20T00:00:00.000Z";
118 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
119 EXPECT_EQ(ms.GetDouble(), -8640000000000000.0);
120
121 str = "2020T12:18Z";
122 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
123 EXPECT_EQ(ms.GetDouble(), 1577881080000.0);
124
125 str = "2020T12:18:17.231Z";
126 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
127 EXPECT_EQ(ms.GetDouble(), 1577881097231.0);
128
129 str = "2020-11T12:18:17.231Z";
130 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
131 EXPECT_EQ(ms.GetDouble(), 1604233097231.0);
132
133 str = "1645-11T12:18:17.231+08:00";
134 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
135 EXPECT_EQ(ms.GetDouble(), -10229658102769.0);
136
137 str = "2020-11-19T12:18-08:12";
138 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
139 EXPECT_EQ(ms.GetDouble(), 1605817800000.0);
140
141 // test Local time
142 str = "Thu Nov 19 2020 20:18:18 GMT+0800";
143 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
144 EXPECT_EQ(ms.GetDouble(), 1605788298000.0);
145
146 str = "Thu Nov 19 2020 20:18 GMT-0800";
147 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
148 EXPECT_EQ(ms.GetDouble(), 1605845880000.0);
149
150 str = "Thu Nov 03 2093 04:18 GMT";
151 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
152 EXPECT_EQ(ms.GetDouble(), 3908060280000.0);
153
154 str = "Thu Nov 19 1820 GMT+1232";
155 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
156 EXPECT_EQ(ms.GetDouble(), -4705734720000.0);
157
158 // test UTC time
159 str = "Thu, 19 Nov 2020 20:18:18 GMT+0800";
160 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
161 EXPECT_EQ(ms.GetDouble(), 1605788298000.0);
162
163 str = "Thu, 19 Nov 2020 20:18 GMT-0800";
164 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
165 EXPECT_EQ(ms.GetDouble(), 1605845880000.0);
166
167 str = "Thu 03 Jun 2093 04:18 GMT";
168 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
169 EXPECT_EQ(ms.GetDouble(), 3894841080000.0);
170
171 str = "Thu 19 Nov 1820 GMT+1232";
172 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
173 EXPECT_EQ(ms.GetDouble(), -4705734720000.0);
174
175 // test YYYY-MM-DD HH:MM:ss
176 str = "2020-11-19 12:18:18.132Z";
177 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
178 EXPECT_EQ(ms.GetDouble(), 1605788298132.0);
179
180 str = "2020-11-19 12:18:18.132+02:21";
181 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
182 EXPECT_EQ(ms.GetDouble(), 1605779838132.0);
183
184 str = "2020-11-19 12:18:18.132-02:21";
185 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
186 EXPECT_EQ(ms.GetDouble(), 1605796758132.0);
187
188 str = "+175760-09-13 03:16:02.003Z";
189 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
190 EXPECT_EQ(ms.GetDouble(), 5484304811762003.0);
191
192 str = "-171821-04-20 13:08:23.037Z";
193 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
194 EXPECT_EQ(ms.GetDouble(), -5484304752696963.0);
195
196 // test Month DD,YYYY HH:MM:SS
197 str = "January 12,2006 22:19:35";
198 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
199 EXPECT_EQ(ms.GetDouble(), 1137075575000.0);
200
201 str = "January 12,2006";
202 ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
203 EXPECT_EQ(ms.GetDouble(), 1136995200000.0);
204 }
205 } // namespace panda::test
206