mirror of
https://gitee.com/openharmony/global_i18n
synced 2025-02-21 08:30:35 +00:00
fix DateTimeFormat calendar timezone
Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
parent
150761236f
commit
d7a8a6ff1f
@ -37,8 +37,8 @@ class DateTimeFormat {
|
||||
public:
|
||||
DateTimeFormat(const std::vector<std::string> &localeTags, std::map<std::string, std::string> &configs);
|
||||
virtual ~DateTimeFormat();
|
||||
std::string Format(int64_t *date, size_t size);
|
||||
std::string FormatRange(int64_t *fromDate, size_t fromDateSize, int64_t *toDate, size_t toDateSize);
|
||||
std::string Format(int64_t milliseconds);
|
||||
std::string FormatRange(int64_t fromMilliseconds, int64_t toMilliseconds);
|
||||
void GetResolvedOptions(std::map<std::string, std::string> &map);
|
||||
std::string GetDateStyle() const;
|
||||
std::string GetTimeStyle() const;
|
||||
@ -113,6 +113,9 @@ private:
|
||||
static const int HALF_HOUR = 30;
|
||||
static const int HOURS_OF_A_DAY = 24;
|
||||
static bool icuInitialized;
|
||||
static const char *TIMEZONE_KEY;
|
||||
static const char *DEFAULT_TIMEZONE;
|
||||
static constexpr int SYS_PARAM_LEN = 128;
|
||||
static bool Init();
|
||||
static std::map<std::string, icu::DateFormat::EStyle> dateTimeStyle;
|
||||
bool InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs);
|
||||
@ -136,6 +139,7 @@ private:
|
||||
int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size);
|
||||
bool CheckInitSuccess();
|
||||
void FreeDateTimeFormat();
|
||||
std::string GetSystemTimezone();
|
||||
};
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
|
@ -13,12 +13,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "date_time_format.h"
|
||||
#include "ohos/init_data.h"
|
||||
#include "locale_config.h"
|
||||
#include "ohos/init_data.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
const char *DateTimeFormat::TIMEZONE_KEY = "persist.time.timezone";
|
||||
const char *DateTimeFormat::DEFAULT_TIMEZONE = "GMT";
|
||||
|
||||
using namespace icu;
|
||||
bool DateTimeFormat::icuInitialized = DateTimeFormat::Init();
|
||||
|
||||
@ -486,7 +490,16 @@ int64_t DateTimeFormat::GetArrayValue(int64_t *dateArray, size_t index, size_t s
|
||||
}
|
||||
}
|
||||
|
||||
std::string DateTimeFormat::Format(int64_t *date, size_t size)
|
||||
std::string DateTimeFormat::GetSystemTimezone()
|
||||
{
|
||||
std::string systemTimezone = ReadSystemParameter(TIMEZONE_KEY, SYS_PARAM_LEN);
|
||||
if (systemTimezone.length() == 0) {
|
||||
systemTimezone = DEFAULT_TIMEZONE;
|
||||
}
|
||||
return systemTimezone;
|
||||
}
|
||||
|
||||
std::string DateTimeFormat::Format(int64_t milliseconds)
|
||||
{
|
||||
if (!createSuccess) {
|
||||
return "";
|
||||
@ -494,27 +507,18 @@ std::string DateTimeFormat::Format(int64_t *date, size_t size)
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
std::string result;
|
||||
UnicodeString dateString;
|
||||
int64_t year = GetArrayValue(date, YEAR_INDEX, size);
|
||||
int64_t month = GetArrayValue(date, MONTH_INDEX, size);
|
||||
int64_t day = GetArrayValue(date, DAY_INDEX, size);
|
||||
int64_t hour = GetArrayValue(date, HOUR_INDEX, size);
|
||||
int64_t minute = GetArrayValue(date, MINUTE_INDEX, size);
|
||||
int64_t second = GetArrayValue(date, SECOND_INDEX, size);
|
||||
calendar->clear();
|
||||
calendar->set(year, month, day, hour, minute, second);
|
||||
if (!timeZone.empty()) {
|
||||
UDate timestamp = calendar->getTime(status);
|
||||
auto zone = std::unique_ptr<TimeZone>(TimeZone::createTimeZone(timeZone.c_str()));
|
||||
calendar->setTimeZone(*zone);
|
||||
dateFormat->setTimeZone(*zone);
|
||||
calendar->setTime(timestamp, status);
|
||||
}
|
||||
std::string timezoneStr = timeZone.empty() ? GetSystemTimezone() : timeZone;
|
||||
auto zone = std::unique_ptr<TimeZone>(TimeZone::createTimeZone(timezoneStr.c_str()));
|
||||
calendar->setTimeZone(*zone);
|
||||
dateFormat->setTimeZone(*zone);
|
||||
calendar->setTime((UDate)milliseconds, status);
|
||||
dateFormat->format(calendar->getTime(status), dateString, status);
|
||||
dateString.toUTF8String(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize, int64_t *toDate, size_t toDateSize)
|
||||
std::string DateTimeFormat::FormatRange(int64_t fromMilliseconds, int64_t toMilliseconds)
|
||||
{
|
||||
if (!createSuccess) {
|
||||
return "";
|
||||
@ -522,40 +526,20 @@ std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize,
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
std::string result;
|
||||
UnicodeString dateString;
|
||||
int64_t year = GetArrayValue(fromDate, YEAR_INDEX, fromDateSize);
|
||||
int64_t month = GetArrayValue(fromDate, MONTH_INDEX, fromDateSize);
|
||||
int64_t day = GetArrayValue(fromDate, DAY_INDEX, fromDateSize);
|
||||
int64_t hour = GetArrayValue(fromDate, HOUR_INDEX, fromDateSize);
|
||||
int64_t minute = GetArrayValue(fromDate, MINUTE_INDEX, fromDateSize);
|
||||
int64_t second = GetArrayValue(fromDate, SECOND_INDEX, fromDateSize);
|
||||
calendar->clear();
|
||||
calendar->set(year, month, day, hour, minute, second);
|
||||
if (!timeZone.empty()) {
|
||||
UDate timestamp = calendar->getTime(status);
|
||||
auto zone = std::unique_ptr<TimeZone>(TimeZone::createTimeZone(timeZone.c_str()));
|
||||
calendar->setTimeZone(*zone);
|
||||
dateIntvFormat->setTimeZone(*zone);
|
||||
calendar->setTime(timestamp, status);
|
||||
}
|
||||
year = GetArrayValue(toDate, YEAR_INDEX, toDateSize);
|
||||
month = GetArrayValue(toDate, MONTH_INDEX, toDateSize);
|
||||
day = GetArrayValue(toDate, DAY_INDEX, toDateSize);
|
||||
hour = GetArrayValue(toDate, HOUR_INDEX, toDateSize);
|
||||
minute = GetArrayValue(toDate, MINUTE_INDEX, toDateSize);
|
||||
second = GetArrayValue(toDate, SECOND_INDEX, toDateSize);
|
||||
std::string timezoneStr = timeZone.empty() ? GetSystemTimezone() : timeZone;
|
||||
auto zone = std::unique_ptr<TimeZone>(TimeZone::createTimeZone(timezoneStr.c_str()));
|
||||
calendar->setTimeZone(*zone);
|
||||
dateIntvFormat->setTimeZone(*zone);
|
||||
calendar->setTime((UDate)fromMilliseconds, status);
|
||||
|
||||
auto toCalendar = std::unique_ptr<Calendar>(Calendar::createInstance(locale, status));
|
||||
if (status != U_ZERO_ERROR || toCalendar == nullptr) {
|
||||
return "";
|
||||
}
|
||||
toCalendar->clear();
|
||||
toCalendar->set(year, month, day, hour, minute, second);
|
||||
if (!timeZone.empty()) {
|
||||
UDate timestamp = toCalendar->getTime(status);
|
||||
auto zone = std::unique_ptr<TimeZone>(TimeZone::createTimeZone(timeZone.c_str()));
|
||||
toCalendar->setTimeZone(*zone);
|
||||
dateIntvFormat->setTimeZone(*zone);
|
||||
toCalendar->setTime(timestamp, status);
|
||||
}
|
||||
toCalendar->setTimeZone(*zone);
|
||||
toCalendar->setTime((UDate)toMilliseconds, status);
|
||||
FieldPosition pos = 0;
|
||||
dateIntvFormat->format(*calendar, *toCalendar, dateString, pos, status);
|
||||
dateString.toUTF8String(result);
|
||||
|
@ -62,7 +62,7 @@ void IntlTest::TearDown(void)
|
||||
HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1)
|
||||
{
|
||||
string locale = "zh-CN-u-hc-h12";
|
||||
string expects = "公元2021年4月14日星期三 下午3:05:03";
|
||||
string expects = "公元1970年1月1日星期四 上午8:20:34";
|
||||
vector<string> locales;
|
||||
locales.push_back("jessie");
|
||||
locales.push_back(locale);
|
||||
@ -79,8 +79,8 @@ HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1)
|
||||
EXPECT_TRUE(false);
|
||||
return;
|
||||
}
|
||||
int64_t date[] = { 2021, 3, 14, 15, 5, 3 };
|
||||
string out = dateFormat->Format(date, 6);
|
||||
int64_t milliseconds = 1234567;
|
||||
string out = dateFormat->Format(milliseconds);
|
||||
EXPECT_EQ(out, expects);
|
||||
EXPECT_EQ(dateFormat->GetYear(), "numeric");
|
||||
EXPECT_EQ(dateFormat->GetMonth(), "long");
|
||||
@ -156,7 +156,7 @@ HWTEST_F(IntlTest, IntlFuncTest003, TestSize.Level1)
|
||||
HWTEST_F(IntlTest, IntlFuncTest004, TestSize.Level1)
|
||||
{
|
||||
string locale = "en-GB";
|
||||
string expects = "14 April 2021, 15:05 – 5 May 2021, 10:05";
|
||||
string expects = "2 January 1970, 18:17 – 12 January 1970, 18:20";
|
||||
vector<string> locales;
|
||||
locales.push_back(locale);
|
||||
string dateStyle = "long";
|
||||
@ -168,9 +168,9 @@ HWTEST_F(IntlTest, IntlFuncTest004, TestSize.Level1)
|
||||
EXPECT_TRUE(false);
|
||||
return;
|
||||
}
|
||||
int64_t date1[] = {2021, 3, 14, 15, 5, 3};
|
||||
int64_t date2[] = {2021, 4, 5, 10, 5, 3};
|
||||
string out = dateFormat->FormatRange(date1, 6, date2, 6);
|
||||
int64_t fromMilliseconds = 123456789;
|
||||
int64_t toMilliseconds = 987654321;
|
||||
string out = dateFormat->FormatRange(fromMilliseconds, toMilliseconds);
|
||||
EXPECT_EQ(out, expects);
|
||||
EXPECT_EQ(dateFormat->GetDateStyle(), dateStyle);
|
||||
EXPECT_EQ(dateFormat->GetTimeStyle(), timeStyle);
|
||||
@ -185,7 +185,7 @@ HWTEST_F(IntlTest, IntlFuncTest004, TestSize.Level1)
|
||||
HWTEST_F(IntlTest, IntlFuncTest005, TestSize.Level1)
|
||||
{
|
||||
string locale = "ja";
|
||||
string expects = "2021年4月14日水曜日";
|
||||
string expects = "1970年1月2日金曜日";
|
||||
vector<string> locales;
|
||||
locales.push_back(locale);
|
||||
map<string, string> options = { { "year", "numeric" },
|
||||
@ -197,13 +197,13 @@ HWTEST_F(IntlTest, IntlFuncTest005, TestSize.Level1)
|
||||
EXPECT_TRUE(false);
|
||||
return;
|
||||
}
|
||||
int64_t date[] = { 2021, 3, 14, 15, 5, 3 };
|
||||
string out = dateFormat->Format(date, 6);
|
||||
int64_t milliseconds = 123456789;
|
||||
string out = dateFormat->Format(milliseconds);
|
||||
EXPECT_EQ(out, expects);
|
||||
int64_t date1[] = {2021, 3, 14, 15, 5, 3};
|
||||
int64_t date2[] = {2021, 4, 5, 10, 5, 3};
|
||||
expects = "2021/4/14水曜日~2021/5/5水曜日";
|
||||
out = dateFormat->FormatRange(date1, 6, date2, 6);
|
||||
int64_t fromMilliseconds = 123456789;
|
||||
int64_t toMilliseconds = 987654321;
|
||||
expects = "1970/1/2金曜日~1970/1/12月曜日";
|
||||
out = dateFormat->FormatRange(fromMilliseconds, toMilliseconds);
|
||||
EXPECT_EQ(out, expects);
|
||||
delete dateFormat;
|
||||
}
|
||||
@ -392,14 +392,14 @@ HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1)
|
||||
map<string, string> options = {};
|
||||
vector<string> locales;
|
||||
locales.push_back(locale);
|
||||
std::string expects = "4/14/21";
|
||||
std::string expects = "3/11/82";
|
||||
DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options);
|
||||
if (!dateFormat) {
|
||||
EXPECT_TRUE(false);
|
||||
return;
|
||||
}
|
||||
int64_t date[] = {2021, 3, 14, 15, 5, 3};
|
||||
string out = dateFormat->Format(date, 6);
|
||||
int64_t milliseconds = 123456789123456;
|
||||
string out = dateFormat->Format(milliseconds);
|
||||
EXPECT_EQ(out, expects);
|
||||
delete dateFormat;
|
||||
}
|
||||
@ -434,7 +434,7 @@ HWTEST_F(IntlTest, IntlFuncTest0013, TestSize.Level1)
|
||||
HWTEST_F(IntlTest, IntlFuncTest0014, TestSize.Level1)
|
||||
{
|
||||
string locale = "zh-CN-u-hc-h12";
|
||||
string expects = "北美太平洋夏令时间";
|
||||
string expects = "北美太平洋标准时间";
|
||||
vector<string> locales;
|
||||
locales.push_back("jessie");
|
||||
locales.push_back(locale);
|
||||
@ -444,8 +444,8 @@ HWTEST_F(IntlTest, IntlFuncTest0014, TestSize.Level1)
|
||||
EXPECT_TRUE(false);
|
||||
return;
|
||||
}
|
||||
int64_t date[] = { 2021, 3, 14, 15, 5, 3 };
|
||||
string out = dateFormat->Format(date, 6);
|
||||
int64_t milliseconds = 123456789;
|
||||
string out = dateFormat->Format(milliseconds);
|
||||
EXPECT_TRUE(out.find(expects) != out.npos);
|
||||
delete dateFormat;
|
||||
}
|
||||
@ -1339,14 +1339,13 @@ HWTEST_F(IntlTest, IntlFuncTest0036, TestSize.Level1)
|
||||
map<string, string> options;
|
||||
DateTimeFormat *formatter = new DateTimeFormat(locales, options);
|
||||
|
||||
const size_t size = 6;
|
||||
int64_t date[size] { 2022, 11, 19, 15, 18, 24 };
|
||||
string res = formatter->Format(date, size);
|
||||
EXPECT_EQ(res, "12/19/22");
|
||||
int64_t milliseconds = 123456789;
|
||||
string res = formatter->Format(milliseconds);
|
||||
EXPECT_EQ(res, "1/2/70");
|
||||
|
||||
int64_t endDate[size] { 2023, 10, 18, 14, 17, 23 };
|
||||
res = formatter->FormatRange(date, size, endDate, size);
|
||||
EXPECT_EQ(res, "12/19/22 \xE2\x80\x93 11/18/23");
|
||||
int64_t milliseconds2 = 987654321;
|
||||
res = formatter->FormatRange(milliseconds, milliseconds2);
|
||||
EXPECT_EQ(res, "1/2/70 \xE2\x80\x93 1/12/70");
|
||||
delete formatter;
|
||||
}
|
||||
|
||||
@ -1382,14 +1381,13 @@ HWTEST_F(IntlTest, IntlFuncTest0037, TestSize.Level1)
|
||||
};
|
||||
DateTimeFormat *formatter = new DateTimeFormat(locales, options);
|
||||
|
||||
const size_t size = 6;
|
||||
int64_t date[size] { 2022, 11, 19, 15, 18, 24 };
|
||||
string res = formatter->Format(date, size);
|
||||
int64_t milliseconds = 123456789;
|
||||
string res = formatter->Format(milliseconds);
|
||||
// 2022年12月19日 GMT+8 15:18:24
|
||||
EXPECT_TRUE(res.length() > 0);
|
||||
|
||||
int64_t endDate[size] { 2023, 10, 18, 14, 17, 23 };
|
||||
res = formatter->FormatRange(date, size, endDate, size);
|
||||
int64_t milliseconds2 = 987654321;
|
||||
res = formatter->FormatRange(milliseconds, milliseconds2);
|
||||
// 2022/12/19 GMT+8 15:18:24 \xE2\x80\x93 2023/11/18 GMT+8 14:17:23
|
||||
EXPECT_TRUE(res.length() > 0);
|
||||
|
||||
@ -1491,13 +1489,12 @@ HWTEST_F(IntlTest, IntlFuncTest0039, TestSize.Level1)
|
||||
{ "formatMatcher", "basic" }
|
||||
};
|
||||
std::unique_ptr<DateTimeFormat> formatter = DateTimeFormat::CreateInstance(locales, inputOptions);
|
||||
const size_t size = 6;
|
||||
int64_t date[size] { 2022, 11, 19, 15, 18, 24 };
|
||||
string res = formatter->Format(date, size);
|
||||
int64_t milliseconds = 123456789;
|
||||
string res = formatter->Format(milliseconds);
|
||||
// Dec 19, 2022, 3:18:24 PM GMT+8
|
||||
EXPECT_TRUE(res.length() > 0);
|
||||
int64_t endDate[size] { 2023, 10, 18, 14, 17, 23 };
|
||||
res = formatter->FormatRange(date, size, endDate, size);
|
||||
int64_t milliseconds2 = 987654321;
|
||||
res = formatter->FormatRange(milliseconds, milliseconds2);
|
||||
// Dec 19, 2022, 3:18:24 PM GMT+8 \xE2\x80\x93 Nov 18, 2023, 2:17:23 PM GMT+8
|
||||
EXPECT_TRUE(res.length() > 0);
|
||||
map<string, string> options;
|
||||
|
@ -101,12 +101,7 @@ private:
|
||||
static napi_value PluralRulesConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value Select(napi_env env, napi_callback_info info);
|
||||
|
||||
static int64_t GetYear(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetMonth(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetDay(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetHour(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetMinute(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetSecond(napi_env env, napi_value *argv, int index);
|
||||
static int64_t GetMilliseconds(napi_env env, napi_value *argv, int index);
|
||||
bool InitLocaleContext(napi_env env, napi_callback_info info, const std::string localeTag,
|
||||
std::map<std::string, std::string> &map);
|
||||
bool InitDateTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
|
||||
|
@ -490,13 +490,9 @@ napi_value IntlAddon::FormatDateTime(napi_env env, napi_callback_info info)
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
|
||||
int64_t year = GetYear(env, argv, 0);
|
||||
int64_t month = GetMonth(env, argv, 0);
|
||||
int64_t day = GetDay(env, argv, 0);
|
||||
int64_t hour = GetHour(env, argv, 0);
|
||||
int64_t minute = GetMinute(env, argv, 0);
|
||||
int64_t second = GetSecond(env, argv, 0);
|
||||
if (year == -1 || month == -1 || day == -1 || hour == -1 || minute == -1 || second == -1) {
|
||||
|
||||
int64_t milliseconds = GetMilliseconds(env, argv, 0);
|
||||
if (milliseconds == -1) {
|
||||
return nullptr;
|
||||
}
|
||||
IntlAddon *obj = nullptr;
|
||||
@ -505,8 +501,7 @@ napi_value IntlAddon::FormatDateTime(napi_env env, napi_callback_info info)
|
||||
HiLog::Error(LABEL, "Get DateTimeFormat object failed");
|
||||
return nullptr;
|
||||
}
|
||||
int64_t date[] = { year, month, day, hour, minute, second };
|
||||
std::string value = obj->datefmt_->Format(date, sizeof(date) / sizeof(int64_t));
|
||||
std::string value = obj->datefmt_->Format(milliseconds);
|
||||
napi_value result = nullptr;
|
||||
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
|
||||
if (status != napi_ok) {
|
||||
@ -527,22 +522,9 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info)
|
||||
HiLog::Error(LABEL, "Parameter wrong");
|
||||
return nullptr;
|
||||
}
|
||||
int64_t firstYear = GetYear(env, argv, 0);
|
||||
int64_t firstMonth = GetMonth(env, argv, 0);
|
||||
int64_t firstDay = GetDay(env, argv, 0);
|
||||
int64_t firstHour = GetHour(env, argv, 0);
|
||||
int64_t firstMinute = GetMinute(env, argv, 0);
|
||||
int64_t firstSecond = GetSecond(env, argv, 0);
|
||||
int64_t firstDate[] = { firstYear, firstMonth, firstDay, firstHour, firstMinute, firstSecond };
|
||||
int64_t secondYear = GetYear(env, argv, 1);
|
||||
int64_t secondMonth = GetMonth(env, argv, 1);
|
||||
int64_t secondDay = GetDay(env, argv, 1);
|
||||
int64_t secondHour = GetHour(env, argv, 1);
|
||||
int64_t secondMinute = GetMinute(env, argv, 1);
|
||||
int64_t secondSecond = GetSecond(env, argv, 1);
|
||||
int64_t secondDate[] = { secondYear, secondMonth, secondDay, secondHour, secondMinute, secondSecond };
|
||||
if (firstYear == -1 || firstMonth == -1 || firstDay == -1 || firstHour == -1 || firstMinute == -1 ||
|
||||
firstSecond == -1) {
|
||||
int64_t firstMilliseconds = GetMilliseconds(env, argv, 0);
|
||||
int64_t secondMilliseconds = GetMilliseconds(env, argv, 1);
|
||||
if (firstMilliseconds == -1 || secondMilliseconds == -1) {
|
||||
return nullptr;
|
||||
}
|
||||
IntlAddon *obj = nullptr;
|
||||
@ -551,8 +533,7 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info)
|
||||
HiLog::Error(LABEL, "Get DateTimeFormat object failed");
|
||||
return nullptr;
|
||||
}
|
||||
std::string value = obj->datefmt_->FormatRange(firstDate, sizeof(firstDate) / sizeof(int64_t), secondDate,
|
||||
sizeof(secondDate) / sizeof(int64_t));
|
||||
std::string value = obj->datefmt_->FormatRange(firstMilliseconds, secondMilliseconds);
|
||||
napi_value result = nullptr;
|
||||
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
|
||||
if (status != napi_ok) {
|
||||
@ -648,142 +629,27 @@ bool IntlAddon::InitNumberFormatContext(napi_env env, napi_callback_info info, s
|
||||
return numberfmt_ != nullptr;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetYear(napi_env env, napi_value *argv, int index)
|
||||
int64_t IntlAddon::GetMilliseconds(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getFullYear", &funcGetDateInfo);
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getTime", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get year property failed");
|
||||
HiLog::Error(LABEL, "Get Milliseconds property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get year function failed");
|
||||
HiLog::Error(LABEL, "Get Milliseconds function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t year = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &year);
|
||||
int64_t milliseconds = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &milliseconds);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get year failed");
|
||||
HiLog::Error(LABEL, "Get Milliseconds failed");
|
||||
return -1;
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetMonth(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getMonth", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get month property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get month function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t month = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &month);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get month failed");
|
||||
return -1;
|
||||
}
|
||||
return month;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetDay(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getDate", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get day property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get day function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t day = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &day);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get day failed");
|
||||
return -1;
|
||||
}
|
||||
return day;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetHour(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getHours", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get hour property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get hour function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t hour = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &hour);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get hour failed");
|
||||
return -1;
|
||||
}
|
||||
return hour;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetMinute(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getMinutes", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get minute property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get minute function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t minute = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &minute);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get minute failed");
|
||||
return -1;
|
||||
}
|
||||
return minute;
|
||||
}
|
||||
|
||||
int64_t IntlAddon::GetSecond(napi_env env, napi_value *argv, int index)
|
||||
{
|
||||
napi_value funcGetDateInfo = nullptr;
|
||||
napi_status status = napi_get_named_property(env, argv[index], "getSeconds", &funcGetDateInfo);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get second property failed");
|
||||
return -1;
|
||||
}
|
||||
napi_value ret_value = nullptr;
|
||||
status = napi_call_function(env, argv[index], funcGetDateInfo, 0, nullptr, &ret_value);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get second function failed");
|
||||
return -1;
|
||||
}
|
||||
int64_t second = 0;
|
||||
status = napi_get_value_int64(env, ret_value, &second);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Get second failed");
|
||||
return -1;
|
||||
}
|
||||
return second;
|
||||
return milliseconds;
|
||||
}
|
||||
|
||||
napi_value IntlAddon::GetLanguage(napi_env env, napi_callback_info info)
|
||||
|
Loading…
x
Reference in New Issue
Block a user