• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BaseTestWithScope<false> {
27 };
28 
JSDateCreate(JSThread * thread)29 JSDate *JSDateCreate(JSThread *thread)
30 {
31     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
32     EcmaVM *ecmaVM = thread->GetEcmaVM();
33     auto globalEnv = ecmaVM->GetGlobalEnv();
34     JSHandle<JSTaggedValue> dateFunction = globalEnv->GetDateFunction();
35     JSHandle<JSDate> dateObject =
36         JSHandle<JSDate>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(dateFunction), dateFunction));
37     return *dateObject;
38 }
39 
HWTEST_F_L0(JSDateTest,Create)40 HWTEST_F_L0(JSDateTest, Create)
41 {
42     double tm = 0.0;
43     JSHandle<JSDate> jsDate(thread, JSDateCreate(thread));
44     EXPECT_EQ(jsDate->GetTimeValue(thread), JSTaggedValue(tm));
45     EXPECT_EQ(jsDate->GetLocalOffset(thread), JSTaggedValue(JSDate::MAX_DOUBLE));
46     tm = 28 * 60 * 60 * 1000;
47     jsDate->SetTimeValue(thread, JSTaggedValue(tm));
48 
49     [[maybe_unused]] double temp = jsDate->GetTimeValue(thread).GetDouble();
50     EXPECT_EQ(jsDate->GetTimeValue(thread), JSTaggedValue(tm));
51 }
52 
HWTEST_F_L0(JSDateTest,MakeTime)53 HWTEST_F_L0(JSDateTest, MakeTime)
54 {
55     double const day1 = ecmascript::JSDate::MakeDay(0, 11, 31);
56     double const time1 = ecmascript::JSDate::MakeTime(0, 0, 0, 0);
57     double ms1 = ecmascript::JSDate::TimeClip(ecmascript::JSDate::MakeDate(day1, time1));
58     EXPECT_EQ(ms1, -62135683200000.0);
59 
60     double const day = ecmascript::JSDate::MakeDay(-1, 11, 31);
61     double const time = ecmascript::JSDate::MakeTime(0, 0, 0, 0);
62     double ms = ecmascript::JSDate::TimeClip(ecmascript::JSDate::MakeDate(day, time));
63     EXPECT_EQ(ms, -62167305600000.0);
64 }
65 
HWTEST_F_L0(JSDateTest,GetTimeFromString)66 HWTEST_F_L0(JSDateTest, GetTimeFromString)
67 {
68     // test ISO 8601
69     CString str = "2020-11-19T12:18:18.132Z";
70     JSTaggedValue ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
71     EXPECT_EQ(ms.GetDouble(), 1605788298132.0);
72 
73     str = "1880-12-30T23:59:59";
74     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
75     EXPECT_EQ(ms.GetDouble(), -2808633901000.0);
76 
77     str = "2020-11-19Z";
78     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
79     EXPECT_EQ(ms.GetDouble(), 1605744000000.0);
80 
81     str = "2020-11";
82     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
83     EXPECT_EQ(ms.GetDouble(), 1604188800000.0);
84 
85     str = "2023-1-11";
86     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
87     EXPECT_EQ(ms.GetDouble(), 1673366400000.0);
88 
89     str = "+275760-09-13T00:00:00.000Z";
90     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
91     EXPECT_EQ(ms.GetDouble(), 8640000000000000.0);
92 
93     str = "-271821-04-20T00:00:00.000Z";
94     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
95     EXPECT_EQ(ms.GetDouble(), -8640000000000000.0);
96 
97     str = "2020T12:18Z";
98     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
99     EXPECT_EQ(ms.GetDouble(), 1577881080000.0);
100 
101     str = "2020T12:18:17.231Z";
102     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
103     EXPECT_EQ(ms.GetDouble(), 1577881097231.0);
104 
105     str = "2020-11T12:18:17.231Z";
106     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
107     EXPECT_EQ(ms.GetDouble(), 1604233097231.0);
108 
109     str = "1645-11T12:18:17.231+08:00";
110     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
111     EXPECT_EQ(ms.GetDouble(), -10229658102769.0);
112 
113     str = "2020-11-19T12:18-08:12";
114     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
115     EXPECT_EQ(ms.GetDouble(), 1605817800000.0);
116 
117     // test Local time
118     str = "Thu Nov 19 2020 20:18:18 GMT+0800";
119     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
120     EXPECT_EQ(ms.GetDouble(), 1605788298000.0);
121 
122     str = "Thu Nov 19 2020 20:18 GMT-0800";
123     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
124     EXPECT_EQ(ms.GetDouble(), 1605845880000.0);
125 
126     str = "Thu Nov 03 2093 04:18 GMT";
127     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
128     EXPECT_EQ(ms.GetDouble(), 3908060280000.0);
129 
130     str = "Thu Nov 19 1820 GMT+1232";
131     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
132     EXPECT_EQ(ms.GetDouble(), -4705734720000.0);
133 
134     // test UTC time
135     str = "Thu, 19 Nov 2020 20:18:18 GMT+0800";
136     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
137     EXPECT_EQ(ms.GetDouble(), 1605788298000.0);
138 
139     str = "Thu, 19 Nov 2020 20:18 GMT-0800";
140     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
141     EXPECT_EQ(ms.GetDouble(), 1605845880000.0);
142 
143     str = "Thu 03 Jun 2093 04:18 GMT";
144     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
145     EXPECT_EQ(ms.GetDouble(), 3894841080000.0);
146 
147     str = "Thu 19 Nov 1820 GMT+1232";
148     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
149     EXPECT_EQ(ms.GetDouble(), -4705734720000.0);
150 
151     // test YYYY-MM-DD HH:MM:ss
152     str = "2020-11-19 12:18:18.132Z";
153     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
154     EXPECT_EQ(ms.GetDouble(), 1605788298132.0);
155 
156     str = "2020-11-19 12:18:18.132+02:21";
157     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
158     EXPECT_EQ(ms.GetDouble(), 1605779838132.0);
159 
160     str = "2020-11-19 12:18:18.132-02:21";
161     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
162     EXPECT_EQ(ms.GetDouble(), 1605796758132.0);
163 
164     str = "+175760-09-13 03:16:02.003Z";
165     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
166     EXPECT_EQ(ms.GetDouble(), 5484304811762003.0);
167 
168     str = "-171821-04-20 13:08:23.037Z";
169     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
170     EXPECT_EQ(ms.GetDouble(), -5484304752696963.0);
171 
172     // test Month DD,YYYY HH:MM:SS
173     str = "January 12,2006 22:19:35";
174     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
175     EXPECT_EQ(ms.GetDouble(), 1137075575000.0);
176 
177     str = "January 12,2006";
178     ms = ecmascript::JSDate::GetTimeFromString(str.c_str(), str.length());
179     EXPECT_EQ(ms.GetDouble(), 1136995200000.0);
180 }
181 }  // namespace panda::test
182