| 1 | /* |
| 2 | * Copyright (C) 2015 Andy VanWagoner (andy@vanwagoner.family) |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(INTL) |
| 29 | |
| 30 | #include "JSDestructibleObject.h" |
| 31 | #include <unicode/udat.h> |
| 32 | #include <unicode/uvernum.h> |
| 33 | |
| 34 | #define JSC_ICU_HAS_UFIELDPOSITER (U_ICU_VERSION_MAJOR_NUM >= 55) |
| 35 | |
| 36 | namespace JSC { |
| 37 | |
| 38 | class IntlDateTimeFormatConstructor; |
| 39 | class JSBoundFunction; |
| 40 | |
| 41 | class IntlDateTimeFormat final : public JSDestructibleObject { |
| 42 | public: |
| 43 | typedef JSDestructibleObject Base; |
| 44 | |
| 45 | static IntlDateTimeFormat* create(VM&, Structure*); |
| 46 | static Structure* createStructure(VM&, JSGlobalObject*, JSValue); |
| 47 | |
| 48 | DECLARE_INFO; |
| 49 | |
| 50 | void initializeDateTimeFormat(ExecState&, JSValue locales, JSValue options); |
| 51 | JSValue format(ExecState&, double value); |
| 52 | #if JSC_ICU_HAS_UFIELDPOSITER |
| 53 | JSValue formatToParts(ExecState&, double value); |
| 54 | #endif |
| 55 | JSObject* resolvedOptions(ExecState&); |
| 56 | |
| 57 | JSBoundFunction* boundFormat() const { return m_boundFormat.get(); } |
| 58 | void setBoundFormat(VM&, JSBoundFunction*); |
| 59 | |
| 60 | protected: |
| 61 | IntlDateTimeFormat(VM&, Structure*); |
| 62 | void finishCreation(VM&); |
| 63 | static void destroy(JSCell*); |
| 64 | static void visitChildren(JSCell*, SlotVisitor&); |
| 65 | |
| 66 | private: |
| 67 | enum class Weekday : uint8_t { None, Narrow, Short, Long }; |
| 68 | enum class Era : uint8_t { None, Narrow, Short, Long }; |
| 69 | enum class Year : uint8_t { None, TwoDigit, Numeric }; |
| 70 | enum class Month : uint8_t { None, TwoDigit, Numeric, Narrow, Short, Long }; |
| 71 | enum class Day : uint8_t { None, TwoDigit, Numeric }; |
| 72 | enum class Hour : uint8_t { None, TwoDigit, Numeric }; |
| 73 | enum class Minute : uint8_t { None, TwoDigit, Numeric }; |
| 74 | enum class Second : uint8_t { None, TwoDigit, Numeric }; |
| 75 | enum class TimeZoneName : uint8_t { None, Short, Long }; |
| 76 | |
| 77 | struct UDateFormatDeleter { |
| 78 | void operator()(UDateFormat*) const; |
| 79 | }; |
| 80 | |
| 81 | void setFormatsFromPattern(const StringView&); |
| 82 | static ASCIILiteral weekdayString(Weekday); |
| 83 | static ASCIILiteral eraString(Era); |
| 84 | static ASCIILiteral yearString(Year); |
| 85 | static ASCIILiteral monthString(Month); |
| 86 | static ASCIILiteral dayString(Day); |
| 87 | static ASCIILiteral hourString(Hour); |
| 88 | static ASCIILiteral minuteString(Minute); |
| 89 | static ASCIILiteral secondString(Second); |
| 90 | static ASCIILiteral timeZoneNameString(TimeZoneName); |
| 91 | |
| 92 | WriteBarrier<JSBoundFunction> m_boundFormat; |
| 93 | std::unique_ptr<UDateFormat, UDateFormatDeleter> m_dateFormat; |
| 94 | |
| 95 | String m_locale; |
| 96 | String m_calendar; |
| 97 | String m_numberingSystem; |
| 98 | String m_timeZone; |
| 99 | String m_hourCycle; |
| 100 | Weekday m_weekday { Weekday::None }; |
| 101 | Era m_era { Era::None }; |
| 102 | Year m_year { Year::None }; |
| 103 | Month m_month { Month::None }; |
| 104 | Day m_day { Day::None }; |
| 105 | Hour m_hour { Hour::None }; |
| 106 | Minute m_minute { Minute::None }; |
| 107 | Second m_second { Second::None }; |
| 108 | TimeZoneName m_timeZoneName { TimeZoneName::None }; |
| 109 | bool m_initializedDateTimeFormat { false }; |
| 110 | |
| 111 | #if JSC_ICU_HAS_UFIELDPOSITER |
| 112 | struct UFieldPositionIteratorDeleter { |
| 113 | void operator()(UFieldPositionIterator*) const; |
| 114 | }; |
| 115 | |
| 116 | static ASCIILiteral partTypeString(UDateFormatField); |
| 117 | #endif |
| 118 | }; |
| 119 | |
| 120 | } // namespace JSC |
| 121 | |
| 122 | #endif // ENABLE(INTL) |
| 123 | |