160916893Sopenharmony_ci/*
260916893Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
360916893Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
460916893Sopenharmony_ci * you may not use this file except in compliance with the License.
560916893Sopenharmony_ci * You may obtain a copy of the License at
660916893Sopenharmony_ci *
760916893Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
860916893Sopenharmony_ci *
960916893Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1060916893Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1160916893Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1260916893Sopenharmony_ci * See the License for the specific language governing permissions and
1360916893Sopenharmony_ci * limitations under the License.
1460916893Sopenharmony_ci */
1560916893Sopenharmony_ci
1660916893Sopenharmony_ci#include "week_info.h"
1760916893Sopenharmony_ci#include <cstring>
1860916893Sopenharmony_ci#include "data_resource.h"
1960916893Sopenharmony_ci
2060916893Sopenharmony_ciusing namespace OHOS::I18N;
2160916893Sopenharmony_ci
2260916893Sopenharmony_cistatic constexpr uint8_t FIRST_DAY_OF_WEEK_INDEX = 0;
2360916893Sopenharmony_cistatic constexpr uint8_t MINIMAL_DAYS_INFIRST_WEEK_INDEX = 1;
2460916893Sopenharmony_cistatic constexpr uint8_t WEEKEND_ONSET_INDEX = 2;
2560916893Sopenharmony_cistatic constexpr uint8_t WEEKEND_CEASE_INDEX = 3;
2660916893Sopenharmony_cistatic constexpr uint8_t WEEK_DATA_LAST_INDEX = 4;
2760916893Sopenharmony_cistatic constexpr uint8_t SINGLE_DATA_LENGTH = 2;
2860916893Sopenharmony_ci
2960916893Sopenharmony_ciWeekInfo::WeekInfo(const LocaleInfo &localeInfo, I18nStatus &status)
3060916893Sopenharmony_ci{
3160916893Sopenharmony_ci    locale = localeInfo;
3260916893Sopenharmony_ci    Init(status);
3360916893Sopenharmony_ci}
3460916893Sopenharmony_ci
3560916893Sopenharmony_civoid WeekInfo::Init(I18nStatus &status)
3660916893Sopenharmony_ci{
3760916893Sopenharmony_ci    DataResource resource(&locale);
3860916893Sopenharmony_ci    bool isSuccess = resource.Init();
3960916893Sopenharmony_ci    if (!isSuccess) {
4060916893Sopenharmony_ci        status = IERROR;
4160916893Sopenharmony_ci        return;
4260916893Sopenharmony_ci    }
4360916893Sopenharmony_ci    char *weekData = resource.GetString(WEEK_DATA);
4460916893Sopenharmony_ci    if (weekData == nullptr) {
4560916893Sopenharmony_ci        status = IERROR;
4660916893Sopenharmony_ci        return;
4760916893Sopenharmony_ci    }
4860916893Sopenharmony_ci    ProcessWeekData(weekData, status);
4960916893Sopenharmony_ci}
5060916893Sopenharmony_ci
5160916893Sopenharmony_civoid WeekInfo::ProcessWeekData(const char *data, I18nStatus &status)
5260916893Sopenharmony_ci{
5360916893Sopenharmony_ci    if (data == nullptr) {
5460916893Sopenharmony_ci        status = IERROR;
5560916893Sopenharmony_ci        return;
5660916893Sopenharmony_ci    }
5760916893Sopenharmony_ci    size_t length = strlen(data);
5860916893Sopenharmony_ci    if (length != SINGLE_DATA_LENGTH * WEEK_DATA_LAST_INDEX - 1) {
5960916893Sopenharmony_ci        status = IERROR;
6060916893Sopenharmony_ci        return;
6160916893Sopenharmony_ci    }
6260916893Sopenharmony_ci    firstDayOfWeek = static_cast<uint8_t>(data[FIRST_DAY_OF_WEEK_INDEX * SINGLE_DATA_LENGTH]) - '0';
6360916893Sopenharmony_ci    minimalDaysInFirstWeek = static_cast<uint8_t>(data[MINIMAL_DAYS_INFIRST_WEEK_INDEX * SINGLE_DATA_LENGTH]) - '0';
6460916893Sopenharmony_ci    firstDayOfWeekend = static_cast<uint8_t>(data[WEEKEND_ONSET_INDEX * SINGLE_DATA_LENGTH]) - '0';
6560916893Sopenharmony_ci    lastDayOfWeekend = static_cast<uint8_t>(data[WEEKEND_CEASE_INDEX * SINGLE_DATA_LENGTH]) - '0';
6660916893Sopenharmony_ci}
6760916893Sopenharmony_ci
6860916893Sopenharmony_ciuint8_t WeekInfo::GetFirstDayOfWeek()
6960916893Sopenharmony_ci{
7060916893Sopenharmony_ci    return firstDayOfWeek;
7160916893Sopenharmony_ci}
7260916893Sopenharmony_ci
7360916893Sopenharmony_ciuint8_t WeekInfo::GetMinimalDaysInFirstWeek()
7460916893Sopenharmony_ci{
7560916893Sopenharmony_ci    return minimalDaysInFirstWeek;
7660916893Sopenharmony_ci}
7760916893Sopenharmony_ci
7860916893Sopenharmony_ciuint8_t WeekInfo::GetFirstDayOfWeekend()
7960916893Sopenharmony_ci{
8060916893Sopenharmony_ci    return firstDayOfWeekend;
8160916893Sopenharmony_ci}
8260916893Sopenharmony_ci
8360916893Sopenharmony_ciuint8_t WeekInfo::GetLastDayOfWeekend()
8460916893Sopenharmony_ci{
8560916893Sopenharmony_ci    return lastDayOfWeekend;
8660916893Sopenharmony_ci}
87