js-relative-time-format.h 3.1 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_RELATIVE_TIME_FORMAT_H_
#define V8_OBJECTS_JS_RELATIVE_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/heap/factory.h"
18
#include "src/objects/managed.h"
19
#include "src/objects/objects.h"
20 21 22 23 24 25 26
#include "unicode/uversion.h"

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

namespace U_ICU_NAMESPACE {
class RelativeDateTimeFormatter;
27
}  // namespace U_ICU_NAMESPACE
28 29 30 31

namespace v8 {
namespace internal {

32 33
#include "torque-generated/src/objects/js-relative-time-format-tq.inc"

34 35 36
class JSRelativeTimeFormat
    : public TorqueGeneratedJSRelativeTimeFormat<JSRelativeTimeFormat,
                                                 JSObject> {
37
 public:
38
  // Creates relative time format object with properties derived from input
39
  // locales and options.
40 41 42
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSRelativeTimeFormat> New(
      Isolate* isolate, Handle<Map> map, Handle<Object> locales,
      Handle<Object> options);
43

44
  V8_WARN_UNUSED_RESULT static Handle<JSObject> ResolvedOptions(
45 46
      Isolate* isolate, Handle<JSRelativeTimeFormat> format_holder);

47
  Handle<String> NumericAsString() const;
48

49
  // ecma402/#sec-Intl.RelativeTimeFormat.prototype.format
50 51 52 53
  V8_WARN_UNUSED_RESULT static MaybeHandle<String> Format(
      Isolate* isolate, Handle<Object> value_obj, Handle<Object> unit_obj,
      Handle<JSRelativeTimeFormat> format);

54
  // ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts
55
  V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> FormatToParts(
56
      Isolate* isolate, Handle<Object> value_obj, Handle<Object> unit_obj,
57
      Handle<JSRelativeTimeFormat> format);
58

59
  V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
60

61
  // RelativeTimeFormat accessors.
62
  DECL_ACCESSORS(icu_formatter, Managed<icu::RelativeDateTimeFormatter>)
63

64 65 66 67 68 69 70
  // Numeric: identifying whether numerical descriptions are always used, or
  // used only when no more specific version is available (e.g., "1 day ago" vs
  // "yesterday").
  //
  // ecma402/#sec-properties-of-intl-relativetimeformat-instances
  enum class Numeric {
    ALWAYS,  // numerical descriptions are always used ("1 day ago")
71
    AUTO     // numerical descriptions are used only when no more specific
72 73 74 75 76
             // version is available ("yesterday")
  };
  inline void set_numeric(Numeric numeric);
  inline Numeric numeric() const;

77 78 79
  // Bit positions in |flags|.
  DEFINE_TORQUE_GENERATED_JS_RELATIVE_TIME_FORMAT_FLAGS()

80 81
  static_assert(Numeric::AUTO <= NumericBit::kMax);
  static_assert(Numeric::ALWAYS <= NumericBit::kMax);
82

83
  DECL_PRINTER(JSRelativeTimeFormat)
84

85
  TQ_OBJECT_CONSTRUCTORS(JSRelativeTimeFormat)
86 87 88 89 90 91 92 93
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_