• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
6 #define V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
7 
8 #ifndef V8_INTL_SUPPORT
9 #error Internationalization is expected to be enabled.
10 #endif  // V8_INTL_SUPPORT
11 
12 #include <set>
13 #include <string>
14 
15 #include "src/base/bit-field.h"
16 #include "src/execution/isolate.h"
17 #include "src/objects/intl-objects.h"
18 #include "src/objects/managed.h"
19 #include "unicode/uversion.h"
20 
21 // Has to be the last include (doesn't have include guards):
22 #include "src/objects/object-macros.h"
23 
24 namespace U_ICU_NAMESPACE {
25 class DateIntervalFormat;
26 class Locale;
27 class SimpleDateFormat;
28 class TimeZone;
29 }  // namespace U_ICU_NAMESPACE
30 
31 namespace v8 {
32 namespace internal {
33 
34 #include "torque-generated/src/objects/js-date-time-format-tq.inc"
35 
36 class JSDateTimeFormat
37     : public TorqueGeneratedJSDateTimeFormat<JSDateTimeFormat, JSObject> {
38  public:
39   V8_WARN_UNUSED_RESULT static MaybeHandle<JSDateTimeFormat> New(
40       Isolate* isolate, Handle<Map> map, Handle<Object> locales,
41       Handle<Object> options, const char* service);
42 
43   V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> ResolvedOptions(
44       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format);
45 
46   V8_WARN_UNUSED_RESULT static Handle<String> Calendar(
47       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format);
48 
49   V8_WARN_UNUSED_RESULT static Handle<Object> TimeZone(
50       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format);
51 
52   // ecma402/#sec-unwrapdatetimeformat
53   V8_WARN_UNUSED_RESULT static MaybeHandle<JSDateTimeFormat>
54   UnwrapDateTimeFormat(Isolate* isolate, Handle<JSReceiver> format_holder);
55 
56   // Convert the options to ICU DateTimePatternGenerator skeleton.
57   static Maybe<std::string> OptionsToSkeleton(Isolate* isolate,
58                                               Handle<JSReceiver> options);
59 
60   // ecma402/#sec-datetime-format-functions
61   // DateTime Format Functions
62   V8_WARN_UNUSED_RESULT static MaybeHandle<String> DateTimeFormat(
63       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
64       Handle<Object> date);
65 
66   // ecma402/#sec-Intl.DateTimeFormat.prototype.formatToParts
67   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
68       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
69       double date_value, bool output_source);
70 
71   // ecma402/#sec-intl.datetimeformat.prototype.formatRange
72   V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatRange(
73       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
74       double x_date_value, double y_date_value);
75 
76   // ecma402/sec-Intl.DateTimeFormat.prototype.formatRangeToParts
77   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatRangeToParts(
78       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
79       double x_date_value, double y_date_value);
80 
81   // ecma-402/#sec-todatetimeoptions
82   enum class RequiredOption { kDate, kTime, kAny };
83   enum class DefaultsOption { kDate, kTime, kAll };
84   V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> ToDateTimeOptions(
85       Isolate* isolate, Handle<Object> input_options, RequiredOption required,
86       DefaultsOption defaults);
87 
88   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToLocaleDateTime(
89       Isolate* isolate, Handle<Object> date, Handle<Object> locales,
90       Handle<Object> options, RequiredOption required, DefaultsOption defaults,
91       const char* method_name);
92 
93   V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
94 
95   Handle<Object> static TimeZoneId(Isolate* isolate, const icu::TimeZone& tz);
96   std::unique_ptr<icu::TimeZone> static CreateTimeZone(const char* timezone);
97 
98   V8_EXPORT_PRIVATE static std::string CanonicalizeTimeZoneID(
99       const std::string& input);
100 
101   Handle<String> HourCycleAsString() const;
102 
103   // ecma-402/#sec-properties-of-intl-datetimeformat-instances
104   enum class DateTimeStyle { kUndefined, kFull, kLong, kMedium, kShort };
105 
106   // enum for "hourCycle" option.
107   enum class HourCycle { kUndefined, kH11, kH12, kH23, kH24 };
108 
109   inline void set_hour_cycle(HourCycle hour_cycle);
110   inline HourCycle hour_cycle() const;
111 
112   inline void set_date_style(DateTimeStyle date_style);
113   inline DateTimeStyle date_style() const;
114 
115   inline void set_time_style(DateTimeStyle time_style);
116   inline DateTimeStyle time_style() const;
117 
118   // Bit positions in |flags|.
119   DEFINE_TORQUE_GENERATED_JS_DATE_TIME_FORMAT_FLAGS()
120 
121   STATIC_ASSERT(HourCycle::kUndefined <= HourCycleBits::kMax);
122   STATIC_ASSERT(HourCycle::kH11 <= HourCycleBits::kMax);
123   STATIC_ASSERT(HourCycle::kH12 <= HourCycleBits::kMax);
124   STATIC_ASSERT(HourCycle::kH23 <= HourCycleBits::kMax);
125   STATIC_ASSERT(HourCycle::kH24 <= HourCycleBits::kMax);
126 
127   STATIC_ASSERT(DateTimeStyle::kUndefined <= DateStyleBits::kMax);
128   STATIC_ASSERT(DateTimeStyle::kFull <= DateStyleBits::kMax);
129   STATIC_ASSERT(DateTimeStyle::kLong <= DateStyleBits::kMax);
130   STATIC_ASSERT(DateTimeStyle::kMedium <= DateStyleBits::kMax);
131   STATIC_ASSERT(DateTimeStyle::kShort <= DateStyleBits::kMax);
132 
133   STATIC_ASSERT(DateTimeStyle::kUndefined <= TimeStyleBits::kMax);
134   STATIC_ASSERT(DateTimeStyle::kFull <= TimeStyleBits::kMax);
135   STATIC_ASSERT(DateTimeStyle::kLong <= TimeStyleBits::kMax);
136   STATIC_ASSERT(DateTimeStyle::kMedium <= TimeStyleBits::kMax);
137   STATIC_ASSERT(DateTimeStyle::kShort <= TimeStyleBits::kMax);
138 
139   DECL_ACCESSORS(icu_locale, Managed<icu::Locale>)
140   DECL_ACCESSORS(icu_simple_date_format, Managed<icu::SimpleDateFormat>)
141   DECL_ACCESSORS(icu_date_interval_format, Managed<icu::DateIntervalFormat>)
142 
143   DECL_BOOLEAN_ACCESSORS(alt_calendar)
144 
145   DECL_PRINTER(JSDateTimeFormat)
146 
147   TQ_OBJECT_CONSTRUCTORS(JSDateTimeFormat)
148 };
149 
150 }  // namespace internal
151 }  // namespace v8
152 
153 #include "src/objects/object-macros-undef.h"
154 
155 #endif  // V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
156