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 OHOS_ACELITE_DATE_TIME_FORMAT_MODULE_H 17 #define OHOS_ACELITE_DATE_TIME_FORMAT_MODULE_H 18 19 #include "acelite_config.h" 20 #if (FEATURE_DATE_FORMAT == 1) 21 #include "js_fwk_common.h" 22 #include "date_time_format.h" 23 namespace OHOS { 24 namespace ACELite { 25 class DateTimeFormatModule final : public MemoryHeap { 26 public: 27 /** 28 * @brief register dateTimeFormat attribute and register the format method to dateTimeFormat 29 */ 30 static void Init(jerry_value_t intlHandle); 31 32 static void DeleteDateFormat(void *pointer); 33 34 static jerry_object_native_info_t GC_CALLBACK; 35 36 private: 37 constexpr static uint8_t TIME_NUMBER_LEN = 6; // the num of number in format string 38 constexpr static uint8_t MAX_NUM_LEN = 10; 39 DateTimeFormatModule(); 40 41 ~DateTimeFormatModule(); 42 43 /** 44 * @brief get the locale number form 0-9 45 * @param info the language and region object 46 * @return get the number digit result 47 */ 48 bool InitNumArray(I18N::LocaleInfo info); 49 50 enum StyleState : uint8_t { 51 SHORT, 52 NUMERIC, 53 LONG, 54 HOUR12, 55 HOUR24, 56 HOUR, 57 UNKNOWN, // the style is not set 58 ERROR // the style value is invalid 59 }; 60 61 static jerry_value_t CreateDateTimeFormat(const jerry_value_t func, 62 const jerry_value_t context, 63 const jerry_value_t args[], 64 const jerry_length_t argsNum); 65 66 static jerry_value_t Format(const jerry_value_t func, 67 const jerry_value_t context, 68 const jerry_value_t args[], 69 const jerry_length_t argsNum); 70 71 const char *SetDateStyle(jerry_value_t style); 72 73 const char *SetTimeStyle(jerry_value_t style); 74 75 /** 76 * @brief set the date time pattern 77 */ 78 bool SetDatePattern(StyleState weekdayStyle, 79 StyleState yearStyle, 80 StyleState monthStyle, 81 StyleState dayStyle); 82 83 /** 84 * @brief get the hour minute second style pattern 85 */ 86 bool GetTimePattern(StyleState hourStyle, 87 StyleState minuteStyle, 88 StyleState secondStyle); 89 90 /** 91 * @brief check the specified position in coampared string is number or not 92 * @param comapre: the compare string 93 * @param start: the specified position 94 * @return if the specified string is digit, return the last position of single number, 95 * else return -1 96 */ 97 int16_t GetNumberEnd(const char *compare, uint8_t start) const; 98 99 StyleState GetWeekdayStyle(jerry_value_t style) const; 100 101 StyleState GetStyle(jerry_value_t style, const char *attrName, const uint8_t index); 102 103 StyleState GetMonthStyle(jerry_value_t style); 104 105 /** 106 * @brief execute the function in date object 107 * @param time : the js date object 108 * funcName : the name of executed function 109 * @return the number value of execute the function 110 */ 111 double GetTimeVal(jerry_value_t time, const char *funcName) const; 112 113 /** 114 * @brief format the time to date through locale info 115 * @param time: the time need to format 116 * res : store the format date string 117 * resSize : the size of format result 118 * start: the start storage position in res 119 */ 120 void FormatDate(time_t time, char *res, const uint8_t resSize, uint8_t &start); 121 122 /** 123 * @brief get the time zone, and convert local time to GMT time 124 * @param time the timestamp of local time 125 */ 126 void ConvertLocalToGMT(time_t &time) const; 127 128 /** 129 * @brief format the time to hour-minute-second through locale info 130 * @param date: the time need to format 131 * res : store the format date string 132 * resSize : the length of format result 133 * start: the start storage position in res 134 */ 135 void FormatTime(time_t time, char *res, const uint8_t resSize, uint8_t &start); 136 137 /** 138 * @brief get the first number in string 139 * @param the string to get the first number 140 * @return if string starts with number, return the end index of first number in string 141 * else return -1 142 */ 143 int16_t GetNum(const char *format) const; 144 145 /** 146 * @brief Get the month format value 147 * @param time the js time which need to format 148 * @return the format js value 149 */ 150 jerry_value_t GetMonthVal(jerry_value_t time) const; 151 152 /** 153 * @brief format the first number to 2 digit 154 * @param time the string need to format to 2-digit or remain 155 * res the string to store the format result 156 * start the start position to store format result 157 * is2Digit if true, format the first number to 2-digit 158 * if false remain the first digit 159 * @return the next number index 160 */ 161 int16_t FormatDigit(const char *time, char *res, const uint8_t resSize, uint8_t &start, bool is2Digit) const; 162 163 void ReleaseNumArray(); 164 165 bool SetMonthPattern(StyleState monthStyle, 166 I18N::AvailableDateTimeFormatPattern widePattern, 167 I18N::AvailableDateTimeFormatPattern shortPattern, 168 I18N::AvailableDateTimeFormatPattern numberPattern); 169 170 uint8_t GetNumInDate(const char *date) const; 171 I18N::DateTimeFormat *dateFormat_; 172 I18N::LocaleInfo *info_; 173 I18N::AvailableDateTimeFormatPattern timePattern_; 174 I18N::AvailableDateTimeFormatPattern datePattern_; 175 StyleState weekStyle_; 176 StyleState monthStyle_; 177 char **numArray_; 178 bool isSetTime_; 179 bool isSetDate_; 180 bool digitArray_[TIME_NUMBER_LEN]; 181 }; 182 } 183 } 184 #endif 185 #endif // DATE_TIME_FORMAT_MODEL_H 186