新增时间日期inner接口

Signed-off-by: zhangdd_ewan <zhangdongdong50@huawei.com>
This commit is contained in:
zhangdd_ewan 2024-03-13 15:24:31 +08:00
parent 1a2346eec6
commit 89bd8474aa
5 changed files with 256 additions and 2 deletions

View File

@ -125,7 +125,9 @@ ohos_shared_library("intl_util") {
cflags_cc = [
"-Wall",
"-fPIC",
"-frtti",
]
remove_configs = [ "//build/config/compiler:no_rtti" ]
deps = [
":CN_phonenumber_xml",
":GB_phonenumber_xml",

View File

@ -0,0 +1,172 @@
/*
* Copyright (c) 2021-2022 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 "date_time_sequence.h"
#include "i18n_hilog.h"
#include "unicode/datefmt.h"
#include "unicode/locid.h"
#include "unicode/smpdtfmt.h"
#include "unicode/dtptngen.h"
#include <regex>
namespace OHOS {
namespace Global {
namespace I18n {
std::string DateTimeSequence::GetDateOrder(const std::string &locale)
{
UErrorCode icuStatus = U_ZERO_ERROR;
icu::Locale localeObj = icu::Locale::forLanguageTag(locale.data(), icuStatus);
if (icuStatus != U_ZERO_ERROR) {
HILOG_ERROR_I18N("Failed to create locale for GetDateOrder");
return "";
}
icu::SimpleDateFormat* formatter = dynamic_cast<icu::SimpleDateFormat*>
(icu::DateFormat::createDateInstance(icu::DateFormat::EStyle::kDefault, localeObj));
if (icuStatus != U_ZERO_ERROR || formatter == nullptr) {
HILOG_ERROR_I18N("Failed to create SimpleDateFormat");
return "";
}
std::string tempValue;
icu::UnicodeString unistr;
formatter->toPattern(unistr);
delete formatter;
unistr.toUTF8String<std::string>(tempValue);
std::string value = ModifyOrder(tempValue);
std::regex pattern("d+");
std::regex reg("M+");
value = regex_replace(value, pattern, "d");
value = regex_replace(value, reg, "M");
return value;
}
std::string DateTimeSequence::GetDateTimeOrder(const std::string &locale)
{
UErrorCode status = U_ZERO_ERROR;
icu::Locale localeObj = icu::Locale::forLanguageTag(locale.data(), status);
if (status != U_ZERO_ERROR) {
HILOG_ERROR_I18N("Failed to create locale with inputs: %{public}s", locale.c_str());
return "";
}
icu::SimpleDateFormat* formatter = dynamic_cast<icu::SimpleDateFormat*>
(icu::DateFormat::createDateInstance(icu::DateFormat::EStyle::kDateTime, localeObj));
if (status != U_ZERO_ERROR || formatter == nullptr) {
HILOG_ERROR_I18N("Failed to create SimpleDateFormat");
return "";
}
icu::UnicodeString pattern;
formatter->toPattern(pattern);
std::string result;
pattern.toUTF8String<std::string>(result);
delete formatter;
return result;
}
std::string DateTimeSequence::GetAmPmTimeOrder(const std::string &locale)
{
UErrorCode status = U_ZERO_ERROR;
icu::Locale localeObj = icu::Locale::forLanguageTag(locale.data(), status);
if (status != U_ZERO_ERROR) {
HILOG_ERROR_I18N("Failed to create locale with inputs: %{public}s", locale.c_str());
return "";
}
icu::DateTimePatternGenerator* gen = icu::DateTimePatternGenerator::createInstance(localeObj, status);
icu::UnicodeString skeleton("h");
icu::UnicodeString resultStr = gen->getBestPattern(skeleton, status);
if (U_FAILURE(status) || gen == nullptr) {
HILOG_ERROR_I18N("Failed to create DateTimePatternGenerator");
return "";
}
std::string result;
resultStr.toUTF8String<std::string>(result);
std::regex pattern0("a"); // a represent morning or afternoon
std::regex pattern1("h"); // h represent time pattern, eg: hh:mm:ss
std::regex pattern2("[^01\\s]");
result = regex_replace(result, pattern0, "1");
result = regex_replace(result, pattern1, "0");
result = regex_replace(result, pattern2, "");
delete gen;
return result;
}
std::string DateTimeSequence::ModifyOrder(std::string &pattern)
{
int order[3] = { 0 }; // total 3 elements 'y', 'M'/'L', 'd'
int lengths[4] = { 0 }; // first elements is the currently found elememnts, thus 4 elements totally.
bool flag = true;
for (size_t i = 0; i < pattern.length(); ++i) {
char ch = pattern[i];
if (flag && std::isalpha(ch)) {
ProcessNormal(ch, order, 3, lengths, 4); // 3, 4 are lengths of these arrays
} else if (ch == '\'') {
flag = !flag;
}
}
std::unordered_map<char, int> pattern2index = {
{ 'y', 1 },
{ 'M', 2 },
{ 'd', 3 },
};
std::string ret;
for (int i = 0; i < 3; ++i) { // 3 is the size of orders
auto it = pattern2index.find(order[i]);
if (it == pattern2index.end()) {
continue;
}
int index = it->second;
if ((lengths[index] > 0) && (lengths[index] <= 6)) { // 6 is the max length of a filed
ret.append(lengths[index], order[i]);
}
if (i < 2) { // 2 is the size of the order minus one
ret.append(1, '-');
}
}
return ret;
}
void DateTimeSequence::ProcessNormal(char ch, int *order, size_t orderSize, int *lengths, size_t lengsSize)
{
char adjust;
int index = -1;
if (ch == 'd') {
adjust = 'd';
index = 3; // 3 is the index of 'd'
} else if ((ch == 'L') || (ch == 'M')) {
adjust = 'M';
index = 2; // 2 is the index of 'M'
} else if (ch == 'y') {
adjust = 'y';
index = 1;
} else {
return;
}
if ((index < 0) || (index >= static_cast<int>(lengsSize))) {
return;
}
if (lengths[index] == 0) {
if (lengths[0] >= 3) { // 3 is the index of order
return;
}
order[lengths[0]] = static_cast<int>(adjust);
++lengths[0];
lengths[index] = 1;
} else {
++lengths[index];
}
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -26,6 +26,7 @@
#include "measure_data.h"
#include "phone_number_format_mock.h"
#include "preferred_language.h"
#include "date_time_sequence.h"
using namespace OHOS::Global::I18n;
using testing::ext::TestSize;
@ -1428,6 +1429,46 @@ HWTEST_F(I18nTest, I18nFuncTest059, TestSize.Level1)
delete current;
delete other;
}
/**
* @tc.name: I18nFuncTest060
* @tc.desc: Test I18n DateTimeSequence
* @tc.type: FUNC
*/
HWTEST_F(I18nTest, I18nFuncTest060, TestSize.Level1)
{
std::string locale = "zh-Hans";
std::string order1 = DateTimeSequence::GetDateOrder(locale);
std::string order2 = DateTimeSequence::GetDateTimeOrder(locale);
std::string order3 = DateTimeSequence::GetAmPmTimeOrder(locale);
EXPECT_EQ(order1, "y-M-d");
EXPECT_EQ(order2, "{1} {0}");
EXPECT_EQ(order3, "10");
std::string locale2 = "en";
std::string order4 = DateTimeSequence::GetDateOrder(locale2);
std::string order5 = DateTimeSequence::GetDateTimeOrder(locale2);
std::string order6 = DateTimeSequence::GetAmPmTimeOrder(locale2);
EXPECT_EQ(order4, "M-d-y");
EXPECT_EQ(order5, "{1}, {0}");
EXPECT_EQ(order6, "0 1");
std::string locale3 = "ar";
std::string order7 = DateTimeSequence::GetDateOrder(locale3);
std::string order8 = DateTimeSequence::GetDateTimeOrder(locale3);
std::string order9 = DateTimeSequence::GetAmPmTimeOrder(locale3);
EXPECT_EQ(order7, "d-M-y");
EXPECT_EQ(order8, "{1}، {0}");
EXPECT_EQ(order9, "0 1");
std::string locale4 = "ug";
std::string order10 = DateTimeSequence::GetDateOrder(locale4);
std::string order11 = DateTimeSequence::GetDateTimeOrder(locale4);
std::string order12 = DateTimeSequence::GetAmPmTimeOrder(locale4);
EXPECT_EQ(order10, "d-M-y");
EXPECT_EQ(order11, "{1}، {0}");
EXPECT_EQ(order12, "0 1");
}
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -23,8 +23,7 @@ int I18nFuncTest001(void);
int I18nFuncTest002(void);
int I18nFuncTest003(void);
int I18nFuncTest004(void);
int I18nFuncTest005(void);
int I18nFuncTest006(void);
int I18nFuncTest060(void);
int I18nFuncTest007(void);
int I18nFuncTest008(void);
int I18nFuncTest009(void);
@ -78,6 +77,7 @@ int I18nFuncTest056(void);
int I18nFuncTest057(void);
int I18nFuncTest058(void);
int I18nFuncTest059(void);
int I18nFuncTest060(void);
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2021-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.
*/
#ifndef OHOS_GLOBAL_I18N_DATE_TIME_SEQUENCE_H
#define OHOS_GLOBAL_I18N_DATE_TIME_SEQUENCE_H
#include <string>
#include "locale_info.h"
namespace OHOS {
namespace Global {
namespace I18n {
class DateTimeSequence {
public:
static std::string GetDateOrder(const std::string &locale);
static std::string GetDateTimeOrder(const std::string &locale);
static std::string GetAmPmTimeOrder(const std::string &locale);
private:
static std::string ModifyOrder(std::string &pattern);
static void ProcessNormal(char ch, int *order, size_t orderSize, int *lengths, size_t lengsSize);
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
#endif