• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 <chrono>
17 #include <ctime>
18 #include <clocale>
19 #include <sstream>
20 #include "unicode/timezone.h"
21 #include "plugins/ets/runtime/types/ets_string.h"
22 
23 namespace ark::ets::intrinsics {
24 
25 constexpr const int32_t MS_IN_MINUTES = 60000;
26 
EscompatDateNow()27 extern "C" double EscompatDateNow()
28 {
29     auto now = std::chrono::system_clock::now();
30     auto nowMs = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
31     return nowMs.time_since_epoch().count();
32 }
33 
EscompatDateGetLocalTimezoneOffset(int64_t ms)34 extern "C" int64_t EscompatDateGetLocalTimezoneOffset(int64_t ms)
35 {
36     UErrorCode success = U_ZERO_ERROR;
37     int32_t stdOffset;
38     int32_t dstOffset;
39     icu::TimeZone *tzlocal = icu::TimeZone::createDefault();
40     tzlocal->getOffset(ms, 1, stdOffset, dstOffset, success);
41     delete tzlocal;
42     return -(stdOffset + dstOffset) / MS_IN_MINUTES;
43 }
44 
EscompatDateGetTimezoneName(int64_t ms)45 extern "C" EtsString *EscompatDateGetTimezoneName(int64_t ms)
46 {
47     UErrorCode success = U_ZERO_ERROR;
48     int32_t stdOffset;
49     int32_t dstOffset;
50     icu::TimeZone *tzlocal = icu::TimeZone::createDefault();
51     icu::UnicodeString s;
52     std::string result;
53     tzlocal->getOffset(ms, 0, stdOffset, dstOffset, success);
54     bool inDayLight = (dstOffset != 0);
55     tzlocal->getDisplayName(static_cast<UBool>(inDayLight), icu::TimeZone::EDisplayType::LONG, s);
56     s.toUTF8String(result);
57     delete tzlocal;
58     return EtsString::CreateFromMUtf8(result.c_str());
59 }
60 
ChronoGetCpuTime()61 extern "C" int64_t ChronoGetCpuTime()
62 {
63     // NOTE(ipetrov, #15499): Need to change approach when coroutine can migrate to other thread
64     return ark::os::time::GetClockTimeInThreadCpuTime();
65 }
66 
67 }  // namespace ark::ets::intrinsics
68