1 /*
2 * Copyright (c) 2021-2022 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 #include "holiday_manager.h"
16 #include "i18n_hilog.h"
17 #include <algorithm>
18 #include <climits>
19 #include <ctime>
20 #include <filesystem>
21 #include <format>
22 #include <fstream>
23 #include <iostream>
24 #include <regex>
25 #include "map"
26 #include "utils.h"
27
28 namespace OHOS {
29 namespace Global {
30 namespace I18n {
31 const char* HolidayManager::ITEM_BEGIN_TAG = "BEGIN:VEVENT";
32 const char* HolidayManager::ITEM_END_TAG = "END:VEVENT";
33 const char* HolidayManager::ITEM_DTSTART_TAG = "DTSTART";
34 const char* HolidayManager::ITEM_DTEND_TAG = "DTEND";
35 const char* HolidayManager::ITEM_SUMMARY_TAG = "SUMMARY";
36 const char* HolidayManager::ITEM_RESOURCES_TAG = "RESOURCES";
37
HolidayManager(const char * path)38 HolidayManager::HolidayManager(const char* path)
39 {
40 if (path == nullptr) {
41 HILOG_ERROR_I18N("Failed: parameter path is NULL.");
42 return;
43 }
44 if (!CheckFilePath(path)) {
45 HILOG_ERROR_I18N("Failed: holiday file path: %{public}s invalid.", path);
46 return;
47 }
48 std::vector<HolidayInfoItem> items = ReadHolidayFile(path);
49 for (size_t i = 0; i < items.size(); i++) {
50 struct tm tmObj = {.tm_mday = items[i].day, .tm_mon = items[i].month, .tm_year = items[i].year};
51 char strDate[10];
52 size_t resCode = strftime(strDate, sizeof(strDate), "%Y%m%d", &tmObj);
53 if (resCode == 0) {
54 HILOG_ERROR_I18N("Failed: strftime error:%{public}zu.", resCode);
55 return;
56 }
57 std::string startDate(strDate);
58 items[i].year += YEAR_START;
59 items[i].month += MONTH_GREATER_ONE;
60 if (holidayItemMap.find(startDate) != holidayItemMap.end()) {
61 std::vector<HolidayInfoItem> *vetor = &(holidayItemMap.find(startDate)->second);
62 vetor->push_back(items[i]);
63 } else {
64 std::vector<HolidayInfoItem> vetor;
65 vetor.push_back(items[i]);
66 holidayItemMap.insert(std::pair<std::string, std::vector<HolidayInfoItem>>(startDate, vetor));
67 }
68 }
69 }
70
~HolidayManager()71 HolidayManager::~HolidayManager()
72 {
73 }
74
SetHolidayData(std::map<std::string,std::vector<HolidayInfoItem>> holidayDataMap)75 void HolidayManager::SetHolidayData(std::map<std::string, std::vector<HolidayInfoItem>> holidayDataMap)
76 {
77 holidayItemMap = holidayDataMap;
78 }
79
IsHoliday()80 bool HolidayManager::IsHoliday()
81 {
82 time_t timeStamp = time(NULL);
83 struct tm* timObj = localtime(&timeStamp);
84 if (timObj == nullptr) {
85 return false;
86 }
87 int32_t year = timObj->tm_year + YEAR_START;
88 int32_t month = timObj->tm_mon + MONTH_GREATER_ONE;
89 return IsHoliday(year, month, timObj->tm_mday);
90 }
91
IsHoliday(int32_t year,int32_t month,int32_t day)92 bool HolidayManager::IsHoliday(int32_t year, int32_t month, int32_t day)
93 {
94 std::string startDate = Format(year, month, day);
95 if (holidayItemMap.find(startDate) != holidayItemMap.end()) {
96 std::vector<HolidayInfoItem> list = holidayItemMap.find(startDate)->second;
97 return list.size() > 0;
98 }
99 return false;
100 }
101
Format(int32_t year,int32_t month,int32_t day)102 std::string HolidayManager::Format(int32_t year, int32_t month, int32_t day)
103 {
104 std::string formated;
105 formated += std::to_string(year);
106 // Numbers less than 10 are one digit
107 formated += month < 10 ? ("0" + std::to_string(month)) : std::to_string(month);
108 // Numbers less than 10 are one digit
109 formated += day < 10 ? ("0" + std::to_string(day)) : std::to_string(day);
110 return formated;
111 }
112
GetHolidayInfoItemArray()113 std::vector<HolidayInfoItem> HolidayManager::GetHolidayInfoItemArray()
114 {
115 time_t timeStamp = time(NULL);
116 struct tm* timObj = localtime(&timeStamp);
117 if (timObj == nullptr) {
118 std::vector<HolidayInfoItem> emptyList;
119 return emptyList;
120 }
121 int32_t realYear = timObj->tm_year + YEAR_START;
122 return GetHolidayInfoItemArray(realYear);
123 }
124
GetHolidayInfoItemArray(int32_t year)125 std::vector<HolidayInfoItem> HolidayManager::GetHolidayInfoItemArray(int32_t year)
126 {
127 std::vector<HolidayInfoItem> vetor;
128 std::string formatedYear = std::to_string(year);
129 std::map<std::string, std::vector<HolidayInfoItem>>::iterator iter;
130 for (iter = holidayItemMap.begin(); iter != holidayItemMap.end(); ++iter) {
131 std::string key = iter->first;
132 if (formatedYear == key.substr(0, 4)) { // 4 is the length of full year
133 std::vector<HolidayInfoItem> temp = iter->second;
134 for (size_t i = 0; i < temp.size(); i++) {
135 vetor.push_back(temp[i]);
136 }
137 }
138 }
139 return vetor;
140 }
141
ReadHolidayFile(const std::string & path)142 std::vector<HolidayInfoItem> HolidayManager::ReadHolidayFile(const std::string &path)
143 {
144 std::vector<HolidayInfoItem> items;
145 std::ifstream fin;
146 fin.open(path.c_str(), std::ios::in);
147 std::string line;
148 while (getline(fin, line)) {
149 line = Trim(line);
150 if (line.compare(ITEM_BEGIN_TAG) != 0) {
151 continue;
152 }
153 struct HolidayInfoItem holidayItem;
154 while (getline(fin, line)) {
155 line = Trim(line);
156 ParseFileLine(line, &(holidayItem));
157 if (line.compare(ITEM_END_TAG) == 0) {
158 items.push_back(holidayItem);
159 break;
160 }
161 }
162 }
163 fin.close();
164 return items;
165 }
166
ParseFileLine(const std::string & line,HolidayInfoItem * holidayItem)167 void HolidayManager::ParseFileLine(const std::string &line, HolidayInfoItem *holidayItem)
168 {
169 if (holidayItem == nullptr) {
170 return;
171 }
172 std::regex reg("([A-Z]+)[:;](.+)");
173 std::smatch match;
174 bool found = RegexSearchNoExcept(line, match, reg);
175 if (found) {
176 std::string tag = match[1].str();
177 std::string value = line.substr(line.find_last_of(":") + 1, line.length());
178 if (tag.compare(ITEM_DTSTART_TAG) == 0) {
179 std::string startDate = value.size() >= 8 ? value.substr(0, 8) : ""; // 8 is date formarted string length
180 if (startDate.size() == 8) {
181 struct tm timeObj;
182 strptime(startDate.c_str(), "%Y%m%d", &timeObj);
183 holidayItem->year = timeObj.tm_year;
184 holidayItem->month = timeObj.tm_mon;
185 holidayItem->day = timeObj.tm_mday;
186 }
187 } else if (tag.compare(ITEM_SUMMARY_TAG) == 0) {
188 holidayItem->baseName = value;
189 } else if (tag.compare(ITEM_RESOURCES_TAG) == 0) {
190 std::string displayName = line.substr(line.find_last_of("=") + 1, line.length());
191 std::string language = displayName.substr(0, displayName.find_first_of(":"));
192 std::string localName = displayName.substr(displayName.find_first_of(":") + 1, displayName.length());
193 transform(language.begin(), language.end(), language.begin(), ::tolower);
194 HolidayLocalName localeName = {language, PseudoLocalizationProcessor(localName)};
195 holidayItem->localNames.push_back(localeName);
196 }
197 }
198 }
199
Trim(std::string & str)200 std::string& HolidayManager::Trim(std::string &str)
201 {
202 if (str.empty()) {
203 return str;
204 }
205 str.erase(0, str.find_first_not_of(" \t"));
206 str.erase(str.find_last_not_of("\r\n\t") + 1);
207 return str;
208 }
209 } // namespace I18n
210 } // namespace Global
211 } // namespace OHOS
212