mirror of
https://gitee.com/openharmony/global_i18n
synced 2024-11-26 16:51:24 +00:00
!816 改读取时区Id列表方式
Merge pull request !816 from zhangdd_ewan/cherry-240904
This commit is contained in:
commit
76c9a0abb9
@ -25,10 +25,6 @@
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
static const uint32_t BYTE_ARRAY_OFFSET_FIRST = 24;
|
||||
static const uint32_t BYTE_ARRAY_OFFSET_SECOND = 16;
|
||||
static const uint32_t BYTE_ARRAY_OFFSET_THIRD = 8;
|
||||
static const uint32_t BYTE_ARRAY_INDEX_THIRD = 3;
|
||||
static std::set<std::string> availableIDs;
|
||||
|
||||
void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
|
||||
|
@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cerrno>
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <filesystem>
|
||||
@ -39,8 +40,8 @@ using namespace std;
|
||||
static const std::string PSEUDO_LOCALE_TAG = "en-XA";
|
||||
static const std::string PSEUDO_START_TAG = "{";
|
||||
static const std::string PSEUDO_END_TAG = "}";
|
||||
static const char *TZDATA_PATH = "/system/etc/zoneinfo/tzdata";
|
||||
static const char *DISTRO_TZDATA_PATH = "/system/etc/tzdata_distro/hos/tzdata";
|
||||
constexpr const char *TIMEZONE_LIST_CONFIG_PATH = "/system/etc/zoneinfo/timezone_list.cfg";
|
||||
constexpr const char *DISTRO_TIMEZONE_LIST_CONFIG = "/system/etc/tzdata_distro/timezone_list.cfg";
|
||||
static std::mutex validLocaleMutex;
|
||||
|
||||
void Split(const string &src, const string &sep, vector<string> &dest)
|
||||
@ -311,51 +312,33 @@ bool CheckSystemPermission()
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t ConvertBytesToSizeT(const char *byteArray)
|
||||
{
|
||||
size_t num0 = static_cast<size_t>(byteArray[0]) << BYTE_ARRAY_OFFSET_FIRST;
|
||||
size_t num1 = static_cast<size_t>(byteArray[1]) << BYTE_ARRAY_OFFSET_SECOND;
|
||||
size_t num2 = static_cast<size_t>(byteArray[2]) << BYTE_ARRAY_OFFSET_THIRD;
|
||||
return num0 + num1 + num2 + static_cast<size_t>(byteArray[BYTE_ARRAY_INDEX_THIRD]);
|
||||
}
|
||||
|
||||
std::set<std::string> GetTimeZoneAvailableIDs(I18nErrorCode &errorCode)
|
||||
{
|
||||
if (availableIDs.size() != 0) {
|
||||
return availableIDs;
|
||||
}
|
||||
struct stat s;
|
||||
const char *tzdataFilePath = stat(DISTRO_TZDATA_PATH, &s) == 0 ? DISTRO_TZDATA_PATH : TZDATA_PATH;
|
||||
std::unique_ptr<char[]> resolvedPath = std::make_unique<char[]>(PATH_MAX);
|
||||
if (realpath(tzdataFilePath, resolvedPath.get()) == nullptr) {
|
||||
HILOG_ERROR_I18N("GetTimeZoneAvailableIDs tzdata file path isn't exists.");
|
||||
const char *tzIdConfigPath = stat(DISTRO_TIMEZONE_LIST_CONFIG, &s) == 0 ?
|
||||
DISTRO_TIMEZONE_LIST_CONFIG : TIMEZONE_LIST_CONFIG_PATH;
|
||||
std::unique_ptr<char[]> resolvedPath = std::make_unique<char[]>(PATH_MAX + 1);
|
||||
if (realpath(tzIdConfigPath, resolvedPath.get()) == nullptr) {
|
||||
HILOG_ERROR_I18N("Get realpath failed, errno: %{public}d.", errno);
|
||||
return availableIDs;
|
||||
}
|
||||
std::ifstream tzdataFile(resolvedPath.get(), std::ios::in | std::ios::binary);
|
||||
if (!tzdataFile.is_open()) {
|
||||
HILOG_ERROR_I18N("Open tzdata failed");
|
||||
std::ifstream file(resolvedPath.get());
|
||||
if (!file.good()) {
|
||||
HILOG_ERROR_I18N("Open timezone list config file failed.");
|
||||
return availableIDs;
|
||||
}
|
||||
const size_t versionLength = 12;
|
||||
tzdataFile.ignore(versionLength);
|
||||
// offset means indexOffset or dataOffset.
|
||||
const size_t offsetSize = 4;
|
||||
// tempSize is the length of one index, include tz id length, data offset and tz file length.
|
||||
const size_t tempSize = 48;
|
||||
char *temp = new char[tempSize];
|
||||
const size_t offsetSizeTwice = 2;
|
||||
tzdataFile.read(temp, offsetSize * offsetSizeTwice);
|
||||
size_t indexOffset = ConvertBytesToSizeT(temp);
|
||||
size_t dataOffset = ConvertBytesToSizeT(temp + offsetSize);
|
||||
tzdataFile.ignore(offsetSize);
|
||||
while (indexOffset < dataOffset) {
|
||||
tzdataFile.read(temp, tempSize);
|
||||
indexOffset += tempSize;
|
||||
std::string tzid(temp);
|
||||
availableIDs.insert(tzid);
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
if (line.length() == 0) {
|
||||
break;
|
||||
}
|
||||
line = line.substr(0, line.find_last_not_of("\r\n") + 1);
|
||||
availableIDs.insert(line);
|
||||
}
|
||||
tzdataFile.close();
|
||||
delete[] temp;
|
||||
file.close();
|
||||
return availableIDs;
|
||||
}
|
||||
} // namespace I18n
|
||||
|
Loading…
Reference in New Issue
Block a user