date_time_format.h 5.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef OHOS_GLOBAL_I18N_DATE_TIME_FORMAT_H
#define OHOS_GLOBAL_I18N_DATE_TIME_FORMAT_H

S
sunyaozu 已提交
18 19 20 21
#include <map>
#include <vector>
#include <climits>
#include <set>
M
meaty-bag-and-wangwang-meat 已提交
22
#include "locale_info.h"
23
#include "unicode/datefmt.h"
M
meaty-bag-and-wangwang-meat 已提交
24
#include "unicode/dtptngen.h"
25
#include "unicode/localebuilder.h"
M
meaty-bag-and-wangwang-meat 已提交
26 27 28 29 30
#include "unicode/locid.h"
#include "unicode/smpdtfmt.h"
#include "unicode/timezone.h"
#include "unicode/calendar.h"
#include "unicode/numsys.h"
M
meaty-bag-and-wangwang-meat 已提交
31
#include "unicode/dtitvfmt.h"
32 33 34 35 36 37

namespace OHOS {
namespace Global {
namespace I18n {
class DateTimeFormat {
public:
M
meaty-bag-and-wangwang-meat 已提交
38
    DateTimeFormat(const std::vector<std::string> &localeTags, std::map<std::string, std::string> &configs);
39
    virtual ~DateTimeFormat();
S
sunyaozu 已提交
40 41
    std::string Format(int64_t *date, size_t size);
    std::string FormatRange(int64_t *fromDate, size_t fromDateSize, int64_t *toDate, size_t toDateSize);
M
meaty-bag-and-wangwang-meat 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    void GetResolvedOptions(std::map<std::string, std::string> &map);
    std::string GetDateStyle() const;
    std::string GetTimeStyle() const;
    std::string GetHourCycle() const;
    std::string GetTimeZone() const;
    std::string GetTimeZoneName() const;
    std::string GetNumberingSystem() const;
    std::string GetHour12() const;
    std::string GetWeekday() const;
    std::string GetEra() const;
    std::string GetYear() const;
    std::string GetMonth() const;
    std::string GetDay() const;
    std::string GetHour() const;
    std::string GetMinute() const;
    std::string GetSecond() const;
M
meaty-bag-and-wangwang-meat 已提交
58 59 60
    std::string GetDayPeriod() const;
    std::string GetLocaleMatcher() const;
    std::string GetFormatMatcher() const;
M
meaty-bag-and-wangwang-meat 已提交
61
    std::string GetFractionalSecondDigits() const;
S
sunyaozu 已提交
62 63 64
    static std::unique_ptr<DateTimeFormat> CreateInstance(const std::vector<std::string> &localeTags,
                                                          std::map<std::string, std::string> &configs);

65
private:
M
meaty-bag-and-wangwang-meat 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    std::string localeTag;
    std::string dateStyle;
    std::string timeStyle;
    std::string hourCycle;
    std::string timeZone;
    std::string numberingSystem;
    std::string hour12;
    std::string weekday;
    std::string era;
    std::string year;
    std::string month;
    std::string day;
    std::string hour;
    std::string minute;
    std::string second;
    std::string timeZoneName;
M
meaty-bag-and-wangwang-meat 已提交
82 83 84
    std::string dayPeriod;
    std::string localeMatcher;
    std::string formatMatcher;
M
meaty-bag-and-wangwang-meat 已提交
85 86 87 88
    icu::DateFormat *dateFormat = nullptr;
    icu::DateIntervalFormat *dateIntvFormat = nullptr;
    icu::Calendar *calendar = nullptr;
    LocaleInfo *localeInfo = nullptr;
M
meaty-bag-and-wangwang-meat 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    icu::Locale locale;
    icu::UnicodeString pattern;
    char16_t yearChar = 'Y';
    char16_t monthChar = 'M';
    char16_t dayChar = 'd';
    char16_t hourChar = 'h';
    char16_t minuteChar = 'm';
    char16_t secondChar = 's';
    char16_t fractionalSecondChar = 'S';
    char16_t timeZoneChar = 'z';
    char16_t weekdayChar = 'E';
    char16_t eraChar = 'G';
    char16_t amPmChar = 'a';
    std::string hourTwoDigitString = "HH";
    std::string hourNumericString = "H";
    static const int32_t NUMERIC_LENGTH = 1;
    static const int32_t TWO_DIGIT_LENGTH = 2;
    static const int32_t SHORT_LENGTH = 3;
    static const int32_t LONG_LENGTH = 4;
    static const int32_t NARROW_LENGTH = 5;
S
sunyaozu 已提交
109 110 111 112 113 114
    static const size_t YEAR_INDEX = 0;
    static const size_t MONTH_INDEX = 1;
    static const size_t DAY_INDEX = 2;
    static const size_t HOUR_INDEX = 3;
    static const size_t MINUTE_INDEX = 4;
    static const size_t SECOND_INDEX = 5;
M
meaty-bag-and-wangwang-meat 已提交
115 116 117 118
    static const int32_t SHORT_ERA_LENGTH = 1;
    static const int32_t LONG_ERA_LENGTH = 4;
    static const int HALF_HOUR = 30;
    static const int HOURS_OF_A_DAY = 24;
119 120
    static bool icuInitialized;
    static bool Init();
M
meaty-bag-and-wangwang-meat 已提交
121
    static std::map<std::string, icu::DateFormat::EStyle> dateTimeStyle;
S
sunyaozu 已提交
122 123
    void InitWithLocale(const std::string &curLocale, std::map<std::string, std::string> &configs);
    void InitWithDefaultLocale(std::map<std::string, std::string> &configs);
M
meaty-bag-and-wangwang-meat 已提交
124 125 126 127 128 129 130 131 132 133
    void ParseConfigsPartOne(std::map<std::string, std::string> &configs);
    void ParseConfigsPartTwo(std::map<std::string, std::string> &configs);
    void AddOptions(std::string option, char16_t optionChar);
    void ComputeSkeleton();
    void ComputePattern();
    void ComputePartOfPattern(std::string option, char16_t character, std::string twoDigitChar,
        std::string numericChar);
    void ComputeHourCycleChars();
    void ComputeWeekdayOrEraOfPattern(std::string option, char16_t character, std::string longChar,
        std::string shortChar, std::string narrowChar);
M
meaty-bag-and-wangwang-meat 已提交
134
    void InitDateFormatWithoutConfigs(UErrorCode &status);
M
meaty-bag-and-wangwang-meat 已提交
135 136
    void InitDateFormat(UErrorCode &status);
    void GetAdditionalResolvedOptions(std::map<std::string, std::string> &map);
S
sunyaozu 已提交
137 138 139
    void FixPatternPartOne();
    void FixPatternPartTwo();
    void removeAmPmChar();
S
sunyaozu 已提交
140
    int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size);
S
sunyaozu 已提交
141
    bool CheckInitSuccess();
142 143 144 145
};
} // namespace I18n
} // namespace Global
} // namespace OHOS
M
mamingshuai 已提交
146
#endif