js-relative-time-format.h 3 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 27 28 29 30 31
#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;
}

namespace v8 {
namespace internal {

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

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

45
  Handle<String> NumericAsString() const;
46

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

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

57
  V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
58

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

62 63 64 65 66 67 68
  // 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")
69
    AUTO     // numerical descriptions are used only when no more specific
70 71 72 73 74
             // version is available ("yesterday")
  };
  inline void set_numeric(Numeric numeric);
  inline Numeric numeric() const;

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

  STATIC_ASSERT(Numeric::AUTO <= NumericBit::kMax);
  STATIC_ASSERT(Numeric::ALWAYS <= NumericBit::kMax);
80

81
  DECL_PRINTER(JSRelativeTimeFormat)
82

83
  TQ_OBJECT_CONSTRUCTORS(JSRelativeTimeFormat)
84 85 86 87 88 89 90 91
};

}  // namespace internal
}  // namespace v8

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

#endif  // V8_OBJECTS_JS_RELATIVE_TIME_FORMAT_H_