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 #ifndef OHOS_GLOBAL_I18N_DATE_TIME_FORMAT_H 16 #define OHOS_GLOBAL_I18N_DATE_TIME_FORMAT_H 17 18 #include <map> 19 #include <vector> 20 #include <climits> 21 #include <set> 22 #include "locale_info.h" 23 #include "unicode/datefmt.h" 24 #include "unicode/dtptngen.h" 25 #include "unicode/localebuilder.h" 26 #include "unicode/locid.h" 27 #include "unicode/smpdtfmt.h" 28 #include "unicode/timezone.h" 29 #include "unicode/calendar.h" 30 #include "unicode/numsys.h" 31 #include "unicode/dtitvfmt.h" 32 33 namespace OHOS { 34 namespace Global { 35 namespace I18n { 36 class DateTimeFormat { 37 public: 38 DateTimeFormat(const std::vector<std::string> &localeTags, std::map<std::string, std::string> &configs); 39 virtual ~DateTimeFormat(); 40 std::string Format(int64_t *date, size_t size); 41 std::string FormatRange(int64_t *fromDate, size_t fromDateSize, int64_t *toDate, size_t toDateSize); 42 void GetResolvedOptions(std::map<std::string, std::string> &map); 43 std::string GetDateStyle() const; 44 std::string GetTimeStyle() const; 45 std::string GetHourCycle() const; 46 std::string GetTimeZone() const; 47 std::string GetTimeZoneName() const; 48 std::string GetNumberingSystem() const; 49 std::string GetHour12() const; 50 std::string GetWeekday() const; 51 std::string GetEra() const; 52 std::string GetYear() const; 53 std::string GetMonth() const; 54 std::string GetDay() const; 55 std::string GetHour() const; 56 std::string GetMinute() const; 57 std::string GetSecond() const; 58 std::string GetDayPeriod() const; 59 std::string GetLocaleMatcher() const; 60 std::string GetFormatMatcher() const; 61 std::string GetFractionalSecondDigits() const; 62 private: 63 std::string localeTag; 64 std::string dateStyle; 65 std::string timeStyle; 66 std::string hourCycle; 67 std::string timeZone; 68 std::string numberingSystem; 69 std::string hour12; 70 std::string weekday; 71 std::string era; 72 std::string year; 73 std::string month; 74 std::string day; 75 std::string hour; 76 std::string minute; 77 std::string second; 78 std::string timeZoneName; 79 std::string dayPeriod; 80 std::string localeMatcher; 81 std::string formatMatcher; 82 icu::DateFormat *dateFormat = nullptr; 83 icu::DateIntervalFormat *dateIntvFormat = nullptr; 84 icu::Calendar *calendar = nullptr; 85 LocaleInfo *localeInfo = nullptr; 86 icu::Locale locale; 87 icu::UnicodeString pattern; 88 char16_t yearChar = 'Y'; 89 char16_t monthChar = 'M'; 90 char16_t dayChar = 'd'; 91 char16_t hourChar = 'h'; 92 char16_t minuteChar = 'm'; 93 char16_t secondChar = 's'; 94 char16_t fractionalSecondChar = 'S'; 95 char16_t timeZoneChar = 'z'; 96 char16_t weekdayChar = 'E'; 97 char16_t eraChar = 'G'; 98 char16_t amPmChar = 'a'; 99 std::string hourTwoDigitString = "HH"; 100 std::string hourNumericString = "H"; 101 static const int32_t NUMERIC_LENGTH = 1; 102 static const int32_t TWO_DIGIT_LENGTH = 2; 103 static const int32_t SHORT_LENGTH = 3; 104 static const int32_t LONG_LENGTH = 4; 105 static const int32_t NARROW_LENGTH = 5; 106 static const size_t YEAR_INDEX = 0; 107 static const size_t MONTH_INDEX = 1; 108 static const size_t DAY_INDEX = 2; 109 static const size_t HOUR_INDEX = 3; 110 static const size_t MINUTE_INDEX = 4; 111 static const size_t SECOND_INDEX = 5; 112 static const int32_t SHORT_ERA_LENGTH = 1; 113 static const int32_t LONG_ERA_LENGTH = 4; 114 static const int HALF_HOUR = 30; 115 static const int HOURS_OF_A_DAY = 24; 116 static bool icuInitialized; 117 static bool Init(); 118 static std::map<std::string, icu::DateFormat::EStyle> dateTimeStyle; 119 void InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs); 120 void InitWithDefaultLocale(std::map<std::string, std::string> &configs); 121 void ParseConfigsPartOne(std::map<std::string, std::string> &configs); 122 void ParseConfigsPartTwo(std::map<std::string, std::string> &configs); 123 void AddOptions(std::string option, char16_t optionChar); 124 void ComputeSkeleton(); 125 void ComputePattern(); 126 void ComputePartOfPattern(std::string option, char16_t character, std::string twoDigitChar, 127 std::string numericChar); 128 void ComputeHourCycleChars(); 129 void ComputeWeekdayOrEraOfPattern(std::string option, char16_t character, std::string longChar, 130 std::string shortChar, std::string narrowChar); 131 void InitDateFormatWithoutConfigs(UErrorCode &status); 132 void InitDateFormat(UErrorCode &status); 133 void GetAdditionalResolvedOptions(std::map<std::string, std::string> &map); 134 void FixPatternPartOne(); 135 void FixPatternPartTwo(); 136 void removeAmPmChar(); 137 int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size); 138 }; 139 } // namespace I18n 140 } // namespace Global 141 } // namespace OHOS 142 #endif