1 /* 2 * Copyright (c) 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 <cstdint> 17 #include <cstdlib> 18 #include <chrono> 19 #include <string> 20 #include <vector> 21 #include "i18n_hilog.h" 22 #include "i18n_ffi.h" 23 #include "i18n_struct.h" 24 #include "i18n_timezone_ffi.h" 25 #include "i18n_timezone_impl.h" 26 #include <utility> 27 28 namespace OHOS { 29 namespace Global { 30 namespace I18n { 31 using namespace OHOS::FFI; 32 using namespace OHOS::HiviewDFX; 33 extern "C" 34 { FfiI18nTimezoneConstructor(char * id,bool isZoneId)35 int64_t FfiI18nTimezoneConstructor(char* id, bool isZoneId) 36 { 37 std::string zoneId(id); 38 std::unique_ptr<I18nTimeZone> timezoneInstance = I18nTimeZone::CreateInstance(zoneId, isZoneId); 39 if (timezoneInstance == nullptr) { 40 HILOG_ERROR_I18N("Create I18nTimeZone fail"); 41 return -1; 42 } 43 auto ffiTimezoneInstance = FFIData::Create<FfiI18nTimeZone>(std::move(timezoneInstance)); 44 if (ffiTimezoneInstance == nullptr) { 45 HILOG_ERROR_I18N("Create FfiI18nTimeZone fail"); 46 return -1; 47 } 48 return ffiTimezoneInstance->GetID(); 49 } 50 FfiI18nTimezoneGetTimezonesByLocation(double longitude,double latitude)51 CArrStr FfiI18nTimezoneGetTimezonesByLocation(double longitude, double latitude) 52 { 53 return VectorStringToCArr(I18nTimeZone::GetTimezoneIdByLocation(longitude, latitude)); 54 } 55 FfiI18nTimezoneGetCityDisplayName(char * cityID,char * locale)56 char* FfiI18nTimezoneGetCityDisplayName(char* cityID, char* locale) 57 { 58 std::string zoneCityID(cityID); 59 std::string zoneLocale(locale); 60 return MallocCString(I18nTimeZone::GetCityDisplayName(zoneCityID, zoneLocale)); 61 } 62 FfiI18nTimezoneGetAvailableZoneCityIDs()63 CArrStr FfiI18nTimezoneGetAvailableZoneCityIDs() 64 { 65 std::unordered_set<std::string> cityIDSet = I18nTimeZone::GetAvailableZoneCityIDs(); 66 std::vector<std::string> cityIDVec(cityIDSet.begin(), cityIDSet.end()); 67 return VectorStringToCArr(cityIDVec); 68 } 69 FfiI18nTimezoneGetAvailableIDs()70 CArrStr FfiI18nTimezoneGetAvailableIDs() 71 { 72 I18nErrorCode errorCode = I18nErrorCode::SUCCESS; 73 std::set<std::string> timezoneIDSet = I18nTimeZone::GetAvailableIDs(errorCode); 74 if (errorCode != I18nErrorCode::SUCCESS) { 75 return { nullptr, 0 }; 76 } 77 std::vector<std::string> timezoneIDVec(timezoneIDSet.begin(), timezoneIDSet.end()); 78 return VectorStringToCArr(timezoneIDVec); 79 } 80 FfiI18nTimezoneGetDisplayName(int64_t remoteDataID,char * locale,bool isDST,int32_t parameterStatus)81 char* FfiI18nTimezoneGetDisplayName(int64_t remoteDataID, char* locale, bool isDST, int32_t parameterStatus) 82 { 83 auto timezone = FFIData::GetData<FfiI18nTimeZone>(remoteDataID); 84 if (!timezone) { 85 HILOG_ERROR_I18N("The FfiI18nTimeZone instance is nullptr"); 86 return nullptr; 87 } 88 char* result; 89 if (parameterStatus == 0) { 90 result = timezone->getDisplayName(); 91 } else if (parameterStatus == 1) { // 1 represents one string parameter. 92 result = timezone->getDisplayName(std::string(locale)); 93 } else if (parameterStatus == 2) { // 2 represents one boolean parameter. 94 result = timezone->getDisplayName(isDST); 95 } else { 96 result = timezone->getDisplayName(std::string(locale), isDST); 97 } 98 return result; 99 } 100 FfiI18nTimezoneGetOffset(int64_t remoteDataID,double date,int32_t parameterStatus)101 int32_t FfiI18nTimezoneGetOffset(int64_t remoteDataID, double date, int32_t parameterStatus) 102 { 103 auto timezone = FFIData::GetData<FfiI18nTimeZone>(remoteDataID); 104 if (!timezone) { 105 HILOG_ERROR_I18N("The FfiI18nTimeZone instance is nullptr"); 106 return 0; 107 } 108 double cDate = 0; 109 if (parameterStatus == 0) { // 0 reprensents date is null, use system time instead 110 auto time = std::chrono::system_clock::now(); 111 auto since_epoch = time.time_since_epoch(); 112 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch); 113 cDate = (double)millis.count(); 114 } else { 115 cDate = date; 116 } 117 return timezone->getOffset(cDate); 118 } 119 FfiI18nTimezoneGetRawOffset(int64_t remoteDataID)120 int32_t FfiI18nTimezoneGetRawOffset(int64_t remoteDataID) 121 { 122 auto timezone = FFIData::GetData<FfiI18nTimeZone>(remoteDataID); 123 if (!timezone) { 124 HILOG_ERROR_I18N("The FfiI18nTimeZone instance is nullptr"); 125 return 0; 126 } 127 return timezone->getRawOffset(); 128 } 129 FfiI18nTimezoneGetID(int64_t remoteDataID)130 char* FfiI18nTimezoneGetID(int64_t remoteDataID) 131 { 132 auto timezone = FFIData::GetData<FfiI18nTimeZone>(remoteDataID); 133 if (!timezone) { 134 HILOG_ERROR_I18N("The FfiI18nTimeZone instance is nullptr"); 135 return nullptr; 136 } 137 return timezone->getID(); 138 } 139 } 140 } // namespace I18n 141 } // namespace Global 142 } // namespace OHOS