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 ECMASCRIPT_JSDATE_H 17 #define ECMASCRIPT_JSDATE_H 18 19 #include <array> 20 21 #include "ecmascript/ecma_runtime_call_info.h" 22 #include "ecmascript/js_tagged_value-inl.h" 23 24 namespace panda::ecmascript { 25 static constexpr int64_t DAYS_IN_YEAR = 365; 26 static constexpr std::array<int, 2> APPROXIMATION_NUMBER = {100000, 3652425}; 27 static constexpr int64_t CHINA_BEFORE_1900_MS = -2177481943000; 28 static constexpr int64_t CHINA_1901_MS = -2177452800000; 29 static constexpr int CHINA_BEFORE_1901_ADDMS = 343000; 30 static constexpr int MS_PER_SECOND = 1000; 31 static constexpr int SEC_PER_MINUTE = 60; 32 static constexpr int SEC_PER_HOUR = 3600; 33 static constexpr int MIN_PER_HOUR = 60; 34 static constexpr int MS_PER_HOUR = 3600 * 1000; 35 static constexpr int MS_PER_DAY = 86400000; 36 static constexpr int DAY_PER_WEEK = 7; 37 static constexpr int DATE_LENGTH = 9; 38 // the index in the Date Fields 39 static constexpr uint8_t YEAR = 0; 40 static constexpr uint8_t MONTH = 1; 41 static constexpr uint8_t DAYS = 2; 42 static constexpr uint8_t HOUR = 3; 43 static constexpr uint8_t MIN = 4; 44 static constexpr uint8_t SEC = 5; 45 static constexpr uint8_t MS = 6; 46 static constexpr uint8_t WEEKDAY = 7; 47 static constexpr uint8_t TIMEZONE = 8; 48 static constexpr int CHINA_BEFORE_1901_MIN = 485; 49 static constexpr int CHINA_AFTER_1901_MIN = 480; 50 static constexpr int CHINA_BEFORE_1901_MS = 343000; 51 static constexpr std::array<int, 3> LEAP_NUMBER = {4, 100, 400}; 52 static constexpr std::array<int, 4> YEAR_NUMBER = {1970, 1969, 1901, 1601}; 53 static constexpr int DAYS_1970_TO_0000 = 719468; 54 static constexpr int DAYS_IN_4_YEARS = 1460; 55 static constexpr int DAYS_IN_100_YEARS = 36524; 56 static constexpr int DAYS_IN_400_YEARS = 146097; 57 static constexpr int DAYS_MAR_TO_DEC = 306; 58 static constexpr int DAYS_JAN_AND_FEB = 59; 59 static constexpr int MONTH_COEFFICIENT = 2; 60 static constexpr int MOUTH_PER_YEAR = 12; 61 static constexpr std::array<int, 2> COEFFICIENT_TO_CIVIL = {5, 153}; 62 static constexpr std::array<int, 3> MONTH_TRANSFORM = {3, 10, -9}; 63 static constexpr int DAYS_FEBRUARY = 28; 64 static constexpr char DEL = 127; 65 66 class DateUtils { 67 public: 68 static void TransferTimeToDate(int64_t timeMs, std::array<int64_t, DATE_LENGTH> *date); 69 static int64_t Mod(int64_t a, int b); 70 static bool IsLeap(int64_t year); 71 static int64_t GetDaysInYear(int64_t year); 72 static int64_t GetDaysFromYear(int64_t year); 73 // return the year, update days. 74 static void GetYearFromDays(std::array<int64_t, DATE_LENGTH> *date); 75 static int64_t FloorDiv(int64_t a, int64_t b); 76 77 private: 78 static bool isCached_; 79 static int preSumDays_; 80 static int preDays_; 81 static int preMonth_; 82 static int preYear_; 83 }; 84 class JSDate : public JSObject { 85 public: 86 CAST_CHECK(JSDate, IsDate); 87 88 static constexpr size_t TIME_VALUE_OFFSET = JSObject::SIZE; 89 ACCESSORS(TimeValue, TIME_VALUE_OFFSET, LOCAL_TIME_OFFSET) 90 ACCESSORS(LocalOffset, LOCAL_TIME_OFFSET, SIZE) // localoffset in min 91 92 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, TIME_VALUE_OFFSET, SIZE) 93 94 static double MakeDay(double year, double month, double date); 95 static double MakeTime(double hour, double min, double sec, double ms); 96 static double MakeDate(double day, double time); 97 static double TimeClip(double time); 98 static JSTaggedValue LocalParseStringToMs(const CString &str); 99 static JSTaggedValue UtcParseStringToMs(const CString &str); 100 static JSTaggedValue IsoParseStringToMs(const CString &str); 101 static int GetSignedNumFromString(const CString &str, int len, int *index); 102 static bool GetNumFromString(const CString &str, int len, int *index, int *num); 103 104 // 20.4.1.7 105 int64_t GetLocalOffsetInMin(const JSThread *thread, int64_t timeMs, bool isLocal); 106 107 // 20.4.1.8 108 double LocalTime(double timeMs) const; 109 110 // 20.4.1.9 111 double UTCTime(double timeMs) const; 112 113 // 20.4.3.1 114 static JSTaggedValue Now(); 115 116 // 20.4.3.2 117 static JSTaggedValue Parse(EcmaRuntimeCallInfo *argv); 118 119 // 20.4.3.4 120 static JSTaggedValue UTC(EcmaRuntimeCallInfo *argv); 121 122 // 20.4.4.10 123 JSTaggedValue GetTime() const; 124 125 // 20.4.4.19 126 JSTaggedValue GetUTCSeconds(); 127 128 // 20.4.4.35 129 JSTaggedValue ToDateString(JSThread *thread) const; 130 static CString ToDateString(double timeMs); 131 132 // 20.4.4.36 133 JSTaggedValue ToISOString(JSThread *thread) const; 134 135 // 20.4.4.41 136 JSTaggedValue ToString(JSThread *thread) const; 137 138 // 20.4.4.42 139 JSTaggedValue ToTimeString(JSThread *thread) const; 140 141 // 20.4.4.43 142 JSTaggedValue ToUTCString(JSThread *thread) const; 143 144 // 20.4.4.44 145 JSTaggedValue ValueOf() const; 146 147 JSTaggedValue SetDateValue(EcmaRuntimeCallInfo *argv, uint32_t code, bool isLocal) const; 148 double GetDateValue(double timeMs, uint8_t code, bool isLocal) const; 149 static JSTaggedValue GetTimeFromString(const char *str, int len); 150 151 static constexpr double MAX_DOUBLE = std::numeric_limits<double>::max(); 152 static constexpr double MAX_INT = std::numeric_limits<int>::max(); 153 static constexpr uint16_t NINETEEN_HUNDRED_YEAR = 1900; 154 static constexpr uint16_t THOUSAND = 1000; 155 static constexpr uint16_t HUNDRED = 100; 156 static constexpr int TEN = 10; 157 static constexpr int NUM_NINE = 9; 158 static constexpr int MONTH_PER_YEAR = 12; 159 static constexpr int MAX_DAYS_MONTH = 31; 160 static double SetDateValues(const std::array<int64_t, DATE_LENGTH> *date, bool isLocal); 161 static double SetDateValues(int64_t year, int64_t month, int64_t day); 162 static void GetDateValues(double timeMs, std::array<int64_t, DATE_LENGTH> *date, bool isLocal); 163 static CString StrToTargetLength(const CString &str, int length); 164 DECL_DUMP() 165 166 private: 167 bool GetThisDateValues(std::array<int64_t, DATE_LENGTH> *date, bool isLocal) const; 168 CString GetLocaleTimeStr(const std::array<int64_t, DATE_LENGTH> &fields) const; 169 CString GetLocaleDateStr(const std::array<int64_t, DATE_LENGTH> &fields) const; 170 static int64_t MathMod(int64_t a, int b); 171 172 static constexpr int MINUTE_PER_HOUR = 60; 173 static constexpr std::array<int, 12> MONTH_DAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 174 // NOLINTNEXTLINE(readability-braces-around-statements,bugprone-suspicious-semicolon) 175 static constexpr int DAYS_FROM_MONTH [2][13] = { 176 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, 177 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366} 178 }; 179 static constexpr int STR_LENGTH_YEAR = 4; 180 static constexpr int STR_LENGTH_OTHERS = 2; 181 static constexpr int YEAR_DELTA = 399999; 182 static constexpr int LINE_YEAR = 1970; 183 static constexpr int CENTURY = 100; 184 static constexpr char NEG = '-'; 185 static constexpr char PLUS = '+'; 186 static constexpr char SPACE = ' '; 187 static constexpr char COLON = ':'; 188 static constexpr char POINT = '.'; 189 static constexpr int LENGTH_MONTH_NAME = 3; 190 static constexpr int MS_PER_MINUTE = 60000; 191 static constexpr int64_t MAX_TIME_IN_MS = static_cast<int64_t>(864000000) * 10000000; 192 static constexpr char FLAG_TIME = 'T'; 193 static constexpr char FLAG_UTC = 'Z'; 194 static constexpr char VIRGULE = '/'; 195 static constexpr char COMMA = ','; 196 static constexpr int LENGTH_PER_TIME = 3; 197 static constexpr int MIN_LENGTH = 10; 198 static constexpr int INDEX_PLUS_NEG = 6; 199 static constexpr int ORIGIN_YEAR = 1901; 200 static constexpr uint32_t CODE_FLAG = 0x0FULL; 201 static constexpr size_t CODE_4_BIT = 4; 202 }; 203 } // namespace panda::ecmascript 204 205 #endif // ECMASCRIPT_JSDATE_H 206