1 /*
2  * Copyright (c) 2021 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 "week_info.h"
17 #include <cstring>
18 #include "data_resource.h"
19 
20 using namespace OHOS::I18N;
21 
22 static constexpr uint8_t FIRST_DAY_OF_WEEK_INDEX = 0;
23 static constexpr uint8_t MINIMAL_DAYS_INFIRST_WEEK_INDEX = 1;
24 static constexpr uint8_t WEEKEND_ONSET_INDEX = 2;
25 static constexpr uint8_t WEEKEND_CEASE_INDEX = 3;
26 static constexpr uint8_t WEEK_DATA_LAST_INDEX = 4;
27 static constexpr uint8_t SINGLE_DATA_LENGTH = 2;
28 
WeekInfo(const LocaleInfo & localeInfo,I18nStatus & status)29 WeekInfo::WeekInfo(const LocaleInfo &localeInfo, I18nStatus &status)
30 {
31     locale = localeInfo;
32     Init(status);
33 }
34 
Init(I18nStatus & status)35 void WeekInfo::Init(I18nStatus &status)
36 {
37     DataResource resource(&locale);
38     bool isSuccess = resource.Init();
39     if (!isSuccess) {
40         status = IERROR;
41         return;
42     }
43     char *weekData = resource.GetString(WEEK_DATA);
44     if (weekData == nullptr) {
45         status = IERROR;
46         return;
47     }
48     ProcessWeekData(weekData, status);
49 }
50 
ProcessWeekData(const char * data,I18nStatus & status)51 void WeekInfo::ProcessWeekData(const char *data, I18nStatus &status)
52 {
53     if (data == nullptr) {
54         status = IERROR;
55         return;
56     }
57     size_t length = strlen(data);
58     if (length != SINGLE_DATA_LENGTH * WEEK_DATA_LAST_INDEX - 1) {
59         status = IERROR;
60         return;
61     }
62     firstDayOfWeek = static_cast<uint8_t>(data[FIRST_DAY_OF_WEEK_INDEX * SINGLE_DATA_LENGTH]) - '0';
63     minimalDaysInFirstWeek = static_cast<uint8_t>(data[MINIMAL_DAYS_INFIRST_WEEK_INDEX * SINGLE_DATA_LENGTH]) - '0';
64     firstDayOfWeekend = static_cast<uint8_t>(data[WEEKEND_ONSET_INDEX * SINGLE_DATA_LENGTH]) - '0';
65     lastDayOfWeekend = static_cast<uint8_t>(data[WEEKEND_CEASE_INDEX * SINGLE_DATA_LENGTH]) - '0';
66 }
67 
GetFirstDayOfWeek()68 uint8_t WeekInfo::GetFirstDayOfWeek()
69 {
70     return firstDayOfWeek;
71 }
72 
GetMinimalDaysInFirstWeek()73 uint8_t WeekInfo::GetMinimalDaysInFirstWeek()
74 {
75     return minimalDaysInFirstWeek;
76 }
77 
GetFirstDayOfWeekend()78 uint8_t WeekInfo::GetFirstDayOfWeekend()
79 {
80     return firstDayOfWeekend;
81 }
82 
GetLastDayOfWeekend()83 uint8_t WeekInfo::GetLastDayOfWeekend()
84 {
85     return lastDayOfWeekend;
86 }
87