1 /**
2 * Copyright (c) 2021-2024 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 constexpr const int32_t MS_IN_SECOND = 1000;
27
EscompatDateNow()28 extern "C" double EscompatDateNow()
29 {
30 auto now = std::chrono::system_clock::now();
31 auto nowMs = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
32 return nowMs.time_since_epoch().count();
33 }
34
EscompatDateGetLocalTimezoneOffset(int64_t ms)35 extern "C" int64_t EscompatDateGetLocalTimezoneOffset(int64_t ms)
36 {
37 UErrorCode success = U_ZERO_ERROR;
38 int32_t stdOffset;
39 int32_t dstOffset;
40 icu::TimeZone *tzlocal = icu::TimeZone::createDefault();
41 tzlocal->getOffset(ms, 1, stdOffset, dstOffset, success);
42 delete tzlocal;
43 return -(stdOffset + dstOffset) / MS_IN_MINUTES;
44 }
45
EscompatDateGetTimezoneName(int64_t ms)46 extern "C" EtsString *EscompatDateGetTimezoneName(int64_t ms)
47 {
48 UErrorCode success = U_ZERO_ERROR;
49 int32_t stdOffset;
50 int32_t dstOffset;
51 icu::TimeZone *tzlocal = icu::TimeZone::createDefault();
52 icu::UnicodeString s;
53 std::string result;
54 tzlocal->getOffset(ms, 0, stdOffset, dstOffset, success);
55 bool inDayLight = (dstOffset != 0);
56 tzlocal->getDisplayName(static_cast<UBool>(inDayLight), icu::TimeZone::EDisplayType::LONG, s);
57 s.toUTF8String(result);
58 delete tzlocal;
59 return EtsString::CreateFromMUtf8(result.c_str());
60 }
61
EscompatDateGetLocaleString(EtsString * format,EtsString * locale,int64_t ms,uint8_t isUtc)62 extern "C" EtsString *EscompatDateGetLocaleString(EtsString *format, EtsString *locale, int64_t ms, uint8_t isUtc)
63 {
64 PandaVector<uint8_t> buf;
65 auto formatS = std::string(format->ConvertToStringView(&buf));
66 auto localeS = std::string(locale->ConvertToStringView(&buf));
67 std::time_t seconds = ms / MS_IN_SECOND;
68 std::stringstream ss;
69 ss.imbue(std::locale(ss.getloc(), new std::time_put_byname<char>(localeS.c_str())));
70 if (static_cast<bool>(isUtc)) {
71 ss << std::put_time(std::gmtime(&seconds), formatS.c_str());
72 } else {
73 ss << std::put_time(std::localtime(&seconds), formatS.c_str());
74 }
75 return EtsString::CreateFromMUtf8(ss.str().c_str());
76 }
77
78 } // namespace ark::ets::intrinsics
79