• 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_INTL_SUPPORT
6 #error Internationalization is expected to be enabled.
7 #endif  // V8_INTL_SUPPORT
8 
9 #ifndef V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
10 #define V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
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 "torque-generated/field-offsets.h"
20 #include "unicode/uversion.h"
21 
22 // Has to be the last include (doesn't have include guards):
23 #include "src/objects/object-macros.h"
24 
25 namespace U_ICU_NAMESPACE {
26 class DateIntervalFormat;
27 class Locale;
28 class SimpleDateFormat;
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   // ecma402/#sec-unwrapdatetimeformat
47   V8_WARN_UNUSED_RESULT static MaybeHandle<JSDateTimeFormat>
48   UnwrapDateTimeFormat(Isolate* isolate, Handle<JSReceiver> format_holder);
49 
50   // Convert the options to ICU DateTimePatternGenerator skeleton.
51   static Maybe<std::string> OptionsToSkeleton(Isolate* isolate,
52                                               Handle<JSReceiver> options);
53 
54   // ecma402/#sec-datetime-format-functions
55   // DateTime Format Functions
56   V8_WARN_UNUSED_RESULT static MaybeHandle<String> DateTimeFormat(
57       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
58       Handle<Object> date);
59 
60   // ecma402/#sec-Intl.DateTimeFormat.prototype.formatToParts
61   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
62       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
63       double date_value);
64 
65   // ecma402/#sec-intl.datetimeformat.prototype.formatRange
66   V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatRange(
67       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
68       double x_date_value, double y_date_value);
69 
70   // ecma402/sec-Intl.DateTimeFormat.prototype.formatRangeToParts
71   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatRangeToParts(
72       Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
73       double x_date_value, double y_date_value);
74 
75   // ecma-402/#sec-todatetimeoptions
76   enum class RequiredOption { kDate, kTime, kAny };
77   enum class DefaultsOption { kDate, kTime, kAll };
78   V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> ToDateTimeOptions(
79       Isolate* isolate, Handle<Object> input_options, RequiredOption required,
80       DefaultsOption defaults);
81 
82   V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToLocaleDateTime(
83       Isolate* isolate, Handle<Object> date, Handle<Object> locales,
84       Handle<Object> options, RequiredOption required, DefaultsOption defaults,
85       const char* method);
86 
87   V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
88 
89   Handle<String> HourCycleAsString() const;
90 
91   // ecma-402/#sec-properties-of-intl-datetimeformat-instances
92   enum class DateTimeStyle { kUndefined, kFull, kLong, kMedium, kShort };
93 
94   // enum for "hourCycle" option.
95   enum class HourCycle { kUndefined, kH11, kH12, kH23, kH24 };
96 
97   inline void set_hour_cycle(HourCycle hour_cycle);
98   inline HourCycle hour_cycle() const;
99 
100   inline void set_date_style(DateTimeStyle date_style);
101   inline DateTimeStyle date_style() const;
102 
103   inline void set_time_style(DateTimeStyle time_style);
104   inline DateTimeStyle time_style() const;
105 
106   // Bit positions in |flags|.
107   DEFINE_TORQUE_GENERATED_JS_DATE_TIME_FORMAT_FLAGS()
108 
109   STATIC_ASSERT(HourCycle::kUndefined <= HourCycleBits::kMax);
110   STATIC_ASSERT(HourCycle::kH11 <= HourCycleBits::kMax);
111   STATIC_ASSERT(HourCycle::kH12 <= HourCycleBits::kMax);
112   STATIC_ASSERT(HourCycle::kH23 <= HourCycleBits::kMax);
113   STATIC_ASSERT(HourCycle::kH24 <= HourCycleBits::kMax);
114 
115   STATIC_ASSERT(DateTimeStyle::kUndefined <= DateStyleBits::kMax);
116   STATIC_ASSERT(DateTimeStyle::kFull <= DateStyleBits::kMax);
117   STATIC_ASSERT(DateTimeStyle::kLong <= DateStyleBits::kMax);
118   STATIC_ASSERT(DateTimeStyle::kMedium <= DateStyleBits::kMax);
119   STATIC_ASSERT(DateTimeStyle::kShort <= DateStyleBits::kMax);
120 
121   STATIC_ASSERT(DateTimeStyle::kUndefined <= TimeStyleBits::kMax);
122   STATIC_ASSERT(DateTimeStyle::kFull <= TimeStyleBits::kMax);
123   STATIC_ASSERT(DateTimeStyle::kLong <= TimeStyleBits::kMax);
124   STATIC_ASSERT(DateTimeStyle::kMedium <= TimeStyleBits::kMax);
125   STATIC_ASSERT(DateTimeStyle::kShort <= TimeStyleBits::kMax);
126 
127   DECL_ACCESSORS(icu_locale, Managed<icu::Locale>)
128   DECL_ACCESSORS(icu_simple_date_format, Managed<icu::SimpleDateFormat>)
129   DECL_ACCESSORS(icu_date_interval_format, Managed<icu::DateIntervalFormat>)
130 
131   DECL_PRINTER(JSDateTimeFormat)
132 
133   TQ_OBJECT_CONSTRUCTORS(JSDateTimeFormat)
134 };
135 
136 }  // namespace internal
137 }  // namespace v8
138 
139 #include "src/objects/object-macros-undef.h"
140 
141 #endif  // V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
142