• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "date_time_data.h"
17 #include <cstring>
18 #include "str_util.h"
19 
20 using namespace OHOS::I18N;
21 
22 using namespace std;
23 
DateTimeData(const char * amPmMarkers,const char * configs,const int size)24 DateTimeData::DateTimeData(const char *amPmMarkers, const char *configs, const int size)
25 {
26     if (amPmMarkers != nullptr) {
27         size_t len = strlen(const_cast<char*>(amPmMarkers));
28         if (len > 0) {
29             this->amPmMarkers = NewArrayAndCopy(amPmMarkers, len);
30         }
31     }
32     // size must >= 2, The first 2 element of configs need to be extracted, the first element
33     // is the time separator and the second is the default hour.
34     if (configs && size >= CONFIG_MIN_SIZE) { // 2 is the size of configs need to be extracted
35         timeSeparator = configs[0];
36         defaultHour = configs[1];
37     }
38 }
39 
GetMonthName(int32_t index,DateTimeDataType type)40 string DateTimeData::GetMonthName(int32_t index, DateTimeDataType type)
41 {
42     if ((index < 0) || (index >= MONTH_SIZE)) {
43         return "";
44     }
45     switch (type) {
46         case DateTimeDataType::FORMAT_ABBR: {
47             return Parse(formatAbbreviatedMonthNames, index);
48         }
49         case DateTimeDataType::FORMAT_WIDE: {
50             return Parse(formatWideMonthNames, index);
51         }
52         case DateTimeDataType::STANDALONE_ABBR: {
53             return Parse(standaloneAbbreviatedMonthNames, index);
54         }
55         default: {
56             return Parse(standaloneWideMonthNames, index);
57         }
58     }
59 }
60 
GetDayName(int32_t index,DateTimeDataType type)61 string DateTimeData::GetDayName(int32_t index, DateTimeDataType type)
62 {
63     if ((index < 0) || (index >= DAY_SIZE)) {
64         return "";
65     }
66     switch (type) {
67         case DateTimeDataType::FORMAT_ABBR: {
68             return Parse(formatAbbreviatedDayNames, index);
69         }
70         case DateTimeDataType::FORMAT_WIDE: {
71             return Parse(formatWideDayNames, index);
72         }
73         case DateTimeDataType::STANDALONE_ABBR: {
74             return Parse(standaloneAbbreviatedDayNames, index);
75         }
76         default: {
77             return Parse(standaloneWideDayNames, index);
78         }
79     }
80 }
81 
GetAmPmMarker(int32_t index,DateTimeDataType type)82 string DateTimeData::GetAmPmMarker(int32_t index, DateTimeDataType type)
83 {
84     if ((index < 0) || (index >= AM_SIZE) || amPmMarkers == nullptr) {
85         return "";
86     }
87     return (strlen(amPmMarkers) > 0) ? Parse(amPmMarkers, index) : "";
88 }
89 
GetTimeSeparator(void) const90 char DateTimeData::GetTimeSeparator(void) const
91 {
92     return timeSeparator;
93 }
94 
GetDefaultHour(void) const95 char DateTimeData::GetDefaultHour(void) const
96 {
97     return defaultHour;
98 }
99 
~DateTimeData()100 DateTimeData::~DateTimeData()
101 {
102     I18nFree(static_cast<void *>(formatAbbreviatedMonthNames));
103     I18nFree(static_cast<void *>(formatWideMonthNames));
104     I18nFree(static_cast<void *>(standaloneAbbreviatedMonthNames));
105     I18nFree(static_cast<void *>(standaloneWideMonthNames));
106     I18nFree(static_cast<void *>(formatAbbreviatedDayNames));
107     I18nFree(static_cast<void *>(formatWideDayNames));
108     I18nFree(static_cast<void *>(standaloneAbbreviatedDayNames));
109     I18nFree(static_cast<void *>(standaloneWideDayNames));
110     I18nFree(static_cast<void *>(timePatterns));
111     I18nFree(static_cast<void *>(datePatterns));
112     I18nFree(static_cast<void *>(amPmMarkers));
113     I18nFree(static_cast<void *>(hourMinuteSecondPatterns));
114     I18nFree(static_cast<void *>(fullMediumShortPatterns));
115     I18nFree(static_cast<void *>(elapsedPatterns));
116 }
117 
SetMonthNamesData(const char * formatAbbreviatedMonthNames,const char * formatWideMonthNames,const char * standaloneAbbreviatedMonthNames,const char * standaloneWideMonthNames)118 void DateTimeData::SetMonthNamesData(const char *formatAbbreviatedMonthNames, const char *formatWideMonthNames,
119     const char *standaloneAbbreviatedMonthNames, const char *standaloneWideMonthNames)
120 {
121     if ((formatAbbreviatedMonthNames == nullptr) || (formatWideMonthNames == nullptr) ||
122         (standaloneAbbreviatedMonthNames == nullptr) || (standaloneWideMonthNames == nullptr)) {
123         return;
124     }
125     this->formatAbbreviatedMonthNames = NewArrayAndCopy(formatAbbreviatedMonthNames,
126         strlen(formatAbbreviatedMonthNames));
127     this->formatWideMonthNames = NewArrayAndCopy(formatWideMonthNames, strlen(formatWideMonthNames));
128     this->standaloneAbbreviatedMonthNames = NewArrayAndCopy(standaloneAbbreviatedMonthNames,
129         strlen(standaloneAbbreviatedMonthNames));
130     this->standaloneWideMonthNames = NewArrayAndCopy(standaloneWideMonthNames, strlen(standaloneWideMonthNames));
131 }
132 
SetDayNamesData(const char * formatAbbreviatedDayNames,const char * formatWideDayNames,const char * standaloneAbbreviatedDayNames,const char * standaloneWideDayNames)133 void DateTimeData::SetDayNamesData(const char *formatAbbreviatedDayNames, const char *formatWideDayNames,
134     const char *standaloneAbbreviatedDayNames, const char *standaloneWideDayNames)
135 {
136     if ((formatAbbreviatedDayNames == nullptr) || (formatWideDayNames == nullptr) ||
137         (standaloneAbbreviatedDayNames == nullptr) || (standaloneWideDayNames == nullptr)) {
138         return;
139     }
140     this->formatAbbreviatedDayNames = NewArrayAndCopy(formatAbbreviatedDayNames,
141         strlen(formatAbbreviatedDayNames));
142     this->formatWideDayNames = NewArrayAndCopy(formatWideDayNames, strlen(formatWideDayNames));
143     this->standaloneAbbreviatedDayNames = NewArrayAndCopy(standaloneAbbreviatedDayNames,
144         strlen(standaloneAbbreviatedDayNames));
145     this->standaloneWideDayNames = NewArrayAndCopy(standaloneWideDayNames, strlen(standaloneWideDayNames));
146 }
147 
SetPatternsData(const char * datePatterns,const char * timePatterns,const char * hourMinuteSecondPatterns,const char * fullMediumShortPatterns,const char * elapsedPatterns)148 void DateTimeData::SetPatternsData(const char *datePatterns, const char *timePatterns,
149     const char *hourMinuteSecondPatterns, const char *fullMediumShortPatterns, const char *elapsedPatterns)
150 {
151     if ((datePatterns == nullptr) || (timePatterns == nullptr) ||
152         (hourMinuteSecondPatterns == nullptr) || (fullMediumShortPatterns == nullptr) ||
153         (elapsedPatterns == nullptr)) {
154         return;
155     }
156     size_t timeLength = strlen(timePatterns);
157     size_t dateLength = strlen(datePatterns);
158     size_t hourLength = strlen(hourMinuteSecondPatterns);
159     size_t fullLength = strlen(fullMediumShortPatterns);
160     size_t elapsedLength = strlen(elapsedPatterns);
161     if ((timeLength == 0) || (dateLength == 0) || (hourLength == 0) || (fullLength == 0)) {
162         return;
163     }
164     I18nFree(static_cast<void *>(this->timePatterns));
165     this->timePatterns = NewArrayAndCopy(timePatterns, timeLength);
166     I18nFree(static_cast<void *>(this->datePatterns));
167     this->datePatterns = NewArrayAndCopy(datePatterns, dateLength);
168     I18nFree(static_cast<void *>(this->hourMinuteSecondPatterns));
169     this->hourMinuteSecondPatterns = NewArrayAndCopy(hourMinuteSecondPatterns, hourLength);
170     I18nFree(static_cast<void *>(this->fullMediumShortPatterns));
171     this->fullMediumShortPatterns = NewArrayAndCopy(fullMediumShortPatterns, fullLength);
172     I18nFree(static_cast<void *>(this->elapsedPatterns));
173     this->elapsedPatterns = NewArrayAndCopy(elapsedPatterns, elapsedLength);
174 }
175