diff --git a/BUILD.gn b/BUILD.gn index 5004a81c046489524f31baa003cdbdcd78e4e1d3..5bcce284a18292cd7b7d4e0eb6af9cd55a273601 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1334,7 +1334,6 @@ if (is_mingw) { "common_components/platform/unix/map.cpp", "ecmascript/platform/unix/file.cpp", "ecmascript/platform/unix/map.cpp", - "ecmascript/platform/unix/time.cpp", "ecmascript/platform/unix/aot_crash_info.cpp", ] if (is_mac) { @@ -1352,6 +1351,7 @@ if (is_mingw) { "ecmascript/platform/unix/mac/asm_stack.cpp", "ecmascript/platform/common/ecma_context.cpp", "ecmascript/platform/common/parameters.cpp", + "ecmascript/platform/unix/time.cpp", ] } else if (target_os == "ios") { ecma_platform_source += [ @@ -1368,6 +1368,7 @@ if (is_mingw) { "ecmascript/platform/common/ecma_context.cpp", "ecmascript/platform/common/parameters.cpp", "ecmascript/platform/unix/mac/asm_stack.cpp", + "ecmascript/platform/unix/time.cpp", ] } else if (is_ohos || target_os == "android") { ecma_platform_source += [ @@ -1381,6 +1382,7 @@ if (is_mingw) { "ecmascript/platform/unix/ohos/dfx_hisys_event.cpp", "ecmascript/platform/common/module.cpp", "ecmascript/platform/common/asm_stack.cpp", + "ecmascript/platform/unix/ohos/time.cpp", ] if (is_ohos) { @@ -1420,6 +1422,7 @@ if (is_mingw) { "ecmascript/platform/common/ecma_context.cpp", "ecmascript/platform/common/parameters.cpp", "ecmascript/platform/common/asm_stack.cpp", + "ecmascript/platform/unix/time.cpp", ] } else { print("Invalid os!") diff --git a/ecmascript/platform/unix/ohos/time.cpp b/ecmascript/platform/unix/ohos/time.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f42ac6dcb3bda296d12a232eb99f201f732f0c2 --- /dev/null +++ b/ecmascript/platform/unix/ohos/time.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ecmascript/platform/time.h" + +#include + +namespace panda::ecmascript { +static constexpr uint16_t THOUSAND = 1000; +static constexpr int SEC_PER_MINUTE = 60; + +int64_t GetLocalOffsetFromOS(int64_t timeMs, bool isLocal) +{ + if (!isLocal) { + return 0; + } + timeMs /= THOUSAND; + time_t tv = timeMs; + struct tm tm { + }; + struct tm *t = localtime_noenv_r(&tv, &tm); + if (t == nullptr) { + return 0; + } + + return t->tm_gmtoff / SEC_PER_MINUTE; +} + +bool IsDst(int64_t timeMs) +{ + timeMs /= THOUSAND; + time_t tv = timeMs; + struct tm tm { + }; + struct tm *t = nullptr; + t = localtime_noenv_r(&tv, &tm); + return (t != nullptr) && t->tm_isdst; +} +} // namespace panda::ecmascript diff --git a/test/moduletest/datecase/datecase.js b/test/moduletest/datecase/datecase.js index fd393531dc201c74bead92b2354570b803685d18..ff1e00407644eb28cec63b2b4b4cfea99f3a89cd 100644 --- a/test/moduletest/datecase/datecase.js +++ b/test/moduletest/datecase/datecase.js @@ -56,4 +56,10 @@ print(d9.getMonth() == 1); print(d9.getDate() == 1); print(d9.getHours() == 0); -print(d10.getMonth(),d10.getDate(),d10.getHours()); \ No newline at end of file +print(d10.getMonth(),d10.getDate(),d10.getHours()); + +print(Date.parse("1972T23:50:03.500+01:00")); +print(Date.parse("1972T23:50:03.500Z")); +print(Date.parse("1972T23:50:03.500Z")); +print(Date.parse("1972T23:50:03Z")); +print(Date.parse("1972Z")); \ No newline at end of file diff --git a/test/moduletest/datecase/expect_output.txt b/test/moduletest/datecase/expect_output.txt index 2049fae296c5eda65d0b9377e33c88154d798202..c4c37294f4b79aa1dac6f24e333972b8a3b49429 100644 --- a/test/moduletest/datecase/expect_output.txt +++ b/test/moduletest/datecase/expect_output.txt @@ -32,3 +32,8 @@ true true true 2 26 23 +63154203500 +63157803500 +63157803500 +63157803000 +63072000000