js-date-time-format.h 5.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif  // V8_INTL_SUPPORT

#ifndef V8_OBJECTS_JS_DATE_TIME_FORMAT_H_
#define V8_OBJECTS_JS_DATE_TIME_FORMAT_H_

12 13 14
#include <set>
#include <string>

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-tq.h"
20
#include "unicode/uversion.h"
21 22 23 24 25

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace U_ICU_NAMESPACE {
26
class DateIntervalFormat;
27
class Locale;
28
class SimpleDateFormat;
29
}  // namespace U_ICU_NAMESPACE
30 31 32 33

namespace v8 {
namespace internal {

34 35
class JSDateTimeFormat
    : public TorqueGeneratedJSDateTimeFormat<JSDateTimeFormat, JSObject> {
36
 public:
37 38
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSDateTimeFormat> New(
      Isolate* isolate, Handle<Map> map, Handle<Object> locales,
39
      Handle<Object> options, const char* service);
40

41
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> ResolvedOptions(
42
      Isolate* isolate, Handle<JSDateTimeFormat> date_time_format);
43

44
  // ecma402/#sec-unwrapdatetimeformat
45 46
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSDateTimeFormat>
  UnwrapDateTimeFormat(Isolate* isolate, Handle<JSReceiver> format_holder);
47

48 49 50 51
  // Convert the options to ICU DateTimePatternGenerator skeleton.
  static Maybe<std::string> OptionsToSkeleton(Isolate* isolate,
                                              Handle<JSReceiver> options);

52 53 54
  // ecma402/#sec-datetime-format-functions
  // DateTime Format Functions
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> DateTimeFormat(
55
      Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
56 57
      Handle<Object> date);

58 59
  // ecma402/#sec-Intl.DateTimeFormat.prototype.formatToParts
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
60 61 62
      Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
      double date_value);

63 64 65 66 67 68 69 70 71 72
  // ecma402/#sec-intl.datetimeformat.prototype.formatRange
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> FormatRange(
      Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
      double x_date_value, double y_date_value);

  // ecma402/sec-Intl.DateTimeFormat.prototype.formatRangeToParts
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatRangeToParts(
      Isolate* isolate, Handle<JSDateTimeFormat> date_time_format,
      double x_date_value, double y_date_value);

73
  // ecma-402/#sec-todatetimeoptions
74 75
  enum class RequiredOption { kDate, kTime, kAny };
  enum class DefaultsOption { kDate, kTime, kAll };
76
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSObject> ToDateTimeOptions(
77 78
      Isolate* isolate, Handle<Object> input_options, RequiredOption required,
      DefaultsOption defaults);
79 80 81

  V8_WARN_UNUSED_RESULT static MaybeHandle<String> ToLocaleDateTime(
      Isolate* isolate, Handle<Object> date, Handle<Object> locales,
82 83
      Handle<Object> options, RequiredOption required, DefaultsOption defaults,
      const char* method);
84

85
  V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
86

87
  Handle<String> HourCycleAsString() const;
88

89 90 91
  // ecma-402/#sec-properties-of-intl-datetimeformat-instances
  enum class DateTimeStyle { kUndefined, kFull, kLong, kMedium, kShort };

92 93 94 95 96
  // enum for "hourCycle" option.
  enum class HourCycle { kUndefined, kH11, kH12, kH23, kH24 };

  inline void set_hour_cycle(HourCycle hour_cycle);
  inline HourCycle hour_cycle() const;
97

98 99 100 101 102 103
  inline void set_date_style(DateTimeStyle date_style);
  inline DateTimeStyle date_style() const;

  inline void set_time_style(DateTimeStyle time_style);
  inline DateTimeStyle time_style() const;

104 105
  // Bit positions in |flags|.
  DEFINE_TORQUE_GENERATED_JS_DATE_TIME_FORMAT_FLAGS()
106

107 108 109 110 111
  STATIC_ASSERT(HourCycle::kUndefined <= HourCycleBits::kMax);
  STATIC_ASSERT(HourCycle::kH11 <= HourCycleBits::kMax);
  STATIC_ASSERT(HourCycle::kH12 <= HourCycleBits::kMax);
  STATIC_ASSERT(HourCycle::kH23 <= HourCycleBits::kMax);
  STATIC_ASSERT(HourCycle::kH24 <= HourCycleBits::kMax);
112

113 114 115 116 117 118 119 120 121 122 123 124
  STATIC_ASSERT(DateTimeStyle::kUndefined <= DateStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kFull <= DateStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kLong <= DateStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kMedium <= DateStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kShort <= DateStyleBits::kMax);

  STATIC_ASSERT(DateTimeStyle::kUndefined <= TimeStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kFull <= TimeStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kLong <= TimeStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kMedium <= TimeStyleBits::kMax);
  STATIC_ASSERT(DateTimeStyle::kShort <= TimeStyleBits::kMax);

125 126
  DECL_ACCESSORS(icu_locale, Managed<icu::Locale>)
  DECL_ACCESSORS(icu_simple_date_format, Managed<icu::SimpleDateFormat>)
127
  DECL_ACCESSORS(icu_date_interval_format, Managed<icu::DateIntervalFormat>)
128

129 130
  DECL_PRINTER(JSDateTimeFormat)

131
  TQ_OBJECT_CONSTRUCTORS(JSDateTimeFormat)
132 133 134 135 136 137 138 139
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_JS_DATE_TIME_FORMAT_H_