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 #ifndef DATE_TIME_FORMAT_IMPL_H
17 #define DATE_TIME_FORMAT_IMPL_H
18 
19 #include <ctype.h>
20 #include <string>
21 #include "types.h"
22 #include "time.h"
23 #include "locale_info.h"
24 #include "number_format_impl.h"
25 #include "data_resource.h"
26 #include "date_time_data.h"
27 #include "str_util.h"
28 
29 #define YEAR_START 1900
30 #define LENGTH_HOUR 12
31 #define MAX_COUNT 10
32 #define SECONDS_IN_HOUR 3600
33 #define SECONDS_IN_MINUTE 60
34 #define DECIMAL_COUNT 10
35 #define ABB_COUNT 3
36 #define WIDE_COUNT 4
37 #define SHORT_YEAR_FORMAT_COUNT 2
38 #define ONE_HUNDRED_YEAR 100
39 #define QUOTE '\''
40 #define AM_PM_MAX_LENGTH 2
41 #define CONSTANT_TIME_NUMBER 10
42 #define SECOND_IN_MILLIS 1000
43 #define MINUTE_IN_MILLIS 60000
44 #define HOUR_IN_MILLIS 3600000
45 
46 namespace OHOS {
47 namespace I18N {
48 struct ElapsedTime {
49     int32_t hours;
50     int32_t minutes;
51     int32_t seconds;
52     int32_t milliseconds;
53 };
54 class DateTimeFormatImpl {
55 public:
56     DateTimeFormatImpl(AvailableDateTimeFormatPattern requestpattern, const LocaleInfo &locale);
57     virtual ~DateTimeFormatImpl();
58     bool Init(const DataResource &resource);
59     void Format(const time_t &cal, const std::string &zoneInfo, std::string &apependTo, I18nStatus &status) const;
60     void ApplyPattern(const AvailableDateTimeFormatPattern &pattern);
61     LocaleInfo GetLocale();
62     std::string GetWeekName(const int32_t &index, DateTimeDataType type) const;
63     std::string GetMonthName(const int32_t &index, DateTimeDataType type) const;
64     std::string GetAmPmMarker(const int32_t &index, DateTimeDataType type = DateTimeDataType::STANDALONE_ABBR) const;
GetFPattern()65     std::string GetFPattern()
66     {
67         return fPattern;
68     }
SetNumberFormatter(NumberFormatImpl * numberFormat)69     void SetNumberFormatter(NumberFormatImpl *numberFormat)
70     {
71         this->numberFormat = numberFormat;
72     }
73     int8_t Get12HourTimeWithoutAmpm(const time_t &cal, const std::string &zoneInfo,
74         std::string &appendTo, I18nStatus &status) const;
75     std::string FormatElapsedDuration(int32_t milliseconds, ElapsedPatternType type, I18nStatus &status) const;
76     std::string GetTimeSeparator();
77 private:
78     void FreeResource();
79     void Format(const struct tm &time, const std::string &pattern, std::string &appendTo, I18nStatus &status) const;
80     void ZeroPadding(std::string &appendTo, uint32_t minValue, uint32_t maxValue, int32_t value) const;
81     void Process(const tm &time, std::string &append, char pre, uint32_t count,  I18nStatus &status) const;
82     void ProcessTime(const tm &time, std::string &append, char pre, uint32_t count, I18nStatus &status) const;
83     void ProcessWeekDayYear(const tm &time, std::string &appendTo, char pre,
84         uint32_t count,  I18nStatus &status) const;
85     bool IsTimeChar(char ch) const;
86     int32_t ParseZoneInfo(const std::string &zoneInfo) const;
87     char *GetNoAmPmPattern(const std::string &patternString, int8_t &ret) const;
88     void FormatElapsed(const struct ElapsedTime &time, char pre, uint32_t count, std::string &appendTo,
89         I18nStatus &status) const;
90     std::string FormatNumber(int32_t value) const;
91     std::string FormatYear(int32_t value) const;
92     std::string GetZero() const;
93     uint32_t GetLength(int32_t value) const;
94     std::string AddSeconds(const std::string &hmPattern) const;
95     AvailableDateTimeFormatPattern requestPattern;
96     DateTimeData *data = nullptr;
97     NumberFormatImpl *numberFormat = nullptr;
98     std::string fPattern = "";
99     LocaleInfo fLocale;
100 };
101 } // namespace I18N
102 } // namespace OHOS
103 #endif
104